Subscribe via

Widget Bugs or Features in WordPress 2.5?

Thaya Kareeson


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!

Save and Share
StumbleUpon
Reddit

Currently experiencing problems with Intense Debate commenting system. Please make sure your browser's Javascript is enabled and try again later.