6

This is default get_template_part function inside WordPress:

function get_template_part( $slug, $name = null ) {
    do_action( "get_template_part_{$slug}", $slug, $name );

    $templates = array();
    if ( isset($name) )
        $templates[] = "{$slug}-{$name}.php";

    $templates[] = "{$slug}.php";

    locate_template($templates, true, false);
}

I am trying to use that action for locating custom post type loop file from plugin:

add_action( "get_template_part_templates/loop", function($slug, $name){
    if ("example" == $name){
        if (!locate_template("templates/loop-{$name}.php", false, false)){
            /* What do you suggest to do here? */
        }
    }   
},10,2 );

I need a solution that;

  1. Check if theme have files for "example" custom post type
  2. If dont have; use plugin's template files for showing and dont use theme's default solution

Update: this is the code that calling template part in theme:

global $post;
get_template_part( 'templates/loop', $post->post_type );
2
  • And what have you tried so far?
    – vancoder
    Apr 2, 2013 at 22:45
  • lots of.. main problem was i couldnt stop get_template_part function after action. So loop was showing double loop for same post Apr 2, 2013 at 22:54

2 Answers 2

2

/**
*Extend WP Core get_template_part() function to load files from the within Plugin directory defined by PLUGIN_DIR_PATH constant
* * Load the page to be displayed 
* from within plugin files directory only 
* * @uses mec_locate_admin_menu_template() function 
* * @param $slug * @param null $name 
*/ 

function mec_get_admin_menu_page($slug, $name = null) {

do_action("mec_get_admin_menu_page_{$slug}", $slug, $name);

$templates = array();
if (isset($name))
    $templates[] = "{$slug}-{$name}.php";

$templates[] = "{$slug}.php";

mec_locate_admin_menu_template($templates, true, false);
}

/* Extend locate_template from WP Core 
* Define a location of your plugin file dir to a constant in this case = PLUGIN_DIR_PATH 
* Note: PLUGIN_DIR_PATH - can be any folder/subdirectory within your plugin files 
*/ 

function mec_locate_admin_menu_template($template_names, $load = false, $require_once = true ) 
{ 
$located = ''; 
foreach ( (array) $template_names as $template_name ) { 
if ( !$template_name ) continue; 

/* search file within the PLUGIN_DIR_PATH only */ 
if ( file_exists(PLUGIN_DIR_PATH . '/' . $template_name)) { 
$located = PLUGIN_DIR_PATH . '/' . $template_name; 
break; 
} 
}

if ( $load && '' != $located )
    load_template( $located, $require_once );

return $located;
}

Then use the mec_get_admin_menu_page($slug, $name = null); function anywhere in your plugin files like the get_template_part($slug, $name = null) function.

mec_get_admin_menu_page('custom-page','one'); 

The above sample function will look for custom-page-one.php file inside your PLUGIN_DIR_PATH and loads it.

Also I recommend you use:

define('PLUGIN_DIR_PATH', plugin_dir_path(__FILE__));

to define your Plugin Directory Path.

3
  • 1
    Please do not define such a constant. Keep the global namespace clean, assign the value to a protected value of your plugin controller.
    – fuxia
    Aug 8, 2013 at 8:15
  • This is not theme overriding. Basically its not even wordpress way to modify a wordpress function. Aug 8, 2013 at 11:29
  • 1
    Small hint to see on this usable class, include all functions for the requirement.
    – bueltge
    May 2, 2014 at 22:04
0

You Need to hook into the template_include filter e.g.

add_filter('template_include', 'my_function_name');
function my_function_name( $template ) {
 if ("example" == $name){
  $template = dirname( __FILE__ ) . '/my-template.php';
 }
 return $template;
}

I asked this on here a couple of years ago and have used it a few times since for projects :)

1
  • Hi Gareth, I was looking for something else, but while scanning through I noticed that $name is undefined in your example. May 21, 2020 at 1:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.