Wordpress: How to Hook into get_the_content

admin

Administrator
Staff member
I am looking for a proper way to hook into
Code:
get_the_content
method in Wordpress. Wordpress provide hooks for
Code:
the_content
,
Code:
the_excerpt()
and
Code:
get_the_excerpt()
but not for
Code:
get_the_content()
. I have a plugin which I maintain that appends and prepends some HTML in the content post.
I used the same for
Code:
the_excerpt()
and
Code:
get_the_excerpt()
. What should I do to support
Code:
get_the_content()
as well as I see most themes now use
Code:
get_the_content()
? ie.: if the theme is using
Code:
get_the_content()
instead of
Code:
the_content()
, my plugin fails.

My plugin code is the following:

Code:
add_action('init', 'my_init');

function my_init() {
  add_filter('the_content', 'my_method', 12);
  add_filter('get_the_content', 'my_method', 12);
}


function my_method($content) {
  $content = "blah blah".$content."blah blah";

  return $content;
}

The above code (in my plugin) listens to
Code:
the_content call
from the theme and add my own content to the article.

Assume this is the method in a theme file:

Code:
<div class="entry-content">
  <?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentytwelve' ) ); ?>
</div><!-- .entry-content -->

It works properly in the above case.

But if the theme code is something like this (Note the change from the_content() to
Code:
get_the_content())
.

Code:
<div class="entry-content">
  <?php echo get_the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentytwelve' ) ); ?>
</div><!-- .entry-content -->

My plugin fails as there is no hooks defined for
Code:
get_the_content
. As I am not in control of theme code, the solution which Wordpress explains in the <a href="http://codex.wordpress.org/Function_Reference/get_the_content" rel="nofollow">documentation</a> doesn't help for me:

Code:
&lt;?php apply_filters('the_content',get_the_content( $more_link_text, $stripteaser, $more_file )) ?&gt;

Update: Problem definition expanded for clarity.