Getbeans / Beans

Beans WordPress Theme Framework. The default branch is set to development, please switch to the master branch for production.
https://www.getbeans.io
Other
391 stars 61 forks source link

Add Configurable Filters #56

Open hellofromtonya opened 7 years ago

hellofromtonya commented 7 years ago

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

}
ibes commented 6 years ago

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?

christophherr commented 6 years ago

@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.