使用插件添加_主题_支持

时间:2015-10-27 作者:Bryan Willis

我想为源代码的Opren创建一个特定于主题的插件Bootstrap Genesis 主题,我可以使用add_theme_support 函数可以添加或删除插件子文件夹中的几个迷你插件或主题模块/功能。我发现this question here, 但根据公认的答案,我们无法找到答案。


So the plugin structure would look like this:

themeplugin/
├── addons/
│   ├── addon1.php
│   ├── addon2.php
│   └── addon3.php
└── themeplugin.php

The main plugin file, lets call it themeplugin.php would be this:

<?php
/*
Plugin Name:       My Theme Name Addons
Plugin URI:        https://example.com
Description:       A collection of mini plugins/addons to apply specific changes.
Version:           1.0
Author:            Author Name
Author URI:        https://example.com/author
License:           MIT License
License URI:       http://opensource.org/licenses/MIT
*/

function load_themename_addons() {
  foreach (glob(__DIR__ . \'/addons/*.php\') as $file) {
    if (current_theme_supports(\'themename-\' . basename($file, \'.php\'))) {
      require_once $file;
    }
  }
}
add_action(\'after_setup_theme\', \'load_themename_addons\');
然后在我的functions.php 我可以通过添加主题支持、使用每个插件文件名来启用这些插件功能/插件:

<?php
/* functions.php */
add_theme_support(\'themename-addon1.php\');
add_theme_support(\'themename-addon1.php\');
add_theme_support(\'themename-addon1.php\');
然而,这不起作用,我似乎无法找出我错过了什么。。。当我删除主题支持时,请检查它是否正常工作:

function load_themename_addons() {
  foreach (glob(__DIR__ . \'/addons/*.php\') as $file) {
      require_once $file;
  }
}
add_action(\'after_setup_theme\', \'load_themename_addons\');

1 个回复
最合适的回答,由SO网友:Bryan Willis 整理而成

玩过之后就明白了Rarsts 答复here.

Here is what I ended up with:

 <?php
    /*
    Plugin Name:       Bootstrap Genesis Addons
    Plugin URI:        https://github.com/bryanwillis/bootstrap-genesis-addons
    Description:       A collection of mini plugins/addons to apply specific changes to my site.
    Version:           1.0
    Author:            Bryan Willis
    Author URI:        https://github.com/bryanwillis/
    License:           MIT License
    License URI:       http://opensource.org/licenses/MIT
    */


function bootstrap_genesis_addons() {
  global $_wp_theme_features;
  foreach (glob(__DIR__ . \'/addons/*.php\') as $file) {
    $feature = \'bsg-\' . basename($file, \'.php\');
    if (isset($_wp_theme_features[$feature])) {
      require_once $file;
    }
  }
}
add_action(\'after_setup_theme\', \'bootstrap_genesis_addons\', 100);


The theme plugin should follow this file structure:

bsg-addons/
├── addons/
│   ├── foo.php
│   ├── bar.php
│   └── baz.php
└── bsg-addons.php
所有加载项都应添加到名为“加载项”的子文件夹中,每个文件都有一个唯一的名称


Last add theme support the same way you would anything else:

add_theme_support(\'bsg-foo\');
其中,上面的示例主题支持名称“bsg-”+“foo”,其中“foo”是您在addons文件夹中创建的文件名basename之一:bsg-addons/addons/foo.php


It seems to work great and is a lot cleaner in my opinion than adding a bunch of unnecessary data to the database to perform the activations / deactivations of modules or theme specific features like custom post types. It also works for adding entire plugins directly into your theme.

相关推荐