Widget Logic Wordpress Plugin - Rating, Reviews, Demo & Download
Plugin Description
This plugin gives every widget an extra control field called “Widget logic” that lets you control the pages that the widget will appear on. The text field lets you use WP’s Conditional Tags, or any general PHP code.
PLEASE NOTE The widget logic you introduce is EVAL’d directly. Anyone who has access to edit widget appearance will have the right to add any code, including malicious and possibly destructive functions. There is an optional filter ‘widget_logic_eval_override’ which you can use to bypass the EVAL with your own code if needed. (See Other Notes).
The configuring and options are in the usual widget admin interface.
BIG UPDATE:
-
Now you can control widget in Gutenberg Widgets editor as well as in Classic Editor. It is just as easy as before but also in gutenberg view.
-
Pre-installed widgets let you add special widget with one click of the mouse. First pre-installed widget is Live Match that let you add widget of one random live football game with real time score updates (teams logos, livescore, minute of the match, tournament name). And more interesting widgets to come!
Configuration
Aside from logic against your widgets, there are three options added to the foot of the widget admin page (see screenshots).
-
Use ‘wp_reset_query’ fix — Many features of WP, as well as the many themes and plugins out there, can mess with the conditional tags, such that is_home is NOT true on the home page. This can often be fixed with a quick wp_reset_query() statement just before the widgets are called, and this option puts that in for you rather than having to resort to code editing
-
Load logic — This option allows you to set the point in the page load at which your widget logic if first checked. Pre v.50 it was when the ‘wp_head’ trigger happened, ie during the creation of the HTML’s HEAD block. Many themes didn’t call wp_head, which was a problem. From v.50 it happens, by default, as early as possible, which is as soon as the plugin loads. You can now specify these ‘late load’ points (in chronological order):
- after the theme loads (after_setup_theme trigger)
- when all PHP loaded (wp_loaded trigger)
- after query variables set (parse_query) – this is the default
- during page header (wp_head trigger)
You may need to delay the load if your logic depends on functions defined, eg in the theme functions.php file. Conversely you may want the load early so that the widget count is calculated correctly, eg to show an alternative layour or content when a sidebar has no widgets.
-
Don’t cache widget logic results — From v .58 the widget logic code should only execute once, but that might cause unexpected results with some themes, so this option is here to turn that behaviour off. (The truth/false of the code will be evaluated every time the sidebars_widgets filter is called.
Interaction with External Services
Widget Logic uses the external service to obtain up-to-date information about the results of football matches. widgetlogic.org is a source of sports information, that provides a wide range of information about football, including various leagues, tournaments, and championships from around the world.
The functioning of the widgetlogic.org service is based on delivering real-time data about selected matches without the need to refresh the page. This means that data is automatically updated without requiring page reload. This approach ensures users quick and uninterrupted access to the latest sports data without the effort of manually updating information, allowing them to stay informed about ongoing events in real-time.
Writing Logic Code
The text in the ‘Widget logic’ field can be full PHP code and should return ‘true’ when you need the widget to appear. If there is no ‘return’ in the text, an implicit ‘return’ is added to the start and a ‘;’ is added on the end. (This is just to make single statements like is_home() more convenient.)
The Basics
Make good use of WP’s own conditional tags. You can vary and combine code using:
!
(NOT) to reverse the logic, eg!is_home()
is TRUE when this is NOT the home page.||
(OR) to combine conditions.X OR Y
is TRUE when either X is true or Y is true.&&
(AND) to make conditions more specific.X AND Y
is TRUE when both X is true and Y is true.
There are lots of great code examples on the WP forums, and on WP sites across the net. But the WP Codex is also full of good examples to adapt, such as Test if post is in a descendent category.
Examples
is_home()
— just the main blog page!is_page('about')
— everywhere EXCEPT this specific WP ‘page’!is_user_logged_in()
— shown when a user is not logged inis_category(array(5,9,10,11))
— category page of one of the given category IDsis_single() && in_category('baked-goods')
— single post that’s in the category with this slugcurrent_user_can('level_10')
— admin only widgetstrpos($_SERVER['HTTP_REFERER'], "google.com")!=false
— widget to show when clicked through from a google searchis_category() && in_array($cat, get_term_children( 5, 'category'))
— category page that’s a descendent of category 5global $post; return (in_array(77,get_post_ancestors($post)));
— WP page that is a child of page 77global $post; return (is_page('home') || ($post->post_parent=="13"));
— home page OR the page that’s a child of page 13
Note the extra ‘;’ on the end where there is an explicit ‘return’.
The ‘widget_logic_eval_override’ filter
Before the Widget Logic code is evaluated for each widget, the text of the Widget Logic code is passed through this filter. If the filter returns a BOOLEAN result, this is used instead to determine if the widget is visible. Return TRUE for visible.
Screenshots
-
The ‘Widget logic’ field at work in Gutenberg widgets.
-
The ‘Widget logic’ field at work in Classic widgets.
-
The plugin options are at the foot of the usual widget admin page…
wp_reset_query
option, ‘load logic point’ and ‘evaluate more than once’. You can also export and import your site’s WL options as a plain text file for a quick backup/restore and to help troubleshoot issues. -
Pre-installed ‘Live Match’ widget in widgets section
-
Pre-installed ‘Live Match’ widget on website