Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress - Apply remove_filter only on one page

Tags:

wordpress

I am inserting multiple pages in one page (with showmultiplepages plugin), and one page includes a php file (with exec-php). I want to disable a filter only for this included page. If I add

remove_filter( 'the_content', 'wpautop' );

to my included page, any page comming after this page won't have filters too.

Is there some tag like 'the_page' so that only the page will have no filter?

Thanks for help.

like image 397
Ilyssis Avatar asked Jun 08 '11 22:06

Ilyssis


3 Answers

I know this is an old question, but I thought I'd chime in for anyone looking to do this in a more general sense (e.g. not in conjunction with plugin output) and say that you could also just add this to your functions.php file:

add_filter('the_content', 'specific_no_wpautop', 9);
function specific_no_wpautop($content) {
    if (is_page('YOUR PAGE')) { // or whatever other condition you like
        remove_filter( 'the_content', 'wpautop' );
        return $content;
    } else {
        return $content;
    }
}
like image 70
mroncetwice Avatar answered Nov 15 '22 10:11

mroncetwice


I suggest creating a page template for the "one page includes a php file (with exec-php)". Then add an if statement around the remove_filter(...) statement.

if (!is_page_template('my-page.php'))
  remove_filter('the_content', 'wpautop');

Hope it works. ;P

like image 30
Box Avatar answered Nov 15 '22 10:11

Box


Like mroncetwice, I too realize that this is an old question; however, I came to this thread looking for an answer when I saw his. I decided to improve upon it (in terms of suiting my own situation) and am sharing the results with the hope that it may also help others.


Turn wpautop on or off by default and list any exceptions:

/**
 * Allow or remove wpautop based on criteria
 */
function conditional_wpautop($content) {
    // true  = wpautop is  ON  unless any exceptions are met
    // false = wpautop is  OFF unless any exceptions are met
    $wpautop_on_by_default = true;

    // List exceptions here (each exception should either return true or false)
    $exceptions = array(
        is_page_template('page-example-template.php'),
        is_page('example-page'),
    );

    // Checks to see if any exceptions are met // Returns true or false
    $exception_is_met = in_array(true, $exceptions);

    // Returns the content
    if ($wpautop_on_by_default==$exception_is_met) {
        remove_filter('the_content','wpautop');
        return $content;
    } else {
        return $content;
    }
}
add_filter('the_content', 'conditional_wpautop', 9);
like image 33
Leon Williams Avatar answered Nov 15 '22 11:11

Leon Williams