Wordpress get plugin directory

前端 未结 14 865
广开言路
广开言路 2021-01-31 02:09

Is there any function that would return the full path of my plugin in WordPress?

Example is

path/wp-contents/plugins/myplugin

I have tr

14条回答
  •  星月不相逢
    2021-01-31 02:44

    I would suggest to use a WordPress internal constant to solve this case:

    $my_plugin = WP_PLUGIN_DIR . '/my-plugin';
    
    if ( is_dir( $my_plugin ) ) {
        // plugin directory found!
    }
    

    Alternative

    The other valid alternative is to compute the path from the URL which is more complex/confusing. I would not use this code:

    $plugins_url = plugins_url();
    $base_url = get_option( 'siteurl' );
    $plugins_dir = str_replace( $base_url, ABSPATH, $plugins_url );
    // Now $plugins_dir is same as the WP_PLUGIN_DIR constant.
    
    $my_plugin = $plugins_dir . '/my-plugin';
    

    My opinion in this case is: Use the constant WP_PLUGIN_DIR

提交回复
热议问题