Wordpress get plugin directory

前端 未结 14 1015
广开言路
广开言路 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:51

    Unfortunately, most of the answers here seem to forget about one important thing.

    In addition to the plugins dir, plugins might be also in the Must-use plugins (mu-plugins) directory

    Because of that, we need to check multiple directories.
    An example function to do this:

    function get_plugin_dir_path($pluginFolderName){
    
        if ( defined( 'WPMU_PLUGIN_DIR' ) && file_exists( trailingslashit( WPMU_PLUGIN_DIR ) . $pluginFolderName ) ) {
            return trailingslashit( WPMU_PLUGIN_DIR ) . $pluginFolderName;
        } elseif ( defined( 'WP_PLUGIN_DIR' ) && file_exists( trailingslashit( WP_PLUGIN_DIR ) . $pluginFolderName ) ) {
            return trailingslashit( WP_PLUGIN_DIR ) . $pluginFolderName;
        }
        return false;
    
    }
    

提交回复
热议问题