Typically when you copy HTML from other sites with complex URIs in the src or href values, the text widgets won’t validate. It is possible to manually update your html, but this can be very tedious, and if you aren’t perfect, your code will break. I recently ran into this with a client and started to fix the code, then realized, what if the client ever changed the code? It would be broken again.
Instead, I came up with this super simple solution. Just add this to your theme functions.php file tocorrectly format the src and html urls in your text widgets
add_filter( 'widget_text', 'ntg_clean_text_widget_output', 15 ); /** * Formats Text Widget src and href to make valid xhtml * * @author Nick the Geek * @url https://designsbynickthegeek.com/?p=761 * @param string $text * @return string */ function ntg_clean_text_widget_output( $text ){ //$text = htmlspecialchars( $text ); $text = preg_replace_callback('/(src=")([^"]*)/mi', 'ntg_format_src_encode', $text); $text = preg_replace_callback('/(href=")([^"]*)/mi', 'ntg_format_src_encode', $text); return $text; } /** * preg_replace_callback() call back to format valid url strings * * @author Nick the Geek * @url https://designsbynickthegeek.com/?p=761 * @param array $matches * @return string */ function ntg_format_src_encode ($matches) { # get rid of full matching string; all parts are already covered array_shift($matches); //print_r($matches); $matches[1] = htmlspecialchars( $matches[1] ); return implode($matches); }
Jan Hemmingsen says
Not bad, only does it not play well with php in widgets. I have enabled php in widgets and call the home url to name one.
When I do that it seems to add the php after url and thus returns a 404 error when I click the link: http://www.example.com/blog/page-1/%3C?php%20bloginfo('url'%20);%20?%3E
I cannot show you the site, but you surely know the code to enable php as I found it in the Genesis forum 🙂
I think this script needs something excluding php from being read or something similar…
nickthegeek says
Yes it would need something to exclude php, I’ve not tried it with php in the widget as I typically use a php widget to do that. If you are enabling php in the widget, then you can just apply the code directly in your php, which is more efficient. This is really only useful when you can’t apply php directly.
http://www.youtube.com/ says
As I know and met individuals who are suffering from psoriasis so I searched
about this illness.