If Widget – Visibility control for Widgets

Description

Control what widgets your site’s visitors see, based on visibility rules. Here are a few examples:

  • Display a widget only if current User is logged in
  • Hide widgets if Device is mobile
  • Display widgets for Admins and Editors
  • Hide Login or Register widgets for Logged in Users

The plugin is easy to use, each widget will have a new option “Show widget only if” which will enable the selection of rules (example in Screenshots)

Features

  • Basic set of visibility rules
    • User state User is logged in
    • User roles Admin Editor Author etc
    • Page type Front page Blog page
    • Post type Post Page Product etc
    • Visitor device Is Mobile
    • Current URL contains or ends with word your-product
  • Multiple rules – mix multiple rules for a widget visibility
    • show if User is logged in AND Device is mobile
    • show if User is Admin AND Is Front page
  • Support for adding custom visibility rules

Example of adding a new visibility rule is described in the FAQ section

Screenshots

  • Enable of visibility rules for Widgets
  • Visibility rules

FAQ

How can I enable custom visiblity for a widget?

On Widgets editing page, each widget will have a section for controlling visibility. Enable the option “Show widget only if” to reveal and configure visibility rules (Example in screenshots).

How can I add a custom visibility rule for menu items?

New rules can be added by any other plugin or theme.

Example of adding a new custom rule for displaying/hiding a widget when current page is a custom-post-type.

// theme's functions.php or plugin file
add_filter('if_visibility_rules', 'my_new_visibility_rule');

function my_new_visibility_rule($rules) {

  $rules['single-my-custom-post-type'] = array(
    'name'      =>  __('Single my-CPT', 'i18n-domain'),     // name of the condition
    'callback'  =>  function() {                            // callback - must return Boolean
      return is_singular('my-custom-post-type');
    }
  );

  return $rules;
}
Where can I find conditional functions?

WordPress provides a lot of functions which can be used to create custom rules for almost any combination that a theme/plugin developer can think of.

Contributors & Developers

“If Widget – Visibility control for Widgets” is open source software. The following people have contributed to this plugin.

Contributors

Changelog

0.2 – 2 March 2019

  • Updated – Plugin texts
  • Updated – Compatibility with WordPress 5.1

0.1

  • Plugin release. Includes basic visibility rules