wordpress | disable sidebars, widgets on page template


/ Published in: PHP
Save to your folder(s)

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)


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.