Ecommerce Shopify WordPress Discussion

Creating a get_template_part() function like WordPress

I like WordPress function get_template_part() where its basically a PHP file but doesn't use require_once, require, include etc. Basically it returns data to be stored in variable. Every WP function includes another function, its a loop and hard to find how actually it works. I created a small function but I dont know if it is the right way, or it will put a lot of load on system when everything will be using this function to load diffident PHP files as templates: function get_template_part( $template ) { $path = dirname(__DIR__) . DIRECTORY_SEPARATOR .'admin'. DIRECTORY_SEPARATOR .'templates'. DIRECTORY_SEPARATOR; if ( file_exists( $path . $template .'.php' ) ) { ob_start(); require_once $path . $template .'.php'; return ob_get_clean(); } else return "<strong>Template Missing:</strong> ".$template.".php"; } Can someone please check of my above function is good to load all templates in a big system, if not please advise on how does WP does it or if you can please help upgrade my little function to avoid any potential problems. I also created another function, but still not satisfied (this function does not run <?php but only evaluates the variables.. function template($fname) { $tfile = TEMP_DIR."/$fname.tpl"; if(!file_exists($tfile)) return "<br /><b>warning</b> : missing template - $fname<br />"; $data = file_get_contents($tfile); $data = str_replace("\"","\\\"",$data); return $data; } function eval_template($tpl) { $tpl_data = template($tpl); $arr = array(); if( preg_match_all('/\$\w+/', $tpl_data, $matches) ) { $arr = array_unique($matches[0]); foreach($arr as $v) { $v = str_replace('$','',$v); global ${"$v"}; } } }
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.