targeting title in wordpress post

I am working on a wordpress plugin that modifies the title of a post. I only want to do this when I am viewing a single post. To be specific, I want to add a link beside the title, but for purposes of the question, I will be adding some arbitary text.

I started out by using the ‘the_title’ filter hook, and calling this function.

Read More
function add_button_to_title($title)
{
  global $post;
  if(is_single())
  {
    return $title.'googly googly';
  }
  return $title;
}

The problem is, the links on the side bar apparently also use ‘the_title’, as I saw my text showing up in the side bars as well, which led me to:

if(is_single() && in_the_loop())

But then, in my theme(and i suppose themes in general) there is a link to the previous post and next post, which also uses ‘the title’ filter. So finally I have:

if(is_single() && in_the_loop() && ($post->post_title == $title))

The last conditional basically makes sure that it is the title of the post that is being printed, not the title of the next or previous post. This works but I am not sure how well it will work given different themes…It seems like terribly hacked together. Any advice from wordpress gurus out there? I am worried that the title would be modified for other reasons and the conditional will fail.

Any help is appreciated!

Related posts

Leave a Reply

4 comments

  1. Ying,

    There isn’t really a good solution except, as ShaderOp said, requiring theme modification. Your solution will work for the most part. The only exception is if the theme developer has changed the query in a page. I’d say this is probably a good enough solution that it’ll cover more than 95% of the cases you’d run into.

  2. I solved a similar issue by adding a check to see if the title being filtered matches the title of the post. This avoids the issue with other post titles on the page (in sidebar, menu) also getting filtered.

    function add_button_to_title( $title ) {
      global $post;
      if( is_single() && $title == $post->post_title ) {
        return $title . 'googly googly';
      } else {
        return $title;
      }
    }
    
  3. Wouldn’t it be easier to keep the original version of your add_button_to_title function, but instead of hooking it to a filter, call it directly from your single.php page in the appropriate place?

    For example, somewhere in your theme’s single.php, instead of this:

    <h3 class="storytitle">
        <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
    </h3>
    

    Use this:

    <h3 class="storytitle">
        <a href="<?php the_permalink() ?>" rel="bookmark">
            <?php echo add_button_to_title(the_title('', '', false); ?>
        </a>
    </h3>
    
  4. today I ran into a similar problem. the_title gets called several times accross the whole page (e.g., in the html-head, the menus, the sidebar). I followed a similar approach using conditionals and the post/page id.

    Additionally, I added a boolean flag which is set to true using the ‘the_content’ filter. So the title gets changed until the content is displayed. This way, I ensure that sidebars/widgets are not affected (e.g. Thematic theme has a default widget with links to pages – here the other conditionals would not be helpful as get_the_id() would return an equivalent). This ONLY works if the theme uses sidebars on the right. I did not find a way yet how to hook in directly before the ‘the_title’ call for the page/post to enable the boolean flag.

    function myplugin_adjust_title($title, $id) {
        global $myplugin_title_changed;
    
        if ($myplugin_title_changed) {
            return $title;
        }
    
        if (in_the_loop() && is_page('myplugin') && $id == get_the_ID()) {
            $title = '';
        }
        return $title;
    }
    add_filter('the_title', 'myplugin_adjust_title', 10, 2);
    
    function myplugin_adjust_title_helper_content($content) {
        global $myplugin_title_changed;
        $myplugin_title_changed = true;
        return $content;
    }
    add_filter('the_content', 'myplugin_adjust_title_helper_content');