Widget Logic lets you control on which pages widgets appear using WP's conditional tags.
Évaluations
4.5
Mis à jour récemment
July 3, 2019
Version
5.10.4
Installations actives
100K
Widget Logic

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.

Configuration

Outre la logique par rapport à vos widgets, trois options sont ajoutées au bas de la page d’administration du widget (voir captures d’écran).

  • 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)

    Vous devrez peut-être retarder le chargement si votre logique dépend de fonctions définies, par exemple dans le fichier functions.php du thème. Inversement, vous pouvez souhaiter charger plus tôt pour que le nombre de widgets soit calculé correctement, par exemple pour afficher une présentation ou un contenu alternatif lorsqu’une colonne latérale ne contient aucun 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.

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.

Exemples

  • 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’.

Le filtre “widget_logic_eval_override”

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.

Gratuitsur le plan Creator
En procédant à l’installation, vous acceptez les Conditions d’utilisation de WordPress.com ainsi que les Conditions de l’extension tierce.
Installations actives
100K
Testé jusqu’à version
5.2.20
Cette extension est disponible en téléchargement pour être utilisée sur votre installation WordPress auto-hébergée.