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

1 Answer 1

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.org/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;
}
3
  • I changed the code but nothing is changed:( I have edit my question with the code that I used Apr 13, 2020 at 17:39
  • make sure orders.php is not throwing any fatal error, see debug log
    – Maidul
    Apr 13, 2020 at 19:40
  • No I have only a simple echo inside the template Apr 13, 2020 at 20:12

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.