0

I having a warning in my error log

Use of undefined constant PLUGIN_PATH - assumed 'PLUGIN_PATH' (this will throw an Error in a future version of PHP)

When I googled it I couldn't find the answer but seems a lot of websites are showing up this warning publically?

add_filter('single_template', 'my_custom_template');

function my_custom_template($single) {

    global $post;

    /* Checks for single template by post type */
    if ( $post->post_type == 'POST TYPE NAME' ) {
        if ( file_exists( PLUGIN_PATH . '/Custom_File.php' ) ) {
            return PLUGIN_PATH . '/Custom_File.php';
        }
    }

    return $single;

}

This was the code that I used: Custom Post Type Templates from Plugin Folder?

any fix?

  • 1
    Use plugin_dir_path() instead of PLUGIN_PATH as mentioned in the commentary. – nmr Aug 2 at 13:48
1

The error is self explanatory: PLUGIN_PATH is not defined anywhere.

It is not one of WordPress' default constants (which are listed here, and all start with WP_). In the context of the code you've copied, it's apparent that it was either supposed to be replaced with the path to your plugin (in which case using a constant in the example code was a bad idea), or it used to exist (the answer is 8 years old). This is mentioned in the comments on your own link.

To get the path to a plugin file (these days at least), you need to use plugin_dir_path():

if ( file_exists( plugin_dir_path( __FILE__ ) . 'Custom_File.php' ) ) {
    return plugin_dir_path( __FILE__ ) . 'Custom_File.php';
}

Just be aware that plugin_dir_path( __FILE__ ) returns the path to the current file, so if the file containing this code is in a subfolder of your plugin you need to account for that.

  • Thanks you. But PLUGIN_PATH seems to work though it gives the warning? – Latheesh V M Villa Aug 2 at 13:57
  • 1
    If it’s throwing that specific error then it’s impossible for it to work unless your file is inside a directory literally called /PLUGIN_PATH/. Are you sure you don’t have an identical or similar template in your theme? – Jacob Peattie Aug 2 at 14:48
  • Sorry. The problem was with subfolder. The file was inside another folder. The check resulted in bool false so it was returning to the default – Latheesh V M Villa Aug 2 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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