Idea: Idea: adding condition on showing text widget

  1. Ad Lagendijk

    Sidebars and their widgets are very useful. By default, people
    using a sidebar will have it visible always (or never). However, it could be very useful to make the display of a widget conditional. For instance when your (single) page shows a post with category "cats" you might want to show the text widget that shows links to cats sites, or even to show a widget with advertisements of cat-food companies. (I hate this Google advertisement sidebars). I have implemented this extension in my WordPress.

    First I will explain what the admin sees when she introduces a new text widget. She will see an extra textbox (called "Condition") where she can put php code that should evaluate to either TRUE or to FALSE. Possible inputs include "TRUE;", "FALSE;", and "is_home();". Dependent on the outcome the widget will be shown or completely hidden (not from the admin panel of course, where it is always visible). The implementation is not very difficult, and I will get into some details in the end.

    Is this addition useful? Should it be implemented? The good thing is that it introduces new functionality (I think without breaking legacy code) that bloggers might like (a lot). I love it. I have also implemented this extension for the Samsarin-php widget (which is a plugin).

    The problem is that people that do not want to use it, are confronted with a more difficult interface with a widget as simple as a text widget. So the extension might deter users. The second thing is that new functionality might (will) introduce new bugs. In addition its use is not without problems. If the php code in the condition has a php-error in it, the admin-user will be confronted with a not very informative, fatal php parsing error.

    Open Source
    I like WordPress a lot. And I understand that many contributors are just volunteers. But contributing to WordPress development is not simple. I have a C++ background and developers are much more disciplined there. I guess the defense by php-developes for using tabs rather than spaces, and using far too few comments is that it would slow down (html) page rendering. But we know from C++ that code clarity and readibility and maintainability are always far more important than speed.

    How am I know what the function wp_filter_post_kses() does without diving deeply in the wp code. One comment in the widget-text function would have helped me a lot. I can give many of those examples.

    Now I will give you my implementation. I do not know what all wp functions do, so my code might break security, or introduce some nasty bugs. I expect the code to be OK, as it mainly involves duplication of existing code. Anyway here it comes:

    In the file widgets.php in the wp-includes directory I made the folling changes:

    1. In the function wp_widget_text_control(), in addition to $title and $text I added the (string) variable $condition with the default value "TRUE;". A number (4 to 5, or so) changes (extensions) in the function have to be made. They are pretty obvious as one can copy the code for $text.
    2. In the function wp_widget_text_control(), I added a textarea, with an appropriate size (1 row, 20 cols) where the user can add a value for $condition.
    3. In the function wp_widget_text_control() I added three heading, to the input box and to the two textarea boxes, to make clear to the admin user what she can input there.
    4. In the function wp_widget_text I added some code like:
      `// if user-condition is evaluated by php to evaluate
      // to "FALSE" widget will be hidden
      // the first "if" ensures we *have* a condition
      if ($condition) {
      eval("\$flShowWidget = $condition;");
      // you never know whether or not this
      // function will be called from the admin page
      if ($flShowWidget && !is_admin() ) {
    Posted: 10 years ago #
  2. Jen

    There are plugins that allow you associate widgets with specific pages, categories, posts or views.

    Posted: 9 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.

  • Rating

    0 Votes
  • Status

    This is plugin territory