最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - wc_get_template new template does not showing up

programmeradmin3浏览0评论

I use this code in in my plugin file. The content from the original orders.php file is gone that means the filter does work but my own file does not showing up.

I added this code in the plugin main file

<?php
define("PLUGIN_DIR_PATH", plugin_dir_path(__FILE__));
?>

and this code below in the plugin functions file.

The functions.php is located in pluginfolder/required/functions.php

 <?php
add_filter( 'wc_get_template', 'q343_get_template', 10, 5 );
function q343_get_template( $located, $template_name,     $args, $template_path, $default_path ) {    
if ( 'myaccount/orders.php' == $template_name ) {
    $located = PLUGIN_DIR_PATH . 'required/templates/orders.php';
}

return $located;
}
?>

The template that I will load is located in pluginfolder/required/templates/orders.php

I use this code in in my plugin file. The content from the original orders.php file is gone that means the filter does work but my own file does not showing up.

I added this code in the plugin main file

<?php
define("PLUGIN_DIR_PATH", plugin_dir_path(__FILE__));
?>

and this code below in the plugin functions file.

The functions.php is located in pluginfolder/required/functions.php

 <?php
add_filter( 'wc_get_template', 'q343_get_template', 10, 5 );
function q343_get_template( $located, $template_name,     $args, $template_path, $default_path ) {    
if ( 'myaccount/orders.php' == $template_name ) {
    $located = PLUGIN_DIR_PATH . 'required/templates/orders.php';
}

return $located;
}
?>

The template that I will load is located in pluginfolder/required/templates/orders.php

Share Improve this question edited Apr 13, 2020 at 17:37 Loek Lemmens asked Apr 13, 2020 at 11:51 Loek LemmensLoek Lemmens 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

plugin_dir_path( FILE ) will return the current directory. Not sure where you call this q343_get_template() function if it's on a subdirectory then plugin_dir_path( FILE ) is returning that subdirectory path.

From WordPress Code Reference https://developer.wordpress/reference/functions/plugin_dir_path/

The “plugin” part of the name is misleading – it can be used for any file, and will not return the directory of a plugin unless you call it within a file in the plugin’s base directory.

The safest way is to use define on your root plugin file.

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

Then call the function like this

add_filter( 'wc_get_template', 'q343_get_template', 10, 5 );
function q343_get_template( $located, $template_name, $args, $template_path, $default_path ) {    
    if ( 'myaccount/orders.php' == $template_name ) {
        $located = MY_PLUGIN_PATH . 'required/templates/orders.php';
    }

    return $located;
}
发布评论

评论列表(0)

  1. 暂无评论