如何:获取主插件/主题文件?

时间:2011-06-13 作者:kaiser

有一些功能get_theme_data(); &;get_plugin_data(); - 两者都使用get_file_data(); 需要一个特定的文件作为$input.

我得到了一组可以由插件或主题使用的类,没有特定的位置。一切正常,但我不知道如何确定main 插件或主题文件是-我指的是包含注释标题的文件,其中包含有关主题/插件的信息(版本、作者等)。

3 个回复
最合适的回答,由SO网友:kaiser 整理而成

如何获取主题或插件的注释标题数据

。。。持有特定类别的。

下面展示了如何在任何插件或主题中放置一个类,并且仍然能够从注释标题中获取任何主题或插件数据。例如,这对于更新设置/数据库选项非常有用。

don\'t 了解:

如果是插件、mu插件或主题,以及容器/根主题或插件文件夹的嵌套深度注释:

mu插件也适用


/**
 * Plugin root
 * @return Full Path to plugin folder
 */
public function set_root_path() 
{
    $_path = trailingslashit( str_replace( basename( __FILE__ ), "", plugin_basename( __FILE__ ) ) );

    // Allow overriding the location
    $_path = apply_filters( __CLASS__.\'_root\', $_path );

    return $this->_path = $_path;
}


/**
 * Gets the data of the base \'theme\' / \'plugin\' / \'wpmuplugin\'
 * Performance: average loading time on a local (not vanilla) install for 1.000 runs: 0.0042 sec.
 * 
 * @param (mixed) $value
 * @return (array) $value | Theme/Plugin comment header data OR false on failure | default: \'Version\'
 */
public function get_data( $value = \'Version\' )
{
    // Class basename - String to Array
    $_path_data = explode( \'/\', $this->_path );
    // Get rid of the last element, as it\'s only a trailing slash
    array_pop( $_path_data );
    // reverse for faster processing
    krsort( $_path_data );

    // Themes basename
    $theme_roots = get_theme_roots();
    // In case some used register_theme_directory(); before
    // Might not work if an additional themes directory will be registered later
    // Thanks to @Thomas Scholz <http://toscho.de> for the hint
    if ( is_array( $theme_roots ) )
    {    
        foreach ( $_path_data as $_path_part )
        {
            foreach( $theme_roots as $root )
            {
                if ( in_array( $root, $_path_data ) )
                    $_theme_root = $root;
            }
        }
    }
    else 
    {
        // Get rid of the leading slash
        $_theme_root = str_replace( \'/\', \'\', $theme_roots );
    }

    // Plugins basename
    $_plugin_root = basename( WP_PLUGIN_DIR );


    # >>>> get file & load data
    $base_file = \'\';
    // Themes
    if ( in_array( $_theme_root, $_path_data ) )
    {
        foreach ( search_theme_directories() as $folder => $data )
        {
            foreach ( $_path_data as $_path_part )
            {
                if ( $_path_part == $folder )
                    $base_file = trailingslashit( $data[\'theme_root\'] ).$data[\'theme_file\'];
            }
        }

        $file_data = get_theme_data( $base_file );
    }
    // Plugins
    elseif( in_array( $_plugin_root, $_path_data ) )
    {
        $plugins = get_plugins();
        foreach ( $plugins as $plugin_file => $plugin_info )
        {
            $data   = explode( \'/\', $plugin_file );
            $file   = $data[1];
            foreach ( $_path_data as $_path_part )
            {
                if ( $_path_part !== $file )
                    $base_file = WP_CONTENT_DIR.$_type.\'/\'.$data[0].\'/\'.$data[1];
            }
        }

        $file_data = get_plugin_data( $base_file );
    }
    // WPMU Plugins
    else
    {
        // MU plugins basename - compatible for older MU too
        // Thanks (again) to @Thomas Scholz <http://toscho.de> for the hint that mu plugins really exists
        $mu_plugin_dir = ! version_compare( $GLOBALS[\'wp_version\'], \'3.0.0\', \'>=\' ) ? MUPLUGINDIR : WPMU_PLUGIN_DIR;
        $_mu_plugin_root = basename( $mu_plugin_dir );

        if ( ! in_array( $_mu_plugin_root, $_path_data ) )
            return false;

        $mu_plugins = get_mu_plugins();
        foreach ( $mu_plugins as $mu_plugin_file => $mu_plugin_info )
        {
            $data   = explode( \'/\', $mu_plugin_file );
            $file   = $data[1];
            foreach ( $_path_data as $_path_part )
            {
                if ( $_path_part !== $file )
                    $base_file = WP_CONTENT_DIR.$_type.\'/\'.$data[0].\'/\'.$data[1];
            }
        }

        $file_data = get_plugin_data( $base_file );
    }
    # <<<< get file & load data


    // return
    if ( ! empty ( $file_data ) )
        return $file_data[ $value ];

    // return false to determine that we couldn\'t load the comment header data
    return false;
}
用法示例:
echo $this->get_data( \'Author\' ); // The Plugin or Theme Author
echo $this->get_data( \'Version\' ); // The Plugin or Theme Version
echo $this->get_data( \'Name\' ); // The Plugin or Theme Name

SO网友:Hameedullah Khan

您可以使用以下内容获取主题的主文件:

$theme_info_file = trailingslashit( get_template_directory() ) . \'style.css\';
要获取插件的主文件,您需要知道插件名称。然后可以使用以下功能:

function get_plugin_file( $plugin_name ) {
    require_once( ABSPATH . \'/wp-admin/includes/plugin.php\' );
    $plugins = get_plugins();
    foreach( $plugins as $plugin_file => $plugin_info ) {
        if ( $plugin_info[\'Name\'] == $plugin_name ) return $plugin_file;
    }
    return null;
}
例如,获取Akismet的插件主文件。

$akismet_plugin_file =  trailingslashit( WP_PLUGIN_DIR ) . get_plugin_file( \'Akismet\' );

SO网友:Max G J Panas

在我最近做的一个项目中,我使用以下内容来获取主插件文件&;我的插件数据来自插件中的另一个文件,无需硬编码:

get_plugins( \'/\' . explode( \'/\', plugin_basename( __FILE__ ) )[0] );
这将返回以下内容:

array (size=1)
  \'main_plugin_file.php\' => 
    array (size=11)
      \'Name\' => string \'Plugin Name\' (length=11)
      \'PluginURI\' => string \'http://wordpress.org/plugins/plugin-folder/\' (length=41)
      \'Version\' => string \'1.0\' (length=3)
      \'Description\' => string \'BlaBlaBla Something awesome.\' (length=148)
      \'Author\' => string \'Max GJ Panas\' (length=12)
      \'AuthorURI\' => string \'http://maxpanas.com\' (length=19)
      \'TextDomain\' => string \'plugin-name\' (length=11)
      \'DomainPath\' => string \'/languages\' (length=10)
      \'Network\' => boolean false
      \'Title\' => string \'Plugin Name\' (length=11)
      \'AuthorName\' => string \'Max GJ Panas\' (length=12)
您可以在插件中的任何文件中使用它,它应该工作正常,您不必担心任何文件被重命名、路径更改等。

获取各种功能使用的“带插件数据的主插件文件路径”,如is_plugin_active()is_plugin_active_for_network(), 从一个文件not 在主插件文件中,使用以下代码:

 $plugin_dir  = explode( \'/\', plugin_basename( __FILE__ ) )[0];
 $plugin_file = array_keys( get_plugins( "/$plugin_dir" ) )[0];

 if ( is_plugin_active_for_network( "$plugin_dir/$plugin_file" ) ) // for example.
   // do stuff...

结束

相关推荐