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

I having a warning in my error logUse of undefined constant PLUGIN_PATH - assumed 'PLUGIN_PATH' (thiswill thro

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745275121a4619993.html

相关推荐

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

    I having a warning in my error logUse of undefined constant PLUGIN_PATH - assumed 'PLUGIN_PATH' (thiswill thro

    19小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信