How To Ignore a Filter On Applying Filter the Content In a Function

I am having a filter for the content that adds a Table of Contents at the top of my posts. I took the basic idea from an answer at Stack Exchange itself.

I add the filter as follows:

add_filter('the_content', array('TableOfContents', 'writeTOC'), 100);

And until here it is fine, but the problem comes in another function. There is a function in my plugin that takes the first 144 characters of the given post and coverts it into the Meta Description. Now due to the filter mentioned above, the Meta Description doesn’t contain the actual content of my post.

        $meta = apply_filters('the_content', $post->post_content);
        $meta = strip_tags($meta);
        $meta = strip_shortcodes($meta );
        $meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
        $meta = substr($meta, 0, 144);

Rather, my meta description has the first 144 characters including the Table of Contents.

I want to exclude the Table of Contents from my Meta Description, but re-positioning the code did no help.

Below I have attached the full code:

<?php
function Milyin_Head(){ 

global $post; 
if (!is_page()) {   
    if (!is_singular() ) {return; }
    elseif (!empty( $post->post_excerpt)) {
        $meta = $post->post_excerpt ;   
} 
    else {
        // THIS IS MY PROBLEM LINE, i USE Apply Filters for post content....
        $meta = apply_filters('the_content', $post->post_content);
        $meta = strip_tags($meta);
        $meta = strip_shortcodes($meta );
        $meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
        $meta = substr($meta, 0, 175);
echo '<meta name="description" content="'. $meta . '"/>'; 
echo '<meta name="og:description" content="'. $meta . '"/>';
echo '<meta name="twitter:description" content="'. $meta . '"/>';
}
}
else {
echo '<meta name="description" content="Milyin Devotes to Make A World Having Vast Majority Of People Who are Passionate About There Work, Who feel proud to have different Thoughts, Beliefs and Opinions. Milyin Supports all those who Improve Society and bring a change."/>'; 
echo '<meta name="og:description" content="Milyin Devotes to Make A World Having Vast Majority Of People Who are Passionate About There Work, Who feel proud to have different Thoughts, Beliefs and Opinions. Milyin Supports all those who Improve Society and bring a change."/>';
echo '<meta name="twitter:description" content="Milyin Devotes to Make A World Having Vast Majority Of People Who are Passionate About There Work, Who feel proud to have different Thoughts, Beliefs and Opinions. Milyin Supports all those who Improve Society and bring a change."/>';

}
 }
add_action('wp_head', 'Milyin_Head');


//This Function is Now Over, the Next Function Generates the Table Of Content

// Below is the Table Of Content Class, i added this for the sake of someone who wants to actually understand and in depth analyse my code. Its better to just move to bottom where i Apply The Filter

class TableOfContents {

    /**
     * Counts the occurence of header elements in WordPress content
     * 
     * @param type $content
     * @return null|boolean|array
     */
    static function hasToc($tiers, $content) {

        $pattern = '/<h[1-' . $tiers . ']*[^>]*>(.*?)<\/h([1-' . $tiers . '])>/';
        $return = array();
        if (empty($content))
            return null;

        if (!preg_match_all($pattern, $content, $return)) {
            return false;
        }
        return $return;
    }

    /**
     * Generates a table of content only when singular pages are being viewed
     * 
     * @param type $tiers
     * @param type $text
     */
    static function generateTableOfContents($tiers, $content, $draw = TRUE, $return = array()) {

        if (!is_singular())
            return $content;


        $content = $toc . $content;
        $searches = array();
        $replaces = array();
        $return = (is_array($return) && !empty($return) ) ? $return : TableOfContents::hasToc($tiers, $content);

        if ($draw && !empty($return)):
             $toc="<div class="Milyin-Container">";
            $toc .= "<span style=\"font-size:18px;\">Table of Contents</span>";
            $toc .= "<span style=\"float:right; width: 30px;height: 30px;margin: 5px;\" data-toggle=\"collapse\" data-target=\".list\">";
    $toc .= '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0px" y="0px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve"><g><g><path d="M491.318,235.318H20.682C9.26,235.318,0,244.577,0,256s9.26,20.682,20.682,20.682h470.636    c11.423,0,20.682-9.259,20.682-20.682C512,244.578,502.741,235.318,491.318,235.318z"/></g></g><g><g><path d="M491.318,78.439H20.682C9.26,78.439,0,87.699,0,99.121c0,11.422,9.26,20.682,20.682,20.682h470.636    c11.423,0,20.682-9.26,20.682-20.682C512,87.699,502.741,78.439,491.318,78.439z"/></g></g><g><g><path d="M491.318,392.197H20.682C9.26,392.197,0,401.456,0,412.879s9.26,20.682,20.682,20.682h470.636    c11.423,0,20.682-9.259,20.682-20.682S502.741,392.197,491.318,392.197z"/></g></g></svg></span>' ;
            $toc .= "<div class=\"list collapse\">";
            $toc .= "<ul class=\"parent start\">";
            $tags = reset($return);
            $titles = $return[1];
            $url = $return[1];
            $levels = end($return);
            $_level = 2;
            $chapters = array('0','0','0','0','0','0');

            $count = 0;
            foreach ($tags as $i => $htag) {
                $count++;
                $attributes = array();
                $href = $url[$i];
                    $href = strtolower($href);
                    strip_tags($href);
                    $href = preg_replace("/[^a-z0-9_\s-]/", "", $href);
                    $href = preg_replace("/[\s-]+/", " ", $href);
                    $href = preg_replace("/[\s_]/", "-", $href);


                $newId = 'id="' . $href . '"';
                $newhtag = $newId . '>';
                $htagr = str_replace('>' . $titles[$i], "\t" . $newhtag  . $titles[$i], $htag);
                $searches[] = $htag;
                $replaces[] = $htagr;


                if ((int)$levels[$i] === (int)$_level):
                        $chapters[$_level-1] = ((int)$chapters[$_level-1]+1);
                        $chapter = implode('.', array_slice($chapters, 1, ($levels[$i]-1)  ) );
                        $toc .= '<li><span>' . strval($chapter) . '</span> <a href="#' . $href . '">' . $titles[$i] . '</a></li>';
                endif;

                if ($levels[$i] > $_level) {
                    $_steps = ((int) $levels[$i] - (int) $_level);

                    for ($j = 0; $j < $_steps; $j++):
                        $toc .= '<ol class="continue">';
                        $chapters[$levels[$i]-1+$j] = (int)$chapters[$levels[$i]-1+$j]+1;
                        $_level++;
                    endfor;
                    $chapter = implode('.', array_slice($chapters, 1, ($levels[$i]-1)  ) );
                        $toc .= '<li><a href="#' . $href . '">' . $titles[$i] . '</a></li>';
                }

                if ($levels[$i] < $_level) {

                    $_steps = ((int) $_level - (int) $levels[$i]);
                    $chapters[$levels[$i]-1] = (int)$chapters[$levels[$i]-1]+1;
                    $_olevel = $_level;
                    for ($j = 0; $j < $_steps; $j++):
                        $chapters[$levels[$i]+$j] = 0;
                        $toc .= '</ol>';
                        $_level--;
                    endfor;

                    $chapters[$_olevel-1] = 0;
                    $chapter = implode('.', array_slice($chapters, 1, ($levels[$i]-1)  ) );

                        $toc .= '<li><a href="#' . $href . '">' . $titles[$i] . '</a></li>';

                }
            }
            $toc .= '</ul>';
            $toc .= '</div></div><div class="clear"></div>';
            $content = str_replace($searches, $replaces, $content);
            $content = $toc . $content;
        endif;

        return $content;
    }

    /**
     * Appends the table of content to the $content
     * AKA. Executes our filter
     * 
     * @param type $content
     * @return type
     */
    static function writeToc($content) {

        $content = TableOfContents::generateTableOfContents(4, $content, TRUE);
        return $content;
    }

}

add_filter('the_content', array('TableOfContents', 'writeTOC'), 100);

// The Above Line Adds the Table Of Content Before The Post Content
// Problem is that in the first function of code, i want it to exclude this filter. I don't want the Table Of Content text to become my Meta Description


?>

2 Answers
2

You can temporarily detach the function from the hook.

    // THIS IS MY PROBLEM LINE, i USE Apply Filters for post content....

    $priority = has_filter('the_content', ['TableOfContents', 'writeTOC'] );
    if ( $priority !== false )
        remove_filter('the_content', ['TableOfContents', 'writeTOC'], $priority );

    $meta = apply_filters('the_content', $post->post_content);

    if ( $priority !== false )
        add_filter('the_content', ['TableOfContents', 'writeTOC'], $priority );


    $meta = strip_tags($meta);
    $meta = strip_shortcodes($meta );

Codex:

  • has_filter()
  • remove_filter()
  • add_filter()

Leave a Comment