• Hi,

    I made some code to remove the footer widgets on a custom template page I made. The code worked well but removed them on every page. Here’s the code:

    <?php
    add_filter('tc_widgets_footer','my_footer_widget_removal');
    function my_footer_widget_removal($default_widgets_area){
    	if ( ! is_page_template( 'custom-template-page.php' ) )
            return;
        remove_action('__footer', array(TC_init::$instance, 'footer_widgets'), 10);
    }
    ?>

    Would someone know how to limit the removal of the footer widgets ONLY to the custom template page?

    Thanks so much for the help! It’s appreciated!
    Michael

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Mike Sneed

    (@msneed)

    This code works on the breadcrumbs for this page and doesn’t affect the other pages:

    <?php
    add_action ('wp_head' , 'tc_disable_breadcrumb');
    function tc_disable_breadcrumb() {
        if ( ! is_page_template( 'custom-template-page.php' ) )
            return;
        remove_action( '__before_main_container', array( TC_breadcrumb::$instance , 'tc_breadcrumb_display' ), 20 );
    }
    ?>

    I think that there are 2 problems with the above:

    1. You’re using a filter. A filter must always return something. Yet you’re returning nothing. Hence no footer widgets, anywhere.

    However, there’s no reason to use a filter. You’re not changing stuff here, you’re doing stuff. So an add_action is more appropriate than an add_filter.

    2. You need to make sure that you remove the action before WordPress renders the html. Best to hook the action high up on the wp_head hook.

    So the code becomes:

    add_action('wp_head','my_footer_widget_removal');
    function my_footer_widget_removal() {
    	if ( ! is_page_template( 'custom-template-page.php' ) )
    		return;
    	remove_action('__footer', array(TC_init::$instance, 'footer_widgets'), 10);
    }
    Thread Starter Mike Sneed

    (@msneed)

    I’ve been getting a lot better at the functions and hooks after I read the article on hooks written by you. This time I think I was a little confused.. so it seems clearer now to use an action..

    That said.. unfortunately I got all the widgets back but also on the custom template page that I didn’t want them on..

    Any thoughts?

    Thanks EF!

    Ha! I was so busy being smart about the filter and action stuff that I didn’t check the rest. That’ll teach me ??

    Are you sure you’re using is_page_template() correctly? The codex say that it probably won’t work in the loop and the file path has to be right.

    Alternatively, you could simply put the action/function in the page template itself. That would allow you to strip any conditionals, because you know you’re in the template by virtue of it running—if it’s not running, you’re not in it. I’m not sure if it’s considered bad form or anything, but it sounds like a good idea to me (a non-programmer).

    Glad you like the article ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Remove Footer widgets but not on custom template’ is closed to new replies.