• I have a filter applied to the_content that looks for ‘{qname}’ in a post and blows it up in to a restaurant name, ratings, review and link to blog post – you can see an example here.

    When I let that filter run on my category pages, which use the_excerpt, the texts are also replaced, which seems to me to be wrong. Moreover, the the <b> and the <img…> for the rating icons are lost in the process (perhaps because of the tag stripping function of the_excerpt?

    I tried adding a filter for the_excerpt with higher priority but that did not have an impact.

    But when I deleted the filter for the_content then the filter for the the_excerpt did work perfectly, leaving the bold and images in the output. That makes sense because even though the_excerpt strips tags, I’m running the filter on the results of the_excerpt so it should be possible to add html tags at that stage and not lose them.

    But it seems to be the presence of the the_content filter that is the problem, even though it should be ignored given that there is no call to the_content() on this page.

    Can anyone advise?

    HB

Viewing 5 replies - 1 through 5 (of 5 total)
  • Can you post your filter please? Wrap the code it backticks or use the ‘code’ button above.

    Thread Starter hotbelgo

    (@hotbelgo)

    I found a work around in the meantime which is to have a plugin that reads add_filter(‘the_content’,’convertqName’) and then to remove_filter it and add_filter(‘the_excerpt’,’convertqName’) in the specific categories page. That seems to work (I presume that the plugin is called every time a new page is loaded so removing the filter only applies on that page).

    Why I can’t have a plugin that reads:

    add_filter('the_content','convertqName');
    add_filter('the_excerpt','convertqName');

    remains a mystery to me though.
    The function called by the plugin is below.

    BTW: the $filterContext variable was the only way I could find to pass a parameter to the function – another cludge and advice on that would be nice too… ??
    HB

    function convertqName($beginString) {
    	//return $beginString;
    	global $wpdb;
    	global $filterContext;
    
    	$endString = $beginString;
    
    	// search for qNames by matching {, some characters excluding }, }
    	$howMany = preg_match_all("|\{([^\}]+)\}|", $beginString, $matches, PREG_PATTERN_ORDER);
    
    	for ($i = 0; $i < $howMany; $i++) {
    		//matches[0][*] contains entire match, matches[1][*] contains match of ([^\}]+)
    		//i.e. this strips of the {}
    		//$query = "SELECT resto_name, resto_link, resto_address FROM restaurants WHERE resto_qName = {$matches[1][$i]}";
    		$query = "SELECT * FROM restaurants WHERE resto_qName = '{$matches[1][$i]}'";
    		$resto = $wpdb->get_results($query);
    		//echo($query . " result: " . $resto[0]->resto_link . "<br />");
    
    		switch ($filterContext) {
    		case "posts":   //single
    			$newString = "Restaurant: <b>" . $resto[0]->resto_name . "</b> (" . $resto[0]->resto_cuisine . ") <img src='" . get_bloginfo('url') . "/af/map/" . $resto[0]->resto_rating . "stars.png'>&nbsp; &nbsp; <img src=" . get_bloginfo('home') . "/af/map/" . $resto[0]->resto_price . "euro.png><br /><a href='" . get_bloginfo('home') . "/map/#" . $resto[0]->resto_qName . "'>" . $resto[0]->resto_address . "</a>, " . $resto[0]->resto_tel;
    			if ($resto[0]->resto_area != "") {$newString .= " (" . $resto[0]->resto_area . ")<br />";}
    			break;
    		case "page":     //recommendations
    			$newString = "<b>" . $resto[0]->resto_name . "</b> (" . $resto[0]->resto_cuisine . ") <img src='" . get_bloginfo('home') . "/af/map/" . $resto[0]->resto_rating . "stars.png'>&nbsp; &nbsp; <img src=" . get_bloginfo('home') . "/af/map/" . $resto[0]->resto_price . "euro.png><br />" . $resto[0]->resto_comment;
    			if ($resto[0]->resto_link != "none") {$newString .= " <a href='" . $resto[0]->resto_link . "'>Review</a>";}
    			$newString .= "<br /><a href='" . get_bloginfo('home') . "/map/#" . $resto[0]->resto_qName . "'>" . $resto[0]->resto_address . "</a>, " . $resto[0]->resto_tel;
    			if ($resto[0]->resto_area != "") {$newString .= " (" . $resto[0]->resto_area . ")";}
    			break;
    		case "iphone":
    			$newString = "<div class='cell'><h3>" . $resto[0]->resto_name . "</h3><span>" . $resto[0]->resto_comment . "</span><a href='https://maps.google.com/maps?f=q&hl=en&geocode=&q=" . $resto[0]->resto_name . "@" . $resto[0]->resto_lat . "," . $resto[0]->resto_lng . "&ie=UTF8&t=m&z=17&iwloc=addr'><div class='link'></div></a></div>";
    			break;
    		case "rotm":
    			$newString = "<a href='" . $resto[0]->resto_link . "'>" . $resto[0]->resto_name . "</a>";
    			break;
    		case "search":
    			$newString = "Restaurant: <b>" . $resto[0]->resto_name . "</b> <img src='" . get_bloginfo('url') . "/af/map/" . $resto[0]->resto_rating . "stars.png'>&nbsp; <img src=" . get_bloginfo('home') . "/af/map/" . $resto[0]->resto_price . "euro.png>";
    			break;
    		}
    		$endString = str_replace( $matches[0][$i], $newString, $endString);
        }
      return $endString;
    }

    Maybe what you want to be using
    are short codes.

    Thread Starter hotbelgo

    (@hotbelgo)

    Wow, thx – had never seen those before, although the help page is enough to put any amateur off.

    I did notice though that the codex page says that shortcodes only work in the_content. Perhaps that includes the_excerpt like it seems to with filters?

    Shortcodes are relatively new– circa 2.5 according to the codex.

    … the help page is enough to put any amateur off.

    I need to stand up for WP here. Its true that you need to have some understanding of PHP and WordPress to make sense of the codex but overall the docs are fabulous. Head over to openoffice.org and try to work out how to write a OOoBasic macro. Those docs will make your head spin.

    Shortcodes aren’t supposed to work in the_excerpt() though maybe read this post.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘add_filter wierdness’ is closed to new replies.