Widgets Archives - Plugin Republic https://pluginrepublic.com/category/widgets/ WooCommerce Plugins Fri, 27 Nov 2020 13:13:57 +0000 en-US hourly 1 https://pluginrepublic.com/wp-content/uploads/2020/10/cropped-favicon-1-32x32.png Widgets Archives - Plugin Republic https://pluginrepublic.com/category/widgets/ 32 32 Prevent widget from displaying programmatically https://pluginrepublic.com/prevent-widget-displaying-programmatically/ https://pluginrepublic.com/prevent-widget-displaying-programmatically/#respond Thu, 13 Dec 2018 17:13:35 +0000 https://pluginrepublic.com/?p=33101 Choose who can see which widgets

The post Prevent widget from displaying programmatically appeared first on Plugin Republic.

]]>
While developing the WooCommerce Members Only plugin, I wanted a way to ensure that widgets were only available for certain users. You can do this using the widget_display_callback filter – just return false to prevent the widget content from being rendered.

View this code snippet on GitHub.

The post Prevent widget from displaying programmatically appeared first on Plugin Republic.

]]>
https://pluginrepublic.com/prevent-widget-displaying-programmatically/feed/ 0
How to display a widget on specific post types https://pluginrepublic.com/how-to-display-a-widget-on-specific-post-types/ https://pluginrepublic.com/how-to-display-a-widget-on-specific-post-types/#respond Thu, 07 Jan 2016 11:20:12 +0000 https://pluginrepublic.com/?p=117 Conditionally display widgets depending on the post type

The post How to display a widget on specific post types appeared first on Plugin Republic.

]]>
This is a pretty simple method for ensuring your custom widget only displays on specific post types. You can find guidance on creating new widgets at https://codex.wordpress.org/Widgets_API.

To ensure your widget only displays for specified post types, update the widget function with the following snippet:

public function widget( $args, $instance ) {
  if ( 'artist' != get_post_type() ) { // Change this post type as required
    return;
  }
  // Output the content of the widget here
}

All this does is check the post type of the current page then bails before any content is output if the post type doesn’t match the required post type. You can add the rest of the function after.

It’s very simple.

The post How to display a widget on specific post types appeared first on Plugin Republic.

]]>
https://pluginrepublic.com/how-to-display-a-widget-on-specific-post-types/feed/ 0