Oneupweb : Using filters and shortcode in WordPress posts

When developing for WordPress you may come across a situation where you need to hook custom content into a post entry field. Due to the nature of WordPress’s posts and how they are handled, dropping in PHP into the post edit window is not an option – and is not an ideal solution for most end users anyway. Instead, it is more efficient to create custom filters in your functions.php file and use shortcode to insert the content.

In order to accomplish this, you must first bind a filter to whichever template tag you are using to call the content onto the page.

add_filter('the_content', 'shortcode_filter');

Once you have your filter added, you can then determine the type of shortcode you will need to have your filter search for and replace. Personally, I prefer to wrap my shortcode in comments so that if my function breaks or the template gets switched, any visitor to the site will not be able to see the code unless they view the page source.

For this example, I’ll be using

<!--[tag]name1:param1|name2:param2-->

as the format for my tagging. The final code finds and replaces the code within the comment tag with an iframed form that uses query string parameters passed before the pipe to determine what kind of form it should return. The parameters passed after the pipe are used to pass parameters such as “width” and “height” into the iframe tag.

function act_form_iframe( $content ){
	global $post;
	$last = 0;
	if(($a=strpos($content,'<!--[tag]'))!==false ){
		$p_offset = 0;
		$part_content = substr( $content, 0, $a-$last );
		$p_open  = findlast($part_content,'

'); $p_close = findlast($part_content,'

'); $p_offset = ($p_close < $p_open || ($p_open!==false && $p_close===false) ) ? $p_open : $a; while( $a !== false ){ $b = strpos($content,'-->',$a); $newcontent .= substr($content,$last,$p_offset-$last); $p_open_after = strpos($content,'

',$b); $p_close_after = strpos($content,'

',$b); $b = ($p_close_after < $p_open_after || ($p_close_after!==false && $p_open_after===false)) ? $p_close_after+1 : $b; $a = strpos($content,'&lt!--[tag]',$b); $last = $b+3; $part_content = substr( $content, $last, $a-$last ); $p_open = findlast($part_content,'

'); $p_close = findlast($part_content,'

'); $p_offset = ($p_close < $p_open) ? $a-(strlen($part_content)-$p_open) : $a; } $newform_a = strpos($content,'&lt!--[tag]={')+13; $newform_b = strpos($content,'}iframe={')-$newform_a; $newform_str = substr($content,$newform_a,$newform_b); $newform_vars = explode('|', $newform_str); foreach($newform_vars as $newform_var){ $pair = explode(':', $newform_var, 2); $newform_array[$pair[0]] = $pair[1]; } $iframe_a = strpos($content,'}iframe={')+9; $iframe_b = strpos($content,'}-->')-$iframe_a; $iframe_str = substr($content,$iframe_a,$iframe_b); $iframe_vars = explode('|', $iframe_str); $iframe_params = ''; foreach($iframe_vars as $iframe_var){ $pair = explode(':', $iframe_var, 2); $iframe_params .= ' '.$pair[0].'="'.$pair[1].'"'; } $newcontent .= '<iframe src="/blog/form/?'.http_build_query($newform_array, '', '&').'"'.$iframe_params.'></iframe>'; $newcontent .= substr($content,$last); return $newcontent; } else { return $content; } } add_filter('the_content', 'act_form_iframe');