WordPress.org

Ready to get started?Download WordPress

Forums

Widget Logic
[resolved] php scripting help with widget_content (2 posts)

  1. chicagoave
    Member
    Posted 3 years ago #

    Hello,

    I am using widget logic both for conditionally displaying my widgets and also for the widget_content feature in working with ttftitles. Your example is great for replacing a single instance of content between the h2 tag in a widget but what if I want to replaces all instances where h2 or in my case a appears? Looks like I would use preg_match_all or str_replace but I'm not good enough in php to write the code. After several trials and errors asking if someone can help me out.

    Here is what I'm working from, based on the provided example, but not sure how to correctly change preg_match to preg_match_all (?) or str_replace (?) or place the entire function inside a loop (?) to get a replace all going.

    Thanks in advance!

    add_filter('widget_content', 'ttftext_widget_title');
    
    function ttftext_widget_title($content='')
    
    {   preg_match("/<a[^>]*>([^<]+)/",$content, $matches);
        $navitem=$matches[1];
        $insert_img=the_ttftext( $navitem, false, nav );
        $content=preg_replace("/(<a[^>]*>)[^<]+/","$1$insert_img",$content,1);
        return $content;
    }
    
  2. chicagoave
    Member
    Posted 3 years ago #

    solved it with help from a friend. if anyone is interested.

    add_filter('widget_content', 'ttftext_widget_title');
    
    function ttftext_widget_title($content='')
    
    {   return preg_replace("/(<a[^>]*>)([^<]+)/e", "\"$1\".the_ttftext(\"$2\", false, nav)", $content);
    }

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic