Srijith Vijayamohan
Srijith Vijayamohan

Reputation: 915

Wordpress: How to Hook into get_the_content

I am looking for a proper way to hook into get_the_content method in Wordpress. Wordpress provide hooks for the_content, the_excerpt() and get_the_excerpt() but not for 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 the_excerpt() and get_the_excerpt(). What should I do to support get_the_content() as well as I see most themes now use get_the_content()? ie.: if the theme is using get_the_content() instead of the_content(), my plugin fails.

My plugin code is the following:

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 the_content call from the theme and add my own content to the article.

Assume this is the method in a theme file:

<div class="entry-content">
  <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</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 get_the_content()).

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

My plugin fails as there is no hooks defined for get_the_content. As I am not in control of theme code, the solution which Wordpress explains in the documentation doesn't help for me:

<?php apply_filters('the_content',get_the_content( $more_link_text, $stripteaser, $more_file )) ?>

Update: Problem definition expanded for clarity.

Upvotes: 0

Views: 6203

Answers (3)

CyberSEO.net
CyberSEO.net

Reputation: 3

The following code will do the trick (note my_post there):

add_action('init', 'my_init');

function my_init() {
  add_filter('the_content', 'my_method', 12);
  add_filter('the_excerpt', 'my_method', 12);
  add_filter('the_post', 'my_post');
}

function my_post($post) {
  $post->post_excerpt = get_the_excerpt();
  $post->post_content = get_the_content();
  return $post;
}

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

Upvotes: 0

David Chase
David Chase

Reputation: 2073

Have you tried the following?

According to the codex:

If you use plugins that filter content (add_filter('the_content')), then this will not apply the filters, unless you call it this way (using apply_filters):

<?php apply_filters('the_content',get_the_content( $more_link_text, $stripteaser, $more_file )) ?>

Its documented here in the wordpress codex.

EDITED

Try using output buffering to put the_content in a variable.

That way you dont have to worry about get_the_content

ob_start();
the_content();
$newContent = ob_get_clean();

Now its stored in a variable called $newContent easy for you to manipulate.

Upvotes: 2

John Peter
John Peter

Reputation: 2928

Just Try With The Following :

Example With Cform:

add_filter('get_the_content', 'cforms_insert',10);

For Your Reference

(or)

$content = get_the_content();
$content = apply_filters('the_content', $content);
$panels = explode("[newpage]", $content);

I think this may help you to resolve your problem.

Upvotes: -1

Related Questions