WordPress 2.2's – Link Widget Validation Errors
WordPress has really come a long way since I first started using it back when it was version 1.0. It now has a nice clean admin interface (although I am currently using the tiger style admin), a clean and simple installation script, and my new favorites, dynamic sidebars and widgets. However, I noticed that the default links widget creates invalid XHTML markup. After a few minutes of looking through the XHTML and widgets.php file, I realized that the culprit was in fact a core wordpress bug. Currently, wordpress creates widgets with the following code:
'before_widget' => '<li id="%1$s" class="widget %2$s">',
This has the nasty effect of creating widget wrappers according to their widget name due to the “%1$s”, which isn’t a deal until you actually have multiple instances of the same widget; in my case, the links widget. Having figured out the problem, I began searching for solutions online and stumbled upon the actual WordPress bug report in trac. The proposed solution was to add the following line of code before the call to wp_list_bookmarks() in wp_widget_links():
$before_widget = preg_replace('/id="[^"]*"/','id="%id"', $before_widget);
Unfortunately, this solution wasn’t good enough for me because I didn’t want to have to remember this in case the next WordPress release didn’t fix the bug. So, after some quick thinking I came up with the following bit of code that can be added to any theme’s function.php file (I think, I am a bit new to WordPress widgets, in fact, this will be my first).
Edit: This code is actually the exact copy of the widget_links code that ships with wordpress in the widget.php file. All I did was add the previous fix and moved it into functions.php to create a custom widget.
function widget_my_links($args) { global $wp_db_version; extract($args, EXTR_SKIP); if ( $wp_db_version < 3582 ) { // This ONLY works with li/h2 sidebars. get_links_list(); } else { $before_widget = preg_replace('/id="[^"]*"/','id="%id"', $before_widget); wp_list_bookmarks(array( 'title_before' => $before_title, 'title_after' => $after_title, 'category_before' => $before_widget, 'category_after' => $after_widget, 'show_images' => true, 'class' => 'linkcat widget' )); } } if ( function_exists('register_sidebar_widget') ) register_sidebar_widget(__('My Links'), 'widget_my_links');
This should add a custom widget called ‘My Links’ when you view the Presentation->Widgets menu. Replace your current Links Widget with this one, and you should be set to go. No more invalid markup, at least not from the links widget.
Tags: Wordpress
Hi Andrew,
Thank you so much for creating such a great workaround for this WordPress bug. I was having the same problem and your solution worked perfectly!
Np,
I am just glad you found it useful. It is quite an annoying bug for something that seems so simple. Hopefully, the WordPress team creates a real fix in the next release.
Great workaround, thanks!
thx for workaround!
Thank you man!
Hi Andrew,
thanks a lot for your fix which really works well.
It’s a pity that this bug is still unfixed in the WP 2.5 release…
UNIMED Medical Service Group
Мы разрабатываем индивидуальные программы обследования, лечения, а также последующей реабилитации, необходимой в ряде случаев, для пациентов, приезжающих к нам во всех сторон света. Наше кредо: индивидуальный подход к каждому кокретному случаю, учет возраста и состояния обратившегося к нам пациента из-за границы. С этой целью мы собираем заочный консилиум для выяснения целесообразности проведения лечения, на котором обсуждается история болезни пациента с учетом всех деталей, вырабатывается программа сответствующего лечения. После консилиума мы передаем всю необходимую информацию в руки обратившегося к нам пациента, включая предварительную стоимость предстоящего лечения. Следует отметить, что заочный консилиум проводится бесплатно. UNIMED Medical Service Group – это налаженная система оказания помощи. На протяжении всего периода пребывания пациента в Израиле мы обеспечиваем его сответствующим сопровождением, включающим в себя быстрое решение всех организационных вопросов.
Usually I do not learn post on blogs, however I would like to say that this write-up very compelled me to check out and do it! Your writing style has been surprised me. Thank you, quite great article.