Widget Logic

Widget Logic lets you control on which pages widgets appear. It uses any of WP’s conditional tags. It also adds a ‘widget_content’ filt

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.

There is also an option to add a wordpress ‘widget_content’ filter — this lets you tweak standard widgets to suit your theme without editing plugins and core code

Altre note:

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. Make good use of WP’s own conditional tags.

If there is no ‘return’ in the text, an implicit ‘return’ is added to the start and a ‘;’ is added on the end.

Try variations on the examples given below. Use ! (NOT) in front of a conditional tag to reverse the logic, eg !is_home() to show a widget on any page except the home page.

Use || (OR), && (AND) to make more complex conditions. 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.

Remember — the code runs even if the widget doesn’t appear. (Even if it never appears!)

Examples:

  • is_home() — just the main blog page
  • !is_page('about') — everywhere EXCEPT this specific WP ‘page’
  • 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_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.

Example filters

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 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); }

Per ulteriori approfondimenti, vedi questo link: Conditional Tags (Wordpess codex)

Rispondi