• Resolved FeliceAntonio

    (@antonio_09)


    Hi to everyone.
    I’m looking for remove header in specific page with PHP.
    I found this code and this too but it doesn’t work.
    In child theme’s functions.php file I tried these solutions, without results:

    add_action( 'after_setup_theme','tu_remove_header' );
    function tu_remove_header() {
    if ( is_page( 123 ) ) {
        remove_action( 'generate_header','generate_construct_header' );
    }}

    and this

    if ( is_page( 123 ) ){
        remove_action( 'generate_header','generate_construct_header' );
    }

    I can’t understand where I’m wrong.
    For the moment I use this CSS code by Leo.
    Thanks!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hi Felice,

    The first code seems about right.

    One reason I can think of why it isn’t working is likely because the condition isn’t correct.

    Perhaps, the ID indicated in the is_page() isn’t correct.

    If you can point me to the page you’re trying to remove the header on, I can help out in pointing you to the correct ID.

    Thread Starter FeliceAntonio

    (@antonio_09)

    Hi @ejcabquina thanks for your time.
    The page I’m working is this, and the ID is 6.

    // remove header
    add_action( 'after_setup_theme','tu_remove_header' );
    function tu_remove_header() {
    if ( is_page( 6 ) ) {
        remove_action( 'generate_header','generate_construct_header' );
    }}
    • This reply was modified 3 years, 5 months ago by FeliceAntonio. Reason: added code
    Thread Starter FeliceAntonio

    (@antonio_09)

    Well, I found in this website (Step 3: Insert the Code into functions.php) a good solution, so I can work just on functions.php file of child theme, without touching custom CSS.
    Practically, I add <style>.site-header, .site-footer{display:none;}</style> in page where remove header and footer.
    Always Thanks for your time, and GP Team too.
    See you soon!

    • This reply was modified 3 years, 5 months ago by FeliceAntonio.

    Have you fully sorted it out? Thanks for sharing that with us.

    Reminder: Removing the header through PHP will always be better than CSS because there are potential site performance improvements.

    Thread Starter FeliceAntonio

    (@antonio_09)

    I agree. I always try, when possible, to solve with PHP, but in this case I did not understand why the code I used did not work.

    I think it’s because you’re using the wrong hook.

    I don’t think the is_page() functions can be used on after_theme_setup.

    What happens if you use this?

    add_action( 'wp','tu_remove_header' );
    function tu_remove_header() {
    	if( is_page(819) ){
    		remove_action( 'generate_header', 'generate_construct_header');
    	}
    }
    Thread Starter FeliceAntonio

    (@antonio_09)

    Thanks a lot, it works like a charm!!!
    I didn’t think, and I didn’t know, about is_page () could not work well with after_theme_setup.
    Always thanks for your time and the great support -:)

    I wasn’t sure at first as well. I had to check back if the query is run before or after the after_theme_setup.

    is_page() wasn’t working because after_theme_setup is run before the query so the is_page() was always returning FALSE.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Remove header with PHP function’ is closed to new replies.