Skip to content

Instantly share code, notes, and snippets.

@joelworsham
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelworsham/fa9853653d6d95f29e12 to your computer and use it in GitHub Desktop.
Save joelworsham/fa9853653d6d95f29e12 to your computer and use it in GitHub Desktop.
How to dynamically hide a widget.Jaxon WP Meetup rules
<?php
// Don't change anything on this line EXCEPT for "hide_widget_pages". You can change that if you want, but you don't
// have to. But if you do, be sure to change it...
add_filter( 'widget_display_callback', 'hide_widget_pages', 10, 3 );
// ... here as well! And don't worry about changing anything else in this line.
function hide_widget_pages( $instance, $widget, $args ) {
// This is where you would enter the widget ID that you want to hide. Get this by using the Chrome inspector
// to find the "id" attribute that the widget exists in (though don't use the dash and number, like "widget-3"
// would be "widget".
if ( $widget->id_base == 'pages' ) {
// This is where you change what page you want to have the widget display on. You can use the page title,
// the slug (url), or the ID.
// If you remove the exclamation mark, it will do the opposite, meaning it will NOT show on this specific
// page.
if ( ! is_page( 'Contact' ) ) {
return false;
}
}
}
@joelworsham
Copy link
Author

If you need more explanation, please email me! :)

joelworsham@gmail.com

@joelworsham
Copy link
Author

FYI: Currently this is hiding the WordPress core widget "Pages" (shows pages from your site) on any page that is NOT "Contact".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment