Trouble with blocks in non-template php page

Hi Dan,
Apologies for the delayed reply.

Pulse will automatically handle the replacement of plugin short codes for you if (and only if) the item is a child of that include code we looked at earlier;

<?php echo $parsedown->text($content); ?>

Any page, block or blog post that gets displayed through this tag will be fine. Outside of this if we pull in our content from a regular PHP include (of example) then any short codes normally wouldn't be processed and your {{gal:gal name}} tags will simply show up on the page.

To get around this you can use a modified version of PHP's own include function that swaps out the short codes when the item is included.

Copy and paste the code at the end of this post into a plain text file called tpinclude.php (for example) and save it in the root of your site (next to the index.php file) and add the following include to your layout.php file;

<?php include_once("tpinclude.php"); ?>

You can then use the this custom function in your template to pull in and replace the short code tags;

<?php tpinclude("content/blocks/common/footer-address.txt"); ?>

I hope this helps.
Tim.

<?php

//tpinclude - include blocks outside of the main Pulse page area and still process Pulse tags


function tpinclude($tpblock){

	$tpblockcontent     = file_get_contents($tpblock);
		
	if (preg_match_all("/".'(\\{)'.'(\\{)'.'.*?'.'(\\})'.'(\\})'."/", $tpblockcontent, $m)) {   
		
		foreach ($m[0] as $get_embed1) {       
			$get_embed = $get_embed1;
			$get_embed = str_replace("{", "" ,$get_embed); 
			$get_embed = str_replace("}", "" ,$get_embed);  
				  
			if (substr_count($get_embed, ':') >=1 ) {                        
				$exp = explode(':', $get_embed); 
				$vars = array_slice($exp, 1); 
				$tag_var1 = (!empty($vars[0])) ? $vars[0] : '';
				$tag_var2 = (!empty($vars[1])) ? $vars[1] : '';
				$tag_var3 = (!empty($vars[2])) ? $vars[2] : '';
				$get_embed = $exp[0];
			}
			
			ob_start(); 
			if($get_embed == 'template'){ $new_template = $tag_var1; }
			else { include("inc/tags/$get_embed.php");
			$new  = ob_get_contents();}
			$tpblockcontent = str_replace($get_embed1, $new, $tpblockcontent);
			ob_end_clean();
			
			
		}
	}
	
	echo($tpblockcontent);
	unset($tpblock,$tpblockcontent);
}

?>
1 Like