add_theme_support using a plugin

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.

Leave a Comment