Posted By

Internet-Uzver on 03/05/13


Tagged

wordpress sidebar widget


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

ashloren
zemonte


wordpress | disable sidebars, widgets on page template


 / Published in: PHP
 

Disable (remove) some sidebars, or widgets at all in Wordpress on page template with suffix in the template name (*-no-widgets.php, *-no-sidebars.php)

  1. <?php
  2.  
  3. /* Disables widget areas. */
  4. add_filter( 'sidebars_widgets', 'remove_sidebars' );
  5.  
  6. function remove_sidebars( $sidebars_widgets ) {
  7. global $wp_query;
  8.  
  9. if ( is_singular() ) {
  10. $template = get_post_meta( $wp_query->post->ID, "_wp_{$wp_query->post->post_type}_template", true );
  11.  
  12. // Disables all widgets
  13. if ( preg_match("/(-no-widgets.php)$/i", $template ) )
  14. $sidebars_widgets = array( true );
  15.  
  16. // Disables 'primary' & 'secondary' sidebars widgets
  17. if ( preg_match("/(-no-sidebars.php)$/i", $template ) )
  18. $sidebars_widgets['primary'] = false;
  19. $sidebars_widgets['secondary'] = false;
  20. }
  21.  
  22. return $sidebars_widgets;
  23. }
  24.  
  25. ?>

Report this snippet  

You need to login to post a comment.