Subscribe via

Widget Bugs or Features in WordPress 2.5?


This might be useful for WordPress widget developers out there. I wasn’t going to post this before, but I just found that another developer ran into the exact same issue. So hopefully the 10 minutes I spend writing this post can help save somebody hours of debug.

After WordPress 2.5 released I found that my NowThen Photo Display widget broke the sidebar management page in WordPress adminitration. I took the longest time to figure out why, but the root of the problem was that the new WordPress 2.5 actually executes widget code in the wp-admin area. I have no idea why this is the case. Is this a bug or a feature of the new 2.5 code? I can see it as a feature in a sense that, if the code is not able to executed properly in the wp-admin area, then it should not be added to the front-end. If that’s the case, I would expect an error message rather than a broken sidebar management page.

Anyhow, here are a couple of tips that I have if your plugin breaks after the upgrade:

  1. If your widget relies on some kind of path (e.g. wp-content/plugins/your-plugin/) that is based on get_last_dir(dirname(__FILE__)) then that might cause a problem. When the widget gets executed in the backend, your last directory is actually wp-admin/, not wp-content/plugins/
  2. If your widget have nested functions then calls to the lowest level function will not work. For example, if your code looks like:
    function widget_init() {
      function foo() {
        // Declaring bar() here will not make this function accessible in 2.5
        function bar() {
          echo "Hello World";
        }
        // Calling bar() here will cause a function not found error
        bar();
      }
    }

    Then you must change it to look like:

    function widget_init() {
      function bar() {
        echo "Hello World";
      }
      function foo() {
        bar();
      }
    }

Hope this helps. I will most likely submit this bug to WordPress.org forums. Do you have any more development tips on WordPress 2.5 widgets compatibility? Do you have a broken widget that you can’t find a fix for? Feel free to send me a shout below!

4 Responses to “Widget Bugs or Features in WordPress 2.5?”

[go to last comment]
  1. Rosyidi

    I think
    http://www.erik-rasmussen.com/blog/2006/11/30/widgetize-anything/
    can help us to make some plugin work on widget.
    But I still do’nt know how to make some widget plugin convert to without widget.
    For example : http://subscribe2.wordpress.com/2006/09/19/sidebar-without-a-widget/
    do you know how what must I do to make it work without widget?
    I’ve submit
    $content = apply_filters(‘the_content’, ”);
    echo $content;
    to sidebar.php but it doesn’t work 🙁
    thanks a lot.

  2. Ty Bone

    @Rosyidi
    Are you using the latest Subscribe2? I was looking at the subscribe2widget.php code and I see:

    $content = apply_filters('the_content', '<p><!--subscribe2--></p>');

    instead of:

    $content = apply_filters('the_content', '<p><!-subscribe2-></p>');

    which is what is shown on that link you gave me. Maybe you can try using the first version to see if it work?

  3. Rosyidi

    Yes, I use the latest Subscribe2.
    Thanks a lot for your help.

  4. Ty Bone

    @Rosyidi
    Did the solution work out for you?

[go to first comment]

Leave a Reply