最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Change meta tags programatically

programmeradmin2浏览0评论

Is there a way to change the meta tags not in the head but in a template file through code?

I would like to change the meta tags

<meta property="og:title" content="title" /> 
<meta property="og:description" content="description" /> 
<meta property="og:image" content="thumbnail_image" />

They are automatically set when calling the facebook SDK. Is there a way to change these tags in my code (not in header.php)?

I've tried to do this in my child template (content-share.php):

<?php
   add_action( 'wp_head', 'add_meta_tags' , 10 );
?>

And then in functions.php of my theme I have:

function add_meta_tags() {
    echo '<meta property="og:title" content="Test">' . "\n";
}

But that doesn't do anything. Also when I add a die; in the function nothing changes. It's like my function isn't called.

How can I solve this?

Is there a way to change the meta tags not in the head but in a template file through code?

I would like to change the meta tags

<meta property="og:title" content="title" /> 
<meta property="og:description" content="description" /> 
<meta property="og:image" content="thumbnail_image" />

They are automatically set when calling the facebook SDK. Is there a way to change these tags in my code (not in header.php)?

I've tried to do this in my child template (content-share.php):

<?php
   add_action( 'wp_head', 'add_meta_tags' , 10 );
?>

And then in functions.php of my theme I have:

function add_meta_tags() {
    echo '<meta property="og:title" content="Test">' . "\n";
}

But that doesn't do anything. Also when I add a die; in the function nothing changes. It's like my function isn't called.

How can I solve this?

Share Improve this question asked Apr 27, 2015 at 9:50 nielsvnielsv 1731 gold badge5 silver badges17 bronze badges 1
  • You cannot remove tags in this manner. Find an action name plugin uses to add these and do remove_action on those. – sandrodz Commented Nov 2, 2018 at 10:03
Add a comment  | 

1 Answer 1

Reset to default 1

I think you need to do the add_action( 'wp_head', 'add_meta_tags' , 10 ); in your functions.php instead of the template to get the function called. And it adds it then everywhere where you have wp_head(). So if you want to only call it content-share.php you need some conditioning logic like

if (is_page_template('page-templates/content-share.php')) {
  add_action( 'wp_head', 'add_meta_tags' , 10 );
}

Of course change the page-templates folder to be what ever the folder is where you have the template in your theme folder.

发布评论

评论列表(0)

  1. 暂无评论