Support » Fixing WordPress » Having problems with a function

  • I have the following problem:

    I don’t want WP adding wpautop to all pages, but only the ones I need, so I added this:

    function my_wpautop_correction() {
        if( is_page() ) {
            remove_filter( 'the_content', 'wpautop' );
            remove_filter( 'the_excerpt', 'wpautop' );
        }
        if( is_page( array(79, 81) ) ) {
            add_filter( 'the_content', 'wpautop' );
            add_filter( 'the_excerpt', 'wpautop' );
        }
    }
    add_action('pre_get_posts', 'my_wpautop_correction');

    Seems to be working fine, but I am not sure if its the best way to write that function. I’ve tried this:

    function my_wpautop_correction() {
        if( !( is_page(79) || is_page(81) ) ) {
            remove_filter( 'the_content', 'wpautop' );
            remove_filter( 'the_excerpt', 'wpautop' );
        }
    }
    add_action('pre_get_posts', 'my_wpautop_correction');

    But it doesnt work, what I am doing wrong? I want to add wpautop only to pages 79 and 81.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The action hook you are using, pre_get_posts, happens too early.

    From the Codex:

    This hook is called after the query variable object is created, but before the actual query is run.

    Try using init instead.

    Or wp_head maybe?

    function get_rid_of_wpautop(){
      if(!is_single() && !is_page(array(79,81))){
        remove_filter ('the_content', 'wpautop');
        remove_filter ('the_excerpt', 'wpautop');
      }
    }
    
    add_action( 'template_redirect', 'get_rid_of_wpautop' );

    Seems to do the trick.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Having problems with a function’ is closed to new replies.