Wordpress get plugin directory

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

    My solution has been to use inside plugin_dir_path DIR

    plugin_dir_path( __DIR__ ) . $path_inside_plugin_folder;
    

    The above should give you the absolute path to your plugin folder on the server, then you can load your variable with any file within your plugin folder

    0 讨论(0)
  • 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/";
    
    0 讨论(0)
提交回复
热议问题