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):

   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?

  • You cannot remove tags in this manner. Find an action name plugin uses to add these and do remove_action on those. – sandrodz Nov 2 '18 at 10:03

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.

| improve this answer | |
  • My function is called now, he adds the meta tag. But the other one is still in my head. How can I remove it first? – nielsv Apr 27 '15 at 12:10
  • Do you have those meta tags set in the header.php? Or where does the others come from? Does some plugin insert those, and if so, you should check the support for the plugin in question. – jimihenrik Apr 27 '15 at 17:09
  • I think the tags are added by the "WordPress SEO by Yoast" plugin. – nielsv Apr 28 '15 at 8:17
  • edit: nope. Can't you disable the facebook meta from outputting through the plugin in the plugin options? another edit: someone said this on the forums global $wpseo_og, $wpseo_twitter; remove_action( 'wpseo_head', array( $wpseo_og, 'opengraph' ), 30 ); remove_action( 'wpseo_head', array( $wpseo_twitter, 'twitter' ), 40 ); – jimihenrik Apr 28 '15 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.