Add New Page Setting for any Theme Filter (custom per-page topbar content)

The Total theme has been developed in a very modular fashion so that any feature you see can be adjusted conditionally via built-in filters and/or action hooks. In some cases you may want to alter a specific theme feature on a per-page basis and while you can use custom code for that, it may not be ideal for the end user if they are not a developer. In this guide I will show you how you can add a new page setting to the Total Page Settings metabox that you can use to alter the default content in your Top Bar on a per page/post basis.

Adding the New Metabox Setting

Adding new settings to the Total metabox is very easy (snippet guide). Here is the code you would add to your child theme’s functions.php file or custom plugin for adding a new tab and field for altering the Top Bar content.

add_filter( 'wpex_metabox_array', function( $array ) {

    // Create new topbar tab
    $array['custom_tab_topbar'] = array(
        'title'     => __( 'Top Bar', 'wpex' ),
        'post_type' => array( 'page' ), // Remove to display on all pages
        'settings'  => array(
            'topbar_content' => array(
                'title'         => __( 'Top Bar Content', 'total' ),
                'description'   => __( 'Custom topbar content', 'total' ),
                'id'            => 'wpex_topbar_content',
                'type'          => 'textarea',
            ),
        ),
    );

    // Return fields
    return $array;

} );

Result:

Alter The Topbar Content Output

Now that we have our new setting in place the next step is to filter the default top bar content whenever the field is isn't empty via the "wpex_top_bar_content" filter.

add_filter( 'wpex_top_bar_content', function( $content ) {

	// Check if the current page has a value for the new
	// wpex_topbar_content meta field and if so return it for
	// the top bar content
	if ( $meta = get_post_meta( wpex_get_current_post_id(), 'wpex_topbar_content', true ) ) {
		$content = $meta;
	}

	// Return content
	return $content;

} );

And that's it!

Now we have a new field that can be altered on a per-page basis for overriding the default Top Bar content. And you can use this example for any other filter in the theme that may not already have a custom setting for conditionally changing it on a per-page basis.