Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
    }
}
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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.

share|improve this answer
    
Actually I basically have what you have there, but the problem is some plug-ins that do similar things to mine (whereas the $content is replaced with something else, and not actually shown) then the two plug-ins clash. I've updated my post to include all of my function. –  Stephen Cook Sep 11 '11 at 13:19
1  
@Stephen Cook - your code certainly does not have $content as the function argument - see in @Chip Bennett's answer function magicalendar_get_event_page( $content ) { - also, why don't you consider to create a page template for this page? –  Michael Sep 11 '11 at 14:47
    
A Page Template is quite possibly the way to go here. P.S. see edited answer. –  Chip Bennett Sep 11 '11 at 19:48
    
I actually did have $content as a parameter in my plug-in, I don't know how I found a copy to paste here without it. But thanks, I'll try using "return" not "echo" and also a page template –  Stephen Cook Sep 11 '11 at 20:06
    
I'd probably go with a custom Page template. If you go that route, you don't even have to call the_content() (just put your custom code in place of the Loop). That way, you don't even have to worry about other Plugins filtering the_content. –  Chip Bennett Sep 11 '11 at 20:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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