WordPress.org

Plugin Directory

Widget Logic

Widget Logic lets you control on which pages widgets appear using WP's conditional tags. It also adds a 'widget_content' filter.

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 in
  • is_category(array(5,9,10,11)) -- category page of one of the given category IDs
  • is_single() && in_category('baked-goods') -- single post that's in the category with this slug
  • current_user_can('level_10') -- admin only widget
  • strpos($_SERVER['HTTP_REFERER'], "google.com")!=false -- widget to show when clicked through from a google search
  • is_category() && in_array($cat, get_term_children( 5, 'category')) -- category page that's a descendent of category 5
  • global $post; return (in_array(77,get_post_ancestors($post))); -- WP page that is a child of page 77
  • global $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.

The 'widget_content' filter

When this option is active (tick the option tickbox at the foot of the widget admin page) you can modify the text displayed by ANY widget from your own theme's functions.php file. Hook into the filter with:

add_filter('widget_content', 'your_filter_function', [priority], 2);

where [priority] is the optional priority parameter for the add_filter function. The filter function can take a second parameter (if you provde that last parameter '2') like this:

function your_filter_function($content='', $widget_id='')

The second parameter ($widget_id) can be used to target specific widgets if needed.

A WordPress filter function 'takes as input the unmodified data, and returns modified data' which means that widget_content filters are provided with the raw HTML output by the widget, and you are then free to return something else entirely:

Example filters

add_filter('widget_content', 'basic_widget_content_filter');
function basic_widget_content_filter($content='')
{   return $content."<PRE>THIS APPEARS AFTER EVERY WIDGET</PRE>";
}

I was motivated to make this filter in order to render all widget titles with the excellent ttftitles plugin like this:

add_filter('widget_content', 'ttftext_widget_title');
function ttftext_widget_title($content='')
{   preg_match("/<h2[^>]*>([^<]+)/",$content, $matches);
    $heading=$matches[1];
    $insert_img=the_ttftext( $heading, false );
    $content=preg_replace("/(<h2[^>]*>)[^<]+/","$1$insert_img",$content,1);
    return $content;
}

People often ask for a way to give widgets alternating styles. This filter inserts widget_style_a/widget_style_b into the class="widget ..." text usually found in a widget's main definition:

add_filter('widget_content', 'make_alternating_widget_styles');
function make_alternating_widget_styles($content='')
{   global $wl_make_alt_ws;
    $wl_make_alt_ws=($wl_make_alt_ws=="style_a")?"style_b":"style_a";
    return preg_replace('/(class="widget )/', "$1 widget_${wl_make_alt_ws} ", $content);
}

Requires: 2.8 or higher
Compatible up to: 4.1.13
Last Updated: 2 years ago
Active Installs: 300,000+

Ratings

4.9 out of 5 stars

Support

0 of 5 support threads in the last two months have been marked resolved.

Got something to say? Need help?

Compatibility

+
=
Not enough data

0 people say it works.
0 people say it's broken.

100,1,1 100,2,2
100,1,1 100,6,6
100,1,1 100,9,9
100,8,8
100,1,1 0,1,0 0,1,0 78,18,14 100,4,4
86,14,12 100,36,36 100,1,1 0,1,0
87,31,27 0,1,0
96,49,47
100,1,1
100,7,7
100,11,11 0,1,0
100,3,3 100,1,1 0,1,0
95,37,35 100,7,7
100,8,8
100,12,12
100,18,18
100,2,2
100,6,6
95,58,55 100,1,1 100,1,1
100,7,7 0,1,0 91,11,10 100,1,1
96,53,51 100,6,6
100,4,4 97,29,28 0,1,0
100,6,6
89,38,34 0,6,0
93,28,26 100,1,1 100,1,1
60,5,3 20,5,1 100,5,5 69,13,9
92,36,33 100,1,1
100,3,3
83,6,5
100,7,7 100,1,1
100,6,6 67,3,2
70,10,7
88,16,14
100,1,1
67,3,2
100,8,8
100,5,5
89,9,8
100,8,8
100,3,3
75,4,3
50,4,2
100,5,5
100,1,1
83,6,5
100,2,2
67,3,2
100,7,7
100,1,1
100,4,4
100,2,2
100,3,3
50,2,1