Designs By Nick the Geek

Genesis Based WordPress Development and Tutorials for Everyone

  • Email
  • GitHub
  • Twitter
  • Home
  • About
    • My Experience As…
    • Bedtime Stories by Me
  • Themes
  • Genesis Explained
    • Actions
    • Filters
    • Functions
    • Admin
  • Plugins
  • Themes
    • Free
      • Fluid
  • Tutorials
    • Quick Tips
nickthegeek February 6, 2012

Format text widgets for valid urls

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);
}

If you liked this article, tell someone about it

Previous Article

Social Menu Icons Using Sprites

Next Article

Creating Page Templates in Genesis

Related Posts

  • Conditional Loop Actions
  • Creating Page Templates in Genesis
  • Social Menu Icons Using Sprites
  • Custom Post Types Made Easy
  • genesis related postsShow Related Posts with Thumbnails in Genesis

Filed Under: Tutorials Tagged: Quick Tips

Comments

  1. Jan Hemmingsen says

    February 7, 2012 at 9:43 am

    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

      February 16, 2012 at 12:36 pm

      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.

  2. http://www.youtube.com/ says

    July 26, 2013 at 6:36 pm

    As I know and met individuals who are suffering from psoriasis so I searched
    about this illness.

Copyright © 2025 Nick the Geek ·Built on the Genesis Theme Framework using the Streamline 2.0 theme · Log in