get template part - Change the file path on get_template_part via plugin

admin2025-01-07  3

I'm trying to make a plugin that will change the behavior of a theme.

In the theme file I have a get_template_part('libs/templates/user_menu');.

I want to make my plugin "force" the get_template_part to return another slug file (a path to a file in the plugin's folder).

So far this is my code inside the plugin:

function wpse21352_template_part_cb( $slug )
{
    if(slug == 'user_menu') {
         return WP_PLUGIN_URL.'/'.$slug;
    } else {
         return $slug;
    }
}

do_action( "get_template_part_user_menu", 'user_menu' );
add_action( 'wpse21352_template_part_cb', 'get_template_part_user_menu', 10, 1 );

I'm trying to make a plugin that will change the behavior of a theme.

In the theme file I have a get_template_part('libs/templates/user_menu');.

I want to make my plugin "force" the get_template_part to return another slug file (a path to a file in the plugin's folder).

So far this is my code inside the plugin:

function wpse21352_template_part_cb( $slug )
{
    if(slug == 'user_menu') {
         return WP_PLUGIN_URL.'/'.$slug;
    } else {
         return $slug;
    }
}

do_action( "get_template_part_user_menu", 'user_menu' );
add_action( 'wpse21352_template_part_cb', 'get_template_part_user_menu', 10, 1 );
Share Improve this question edited Apr 2, 2014 at 12:26 stealthyninja 1,1301 gold badge15 silver badges21 bronze badges asked Mar 26, 2014 at 15:10 CreremCrerem 1112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Well, I handle this issue by adding some code above get_template_part() function in theme as:

//Theme
/* If any filter set */

if(has_filter('ppr_one_search_item_view'){

     $themePartSlug = apply_filters('ppr_one_search_item_view');
 }
 else{
       $themePartSlug = 'templates/search/place';
 }      
 get_template_part($themePartSlug);

in my plugin:

/* Plugin Code */
function get_template_part_place(){

    // Template file from plugin Directory 

    load_template(PPR1_PLUGIN_PATH.'templates/search_places.php');
}
add_filter( 'ppr_one_search_item_view', 'get_template_part_place');
/* plugin Code End*/

may this will help you

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736259642a604.html

最新回复(0)