Wordpress get plugin directory

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

    Here is a solution, when you are not inside the plugin root:

    As of now with 4.7.5, WordPress does not have a get_plugins_root() function like there is a get_theme_root() function. This is probably because you really shouldn't ever need to modify plugins from your theme, and the plugins root directory never changes.

    However, it can be useful if you need to programmatically affect plug-ins while developing themes.

    Simply, as WP does for the theme root:

    $plugin_root = WP_CONTENT_DIR . '/plugins';
    

    Or, if you need a function, why not just do it the same way WordPress does it for the theme root?

    Just make a copy of the function get_theme_root() from wp-includes/theme.php and paste it into your theme's functions.php file, rename the function to get_plugins_root(), simplify it, and change 'theme' to 'plugins' in the function...

    get_plugins_root() {
    
        $plugins_root = WP_CONTENT_DIR . '/plugins';
    
        /**
         * Filters the absolute path to the plugins directory.
         *
         * @since now
         *
         * @param string $plugins_root Absolute path to plugins directory.
         */
        return apply_filters( 'plugins_root', $plugins_root );
    }
    

    With the path, you can now add the plug-ins folder name that you wish to affect.

    $the_plugin_root = get_plugins_root()."/the-plugin-name/";
    

提交回复
热议问题