Wordpress get plugin directory

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

    As noted on the section Common Usage of Plugins Url function reference page, that's what worked for me:

    If you are using the plugins_url() function in a file that is nested inside a subdirectory of your plugin directory, you should use PHP's dirname() function:

    echo plugins_url('', dirname(__FILE__) );
    

    The output for this was:

    http://domain/app/wp-content/plugins/my-plugin
    

    The file that called the function was my functions.php inside includes, so the complete path to my file was:

    http://domain/app/wp-content/plugins/my-plugin/includes/functions.php
    
    0 讨论(0)
  • 2021-01-31 02:51

    plugin_dir_path( __FILE__ ) will give you plugin path of current file.

    this is mean if you call this function like that inside "your_plugin_dir/sub_dir/file.php" will return "your_plugin_dir/sub_dir/"

    if you want to get the ROOT of your plugin directory, just change __FILE__ to __DIR__

    plugin_dir_path( __DIR__ )
    
    0 讨论(0)
  • 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;
    
    }
    
    0 讨论(0)
  • 2021-01-31 02:53

    You can define a variable into your main Php file. It should be at the root of your plugin folder. The file should be here : .../wp-content/plugins/plugin-folder/my-plugin.php

    You can add into the file this line.

        define( 'MYPLUGIN__PLUGIN_DIR_PATH', plugins_url('', __FILE__ ) );
    

    After you can use your new variable anywhere into your plugin.

        public function Test() 
        {   
                $folder2 = MYPLUGIN__PLUGIN_DIR_PATH . '/folder1/folder2/';
               // $folder2 = .../wp-content/plugins/plugin-folder/folder1/folder2/
        }
    

    I hope it will help someone.

    0 讨论(0)
  • 2021-01-31 02:56

    I will suggest following code. if you are accessing this function from any subfolder.

    plugins_url( 'images/logo.png' , dirname(__FILE__ ));
    
    0 讨论(0)
  • 2021-01-31 02:56

    As mentioned by @tsl143 the function plugins_url() is what you want, along with the __FILE__ magic constant. If you call it from a file inside your plugin folder it will refer to that folder.

    Example:

    For the directory:

    echo plugins_url( '' , __FILE__ ); 
    //outputs: http://www.example.com/wp-content/plugins/my-plugin    
    

    For a file or subdirectory in your plugin directory:

    echo plugins_url( 'images/logo.png' , __FILE__ ); 
    //outputs: http://www.example.com/wp-content/plugins/my-plugin/images/logo.png    
    
    0 讨论(0)
提交回复
热议问题