0

My plugin currently hooks properly with the_content and the_excerpt, but when I try to hook the_post, I get the error "Catchable fatal error: Object of class WP_Post could not be converted to string..." The string appends fine in the first two cases, but the_post appears not to be string, hence the failure. What's needed to interface the_post and the plugin add-on?

add_filter('the_content', 'the_plugin');
add_filter('the_excerpt', 'the_plugin');
add_action('the_post', 'the_plugin'); // breaks

function the_plugin($content) {
  global $add_the_script;

  $content_add_on = '<div class="ACME_STATS" uid="x"></div>';
  $theContent = $content.$content_add_on;
  $add_the_script = true;

  return ($theContent);
}

Addendum 1:

Below is the PHP that renders the front page. It shows 25-word excerpts using <?php echo mts_excerpt(29);?>. How can this mts_excerpt be hooked to append plugin content?

    <?php  $j=0; $i =0; if (have_posts()) : while (have_posts()) : the_post(); ?>
        <article class="<?php echo 'pexcerpt'.$i++?> post excerpt <?php echo (++$j % 2 == 0) ? 'last' : ''; ?>">
            <?php if (empty($mts_options['mts_full_posts'])) : ?>
                <?php if ( has_post_thumbnail() ) { ?>
                    <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="nofollow" id="featured-thumbnail">
                        <?php echo '<div class="featured-thumbnail">'; the_post_thumbnail('featured',array('title' => '')); echo '</div>'; ?>
                        <div class="featured-cat"><?php $category = get_the_category(); echo $category[0]->cat_name; ?></div>
                        <?php if (function_exists('wp_review_show_total')) wp_review_show_total(true, 'latestPost-review-wrapper'); ?>
                    </a>
                <?php } else { ?>
                    <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="nofollow" id="featured-thumbnail">
                        <div class="featured-thumbnail">
                            <img src="<?php echo get_template_directory_uri(); ?>/images/nothumb.png" class="attachment-featured wp-post-image" alt="<?php the_title(); ?>">
                        </div>
                        <div class="featured-cat"><?php $category = get_the_category(); echo $category[0]->cat_name; ?></div>
                        <?php if (function_exists('wp_review_show_total')) wp_review_show_total(true, 'latestPost-review-wrapper'); ?>
                    </a>
                <?php } ?>
            <?php endif; ?>
            <header>                        
                <h2 class="title">
                    <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a>
                </h2>
                <div class="post-info"><span class="theauthor"><?php the_author_posts_link(); ?></span> | <span class="thetime"><?php the_time( get_option( 'date_format' ) ); ?></span></div>

            </header><!--.header-->
            <?php if (empty($mts_options['mts_full_posts'])) : ?>
                <div class="post-content image-caption-format-1">
                    <?php echo mts_excerpt(29);?>
                </div>
                <span class="readMore"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="nofollow"><?php _e('Read More','mythemeshop'); ?></a></span>
            <?php else : ?>
                <div class="post-content image-caption-format-1 full-post">
                    <?php the_content(); ?>
                </div>
                <?php if (mts_post_has_moretag()) : ?>
                    <span class="readMore"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="nofollow"><?php _e('Read More','mythemeshop'); ?></a></span>
                <?php endif; ?>
            <?php endif; ?>
        </article>
    <?php endwhile; else: ?>
  • What part of the post are you trying to edit? You can find all the post object variables here: codex.wordpress.org/Class_Reference/WP_Post – czerspalace Oct 6 '15 at 20:57
  • 2
    the_post is an action, not a filter, it's not for altering the content of the post object. – Milo Oct 6 '15 at 21:11
  • 1
    mts_excerpt is not a native WordPress function. You may have better luck contacting the theme author – czerspalace Oct 7 '15 at 0:32

Your Answer

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

Browse other questions tagged or ask your own question.