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.

I'm trying to run disable auto p filter on only my home page. In my functions.php file, I added this snippet:

function removeP(){
    if ( is_page( 4 ) ) {
        remove_filter('the_content', 'wpautop');
    }
}

add_action( 'after_setup_theme', 'removeP' );

It does not remove the filter on the homepage.

share|improve this question
2  
by homepage I'm assuming you mean Home Page of a website, not a blog. So you can modify the answers below by changing is_home() to is_front_page() - which will look for the template front-page.php and modify it accordingly. –  Howdy_McGee Mar 24 at 17:26
    
never hardcode post IDs, if you must, use post slugs instead. –  Tom J Nowell Mar 24 at 17:28

2 Answers 2

You're calling it on the wrong hook, and you're calling the wrong conditional function to test.

When the after_setup_theme hook is fired, WordPress hasn't finished processing wether you're on a page or a homepage yet.

You should also never pass in hardcoded post IDs. In this case is_home works, as might is_front_page. If you really must reference a hardcoded posts, and I beg that you don't, use a post slug instead, or even better, store a post ID in an option and present a drop down menu to let the user choose in the backend.

Your function should be something like this:

function removeP(){
    if ( is_home () ) {
        remove_filter('the_content', 'wpautop');
    }
}

add_action( 'template_redirect', 'removeP' );
share|improve this answer
    
Wouldn't using a post slug be worse than using a static post ID? The user can change the post slug, but to change the post ID they would have to delete the page entirely. –  Howdy_McGee Mar 24 at 17:35
    
No, a post ID could change because of an import export, or any other number of reasons. I would suggest that neither are used. In a post slug situation at least the slug can be edited back. I would personally opt instead to have a post ID stored in an option with a GUI –  Tom J Nowell Mar 24 at 17:37
    
@TomJNowell you are right. Didn't think about the hook. Bad me –  Pieter Goosen Mar 24 at 17:40

change if ( is_page( 4 )) to if (is_home() ) so:

function removeP(){
    if ( is_home() ) {
        remove_filter('the_content', 'wpautop');
    }
}

add_action( 'after_setup_theme', 'removeP' );
share|improve this answer
    
Not working. Actually, home page get content from other pages. But I already try to inform te id of this pages. –  marcelo2605 Mar 24 at 17:18
    
You're going to have to give us a little bit more info then on what you're trying to do. the is_home() conditional is how you run functions only on the home template. –  Zach Russell Mar 24 at 17:21
    
Russel Found a solution: I add remove_filter('the_content', 'wpautop'); on the top of home page template. –  marcelo2605 Mar 24 at 17:28

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.