1

I want to add_filter for the_content in functions.php of my theme. I have added code which just display echo but it seams that my filter is not applied.

function add_mod_hatom_data($content) {
   // $t = get_the_modified_time('F jS, Y');
   //$author = get_the_author();
   // $title = get_the_title();
   //if(is_single()) {
    echo 'perrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr';
   //   $content .= '<div class="hatom-extra"><span class="entry-title">'.$title.'</span> was last modified: <span class="updated"> '.

//$t.'</span> by <span class="author vcard"><span class="fn">'.$author.'</span></span></div>';
   // }
    return $content;
    }

 add_filter('the_content', 'add_mod_hatom_data');

I have tried to call it like this :

add_filter('the_content', 'add_mod_hatom_data', 99);

or change position to be on top of functions.php with no success.

Do I need to enable somewhere add_filter or it is overridden by some other function ? NOTE: In my single post template I have :

<?php get_header(); ?>      
            <div id="content" class="clearfix row">
                <div id="main" class="col-sm-8 clearfix" role="main">
                    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                    <article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article" itemscope itemtype="http://schema.org/BlogPosting">

                        <header>



                            <div class="page-header clip_content single-post">
                                            <div class="page-header">
                                                <h1 class="single-title-wg" itemprop="headline">
                                                                                                        <?php $category = get_the_category();
                                                                                                        if ($category[0]) {
                                                                                                          echo '<b>'.$category[0]->cat_name.' '.$post->ID.'</b>';
                                                                                                        }
                                                                                                        ?>
                                                </h1>
                                                                                            <h4 class="single-title" itemprop="headline">
                                                                                            <p class="lead">                                                                                                         
                                                                                                                <?php the_title(); ?>
                                                        </p>
                                                                                                           </h4>

                                            </div>


                <div class="item_footer">
                                                        </div>            
                                <?php $category = get_the_category();
                                if ($category[0]) {?>
                                <p class="authorParagraph">
                                    <?php echo '<a href="' . get_category_link($category[0]->term_id) . '" class="clearfix">' . $category[0]->cat_name . '</a></p>';
                                    }
                                    ?>
                            </div>
                            </div>

                        </header> <!-- end article header -->

                        <footer>
                            <!-- <?php the_tags('<p class="tags"><span class="tags-title">' . __("Tags","wpbootstrap") . ':</span> ', ' ', '</p>'); ?> -->
                        </footer> <!-- end article footer -->

                    </article> <!-- end article -->



                    <?php endwhile; ?>          

                    <?php else : ?>

                    <article id="post-not-found">
                        <header>
                            <h1><?php _e("Not Found", "wpbootstrap"); ?></h1>
                        </header>
                        <section class="post_content">
                            <p><?php _e("Sorry, but the requested resource was not found on this site.", "wpbootstrap"); ?></p>
                        </section>
                        <footer>
                        </footer>
                    </article>

                    <?php endif; ?>

                </div> <!-- end #main -->

            </div> <!-- end #content -->

<?php get_footer(); ?>
  • Please enhance the source of the loop inside the template. Do you use the default wp functions the_content() to get the content on front end? If not, then is it not possible to fire to this hook. Maybe you must enhance your solution for output the content via apply_filter. See also the documentation of the hook. – bueltge May 21 '14 at 8:17
  • that 'echo' possibly ends up somewhere before the html of the site is output; try to add the string to the $content like return 'rrr'.$content; – Michael May 21 '14 at 8:28
  • Problem is that my filter is not applied. I have just comment out code in function and put echo to prove that function is not called in single post. – Radenko Zec May 21 '14 at 9:54
  • @bueltge I suspect you are going in good direction but I am not sure that I understand. I think that problem is related because in single post template I use the_post() and there is no the_content() ? – Radenko Zec May 21 '14 at 9:55
1

This part of the single post template is called the loop:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

Within the loop you need to call in the content:

<?php the_content(); ?>

Replace your single post template with this and see if it works now:

<?php get_header(); ?>      
<div id="content" class="clearfix row">
    <div id="main" class="col-sm-8 clearfix" role="main">
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

        <article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article" itemscope itemtype="http://schema.org/BlogPosting">

            <header>



                <div class="page-header clip_content single-post">
                                <div class="page-header">
                                    <h1 class="single-title-wg" itemprop="headline">
                                        <?php $category = get_the_category();
                                        if ($category[0]) {
                                          echo '<b>'.$category[0]->cat_name.' '.$post->ID.'</b>';
                                        }
                                        ?>
                                    </h1>
                                    <h4 class="single-title" itemprop="headline">
                                    <p class="lead"> <?php the_title(); ?></p></h4>
                                    <p><?php the_content(); ?></p>

                                </div>


    <div class="item_footer">
                                            </div>            
                    <?php $category = get_the_category();
                    if ($category[0]) {?>
                    <p class="authorParagraph">
                        <?php echo '<a href="' . get_category_link($category[0]->term_id) . '" class="clearfix">' . $category[0]->cat_name . '</a></p>';
                        }
                        ?>
                </div>
                </div>

            </header> <!-- end article header -->

            <footer>
                <!-- <?php the_tags('<p class="tags"><span class="tags-title">' . __("Tags","wpbootstrap") . ':</span> ', ' ', '</p>'); ?> -->
            </footer> <!-- end article footer -->

        </article> <!-- end article -->



        <?php endwhile; ?>          

        <?php else : ?>

        <article id="post-not-found">
            <header>
                <h1><?php _e("Not Found", "wpbootstrap"); ?></h1>
            </header>
            <section class="post_content">
                <p><?php _e("Sorry, but the requested resource was not found on this site.", "wpbootstrap"); ?></p>
            </section>
            <footer>
            </footer>
        </article>

        <?php endif; ?>

    </div> <!-- end #main -->

</div> <!-- end #content -->

<?php get_footer(); ?>
  • Thanks it worked. I have title and content same so I just need to replace the_title with the_content. – Radenko Zec May 21 '14 at 11:11
1

That is because the variable '$content' is empty. The way you are using this filter is correct. Instead of 'echo' put your values in the '$content variable' and then the 'return $content' will in effect echo this out to the page. Try this:

function add_mod_hatom_data($content) {
   // $t = get_the_modified_time('F jS, Y');
   //$author = get_the_author();
   // $title = get_the_title();
   //if(is_single()) {
    $content = 'perrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr';
   //   $content .= '<div class="hatom-extra"><span class="entry-title">'.$title.'</span> was last modified: <span class="updated"> '.

//$t.'</span> by <span class="author vcard"><span class="fn">'.$author.'</span></span></div>';
   // }
    return $content;
    }

 add_filter('the_content', 'add_mod_hatom_data');
  • This also does not work. I have comment out code in this function and put echo to prove that function will not be called. I have tested also your code 'perrrrr...' is not displayed even I put it in $content variable. I suspect this is related because in template of single post I have the_post() and not the_content() or something ? – Radenko Zec May 21 '14 at 9:57
  • Can you add in your single post template code so we can see. (All of it) – Matt Royal May 21 '14 at 10:04
  • Sure thing. I have updated question with simplified template. – Radenko Zec May 21 '14 at 10:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.