add_filter for specific pages

Is it possible to add this filter

add_filter("the_content", "magicalender_get_event_page");

But only for one specific page, for example

if($post->post_name == "magicalenderpage")

If I put the if statement inside the function, then on any other pages the content isn’t displayed – and with an else statement afterwards that echoes out the content of the page otherwise I encounter problems where it interferes with other plug-ins that also do things with

add_filter("the_content", "foo");

The magicalender_get_event_page function is as follows

function magicalender_get_event_page(){
    global $wpdb;
    global $post;

    if($post->post_name == "magicalenderpage"){
        echo "<div class=\"magi_calender\">";
        for($i = 0; $event = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."magicalender", ARRAY_A, $i); $i++){
            echo"
                <div class=\"event\">
                    <h1>".$event['event']."</h1>
                    <h2>".date('m/d/Y', strtotime($event['dmy']))."</h2>
                    <div>".str_replace("&amp;", "&", str_replace("&#39;", "'", str_replace("&#34;", "\"", $event['desc'])))."</div>
                </div>
            ";
        }
        echo "</div>";

        $o = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."magioptions", ARRAY_A);
        echo"
            <script type=\"text/javascript\">
                var priority = '".$o['dm']."';
                var customDays = new Array('".$o['monday']."', '".$o['tuesday']."', '".$o['wednesday']."', '".$o['thursday']."', '".$o['friday']."', '".$o['saturday']."', '".$o['sunday']."');
                var customMonths = new Array('".$o['january']."', '".$o['february']."', '".$o['march']."', '".$o['april']."', '".$o['may']."', '".$o['june']."', '".$o['july']."', '".$o['august']."', '".$o['september']."', '".$o['october']."', '".$o['november']."', '".$o['december']."');
                var customMonthsShort = new Array('".$o['jan']."', '".$o['feb']."', '".$o['mar']."', '".$o['apr']."', '".$o['ma']."', '".$o['jun']."', '".$o['jul']."', '".$o['aug']."', '".$o['sep']."', '".$o['oct']."', '".$o['nov']."', '".$o['dec']."');
                jQuery(\".magi_calender\").magiCalender(priority, customDays, customMonths, customMonthsShort);
            </script>
        ";
    }
    else{
        return $content;
    }
}

1 Answer
1

It is helpful to post your entire filter and callback code, rather than just pieces.

But, I suspect that the problem is that you’re not returning $the_content outside of your conditional, e.g.:

function magicalendar_get_event_page( $content ) {
    if ( $post->post_name == 'magicalendarpage' ) {
        // Do something to $content
        return $content;
    }
}
add_filter( 'the_content', 'magicalendar_get_event_page' );

If that’s the case, move the return outside the conditional:

function magicalendar_get_event_page( $content ) {
    if ( $post->post_name == 'magicalendarpage' ) {
        // Do something to $content
    }
    return $content;
}
add_filter( 'the_content', 'magicalendar_get_event_page' );

To know why/how it’s interfering with other filters on 'the_content', we probably need to see your code.

EDIT

At least two problems:

  1. You’re not passing $content as an argument to your filter callback
  2. DO NOT ECHO FILTER OUTPUT! Return it. That is very likely causing your problem with other filters applied to the_content.

If you need to filter the_content specifically before or after other filters, then add a priority to your add_filter() call. 10 is the default. Use a lower number to filter sooner; use a higher number to filter later.

Leave a Comment