Beans icon indicating copy to clipboard operation
Beans copied to clipboard

Add Configurable Filters

Open hellofromtonya opened this issue 7 years ago • 2 comments

There are various features and components within the framework that loaded or registered as default. For these, hooking them into a filter event allows child themes or plugins to configure what is needed for the specific web page.

Philosophy: Register or load only what's needed for this specific web page. Provide the means for the child theme/plugin to specify what should be loaded.

For Example - Theme Supports

For example, Beans registers a set of default theme support features. Moving those into a configurable filter does the following:

  1. Child themes can add additional theme supports via the hook.
  2. Minimize or eliminate the need to remove_theme_support.

Sample Code

add_action( 'beans_init', 'beans_add_theme_support' );
/**
 * Add theme support.
 *
 * @ignore
 */
function beans_add_theme_support() {

	/**
	 * Configurable filter for adding theme supports.
	 * 
	 * An array of theme support features is passed to each of the registered callbacks,
	 * allowing each to extend or modify the features to be added via
	 * `add_theme_support`.
	 *
	 * @since 1.5.0
	 *
	 * @param array Array of theme supports
	 *              key: is the feature name
	 *              value: is the value for this feature. If none, set to `null`
	 */
	$theme_supports = apply_filters( 'beans_add_theme_supports', array(
		'title-tag'             => null,
		'custom-background'     => null,
		'menus'                 => null,
		'post-thumbnails'       => null,
		'automatic-feed-links'  => null,
		'html5'                 => array( 'comment-list', 'comment-form', 'search-form', 'gallery', 'caption' ),
		'custom-header'         => array(
			'width'       => 2000,
			'height'      => 500,
			'flex-height' => true,
			'flex-width'  => true,
			'header-text' => false,
		),

		// Beans specific.
		'offcanvas-menu'        => null,
		'beans-default-styling' => null,
	) );

	foreach ( $theme_supports as $feature => $value ) {
		add_theme_support( $feature, $value );
	}

}

hellofromtonya avatar Nov 10 '17 02:11 hellofromtonya

So to be used like this:

<?php 
add_filter( 'beans_add_theme_supports', 'my_theme_support');
function my_theme_support( $theme_supports ) {
    $theme_supports['title-tag'] = true;

    return $theme_supports;
}

In your draft - is the custom-header defined right now? Why should that be activated on default?

And the strategy is rather to enable the minimum than enabling the maximum, yes?

ibes avatar Jun 25 '18 22:06 ibes

@ibes Not quite. Theme support will be added for all the items listed in the array.
The keys with the null value, don't take a configuration. (See the DocBlock explanation) If you don't want to include a theme support, you have to either unset it or use your own config array.

christophherr avatar Jun 26 '18 20:06 christophherr