0

I want to create a theme specific plugin for the opern-sourced Bootstrap Genesis theme where I can use the add_theme_support function to add or remove several mini plugins or theme modules/features that I have in a plugin subfolder. I found this question here, but could not figure it out based on the accepted answer.


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');


Then in my functions.php I could enable these plugin features/addons by adding theme support, by using each addons filename:

<?php
/* functions.php */
add_theme_support('themename-addon1.php');
add_theme_support('themename-addon1.php');
add_theme_support('themename-addon1.php');

However, this doesn't work and I can't seem to figure out what I'm missing... When I remove the theme support check it works fine:

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

Figured it out after playing with Rarsts answer 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

All addons should be added in a subfolder called addons with a unique name for each file.


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

add_theme_support('bsg-foo');

Where the above example theme support name "bsg-" + "foo" where "foo" is one of the filenames basename you created in the addons folder: 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.