iamtgc

How to Fix or Remove Uncooperative Widgets in WordPress

December 3rd, 2008 by tgc

Recently, I was working with php-code-widget to include some custom PHP code in a WordPress widget.

Unfortunately, and much to my surprise, the custom code did not work flawlessly the first time and I was left staring at errors like:

“Fatal error: Call to undefined function test(); in wp-content/plugins/php-code-widget /execphp.php(37): eval()’d code on line 1″.

Unfortunately, as this error shows up in the dashboard where I would normally edit the widgets content, this is not as straight forward to fix as one would hope. So… after diving into the database and some trial and error I found that the widgets reside in the wp_options table, as this table is quite large, and contains more than just widget information, I had to narrow down what I was looking for. I ended up with this query:

mysql> select option_name from wp_options where option_name like 'widget_%'; +------------------------+ | option_name | +------------------------+ | widget_akismet | | widget_archives | | widget_calendar | | widget_categories | | widget_execphp | | widget_meta | | widget_pages | | widget_recent_comments | | widget_recent_entries | | widget_rss | | widget_tag_cloud | | widget_text | +------------------------+

In this case, it is content in the php-code-widget that is causing our errors, as there is no widget_php-code-widget, it seems that the widget_execphp is the next closest thing. So let’s inspect the widget_execphp entry.

mysql> select * from wp_options where option_name='widget_execphp'; +-----------+---------+----------------+-----------------------------------------------------------------------------------+----------+ | option_id | blog_id | option_name | option_value | autoload | +-----------+---------+----------------+-----------------------------------------------------------------------------------+----------+ | 347 | 0 | widget_execphp | a:1:{i:291302011;a:2:{s:5:"title";s:4:"Test";s:4:"text";s:16:"<?php test();?>";}} | yes | +-----------+---------+----------------+-----------------------------------------------------------------------------------+----------+

Bingo… here we can see the code that was entered into the widget. In this case the test() function does not exist.

The first, and arguably the simplest option is to simply delete the entry, we delete it using the option_id value we obtained above. The option_id value is also used later in the article, in the event you chose to update the entry as opposed to removing it.
mysql> delete from wp_options where option_id=347; Query OK, 1 row affected (0.29 sec)

At this point you can simply go back into the dashboard and recreate your widget, more careful not to include non existent functions this time. But that would be too easy, a more interesting alternative is to modify the existing entry.

mysql> update wp_options set option_value = "a:1:{i:291302011;a:2:{s:5:\"title\";s:4:\"Test\";s:4:\"text\";s:20:\"<?php new_test(); ?>\";}}" where option_id=347; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0

Let’s look at this more closely, the above query updates option_value to:

a:1:{i:291302011;a:2:{s:5:”title”;s:4:”Test”;s:4:”text”;s:20:”<?php new_test(); ?>”;}}

If you notice, two things have changed from the previous option_value value.

The first change is to the string length variable, represented by s, which precedes the string. In our example s:16 becomes s:20. More simply put <?php test(); ?> (16 characters in length) becomes <?php new_test(); ?> (20 characters in length).

The necessary changes is to the string itself, and probably the reason you are getting the undesirable behavior in the first place, is left up to the reader. It is my hope that this sets you in the right direction.

Posted in MySQL, Wordpress

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.