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

custom post types - Warning , Use of undefined constant PLUGIN_PATH?

programmeradmin0浏览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?

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?

Share Improve this question asked Aug 2, 2019 at 13:39 user145078user145078 1
  • 1 Use plugin_dir_path() instead of PLUGIN_PATH as mentioned in the commentary. – nmr Commented Aug 2, 2019 at 13:48
Add a comment  | 

1 Answer 1

Reset to default 0

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.

发布评论

评论列表(0)

  1. 暂无评论