Manipulating wp_head content

I’d like to know if it is possible for me to manipulate the output of wp_head()?

Right now I’m using the Yoast SEO plugin to add some social tags into my posts (og:*).

Read More

Now this site is an remake of an older umbraco based blog, and the permalink structure is different, so the old urls are 301 to the new ones, but addthis requires the old url to keep the right count, so we have a custom field that provides the “addthis:url” when required…
Unfortunately facebook & twitter do not use that url, but they use the pages “og:url” meta tag, that the plugin provides (which points to the new url), and seems that it can not be manipulated in the post.

My idea is that before the wp_head is printed out, I would basically do an search-and-replace to change that “og:url” to correct one, but I have not found a way to do that…

So what I’m asking is that is my idea correct and how to do it, or is there a better/official way to fix this solution?

Related posts

3 comments

  1. Information attached to the wp_head action hook is echoed (if it needs to be echoed) as it occurs. There is no “wp_head” content string that you can search and replace.

    1. You will need to find the callback functions/methods for the data
      you are interested in manipulating and hope that there hooks built
      in that will help you out.
    2. Or remove those callbacks and add your own to replace them
    3. Or try output buffering around wp_head:

      ob_start();     
      wp_head(); 
      $head = ob_get_contents();
      ob_end_clean();
      echo $head;
      
  2. You could do one of the following things, for instance:

    1. Hook into wp_head and output your own meta information. This could be defined as a post meta.
    2. Hook into a filter/action of the plugin, if there is one, and alter the meta before it gets printed out.

    // Edit

    I just had a short glimpse at the plugin.

    If you want to alter the og:url, you can do this like so:

    if (is_single()) {
        remove_action('wpseo_opengraph', array('WPSEO_OpenGraph', 'url'), 12);
        add_action('wpseo_opengraph', 'wpse_131062_my_og_url', 12);
    }
    
    function wpse_131062_my_og_url() {
        // this post meta has to be set up and filled by you!
        if ($url = get_post_meta('my_og_url')) {
            ?>
            <meta property="og:url" content="<?php echo esc_attr(esc_url($url)); ?>" />
            <?php
        }
    }
    

    This code is untested, though.

  3. I managed to find this post:
    https://wordpress.stackexchange.com/a/75168/45611

    It basically had what I needed.

    /*
     * This whole block here changes the og:url that WordPress Seo Yoast provides
     * It uses the addthis_share_url custom field, and if it is not present, it defaults
     * to the permalink, just like the plugin does.
     */
    add_action('get_header', 'blog_template_add_ob_start');
    add_action('wp_head', 'blog_template_add_ob_end_flush', 100);
    function blog_template_add_ob_start() {
        ob_start('blog_template_add_filter_wp_head_output');
    }
    function blog_template_add_ob_end_flush() {
        ob_end_flush();
    }
    function blog_template_add_filter_wp_head_output($output) {
        $altUrl = get_post_custom_values('addthis_share_url')[0];
        $url = get_permalink();
    
        if ($altUrl && is_single()) {
            $output = str_ireplace('<meta property="og:url" content="' . $url . '" />', '<meta     property="og:url" content="' . esc_attr(esc_url($altUrl)) . '" />', $output);
        }
        return $output;
    }
    

    Thanks people, you were helpful!

Comments are closed.