Get the Official WPSaviour App Now!

BlogTutorials

Theme Customizer Boilerplate – Conditional Options, Child Themes and Plugins

Thus far we’ve seen how straightforward it is to handle theme options using Theme Customizer Boilerplate and its hooks. As you perhaps recall, essential step was hooking into ‘thsp_cbp_options_array’ filter hook and passing it array of decisions you have to use in your theme.

I’m sure you’re already accustomed to WordPress movement and filter hooks — Plugin API — and the way in which they work, nevertheless merely in case, proper right here’s a quick recap (using filter hooks for example). It’s possible you’ll define your personalized carry out and hook it into an current filter using add_filter carry out:

add_filter( $tag, $function_to_add, $priority, $accepted_args );

Let’s consider priority argument. Its default price is 10, so if you happen to do not use one different amount, that’s what your carry out’s execution priority will seemingly be. Lower the amount, earlier your carry out is executed. So must you do one factor like this:

// Together with first message
carry out my_theme_add_first_message( $content material materials ) {
	$content material materials .= '<p>First Message</p>';
	return $content material materials;
}
add_filter( 'the_content', 'my_theme_add_first_message', 1 );

// Together with second message
carry out my_theme_add_second_message( $content material materials ) {
	$content material materials .= '<p>Second Message</p>';
	return $content material materials;
}
add_filter( 'the_content', 'my_theme_add_second_message', 2 );

In case you title the_content carry out in single.php or each different template publish content material materials will seemingly be confirmed, adopted by First Message, adopted by Second Message. Not because of that’s their order on this code snippet, nevertheless as a consequence of execution priority parameter. Think about hooks as within the occasion that they’d been snowballs rolling down the hill choosing all sort of stuff on their method.

How does this apply to Theme Customizer Boilerplate?

It’s possible you’ll hook into ‘thsp_cbp_options_array’ out of your theme’s carry out.php file, using a personalized carry out (e.g. my_theme_options_array) with priority price set to 1. Which suggests each different carry out that hooks into ‘thsp_cbp_options_array’ filter hook will do it AFTER my_theme_options_array carry out you already outlined. Try this occasion:

carry out my_theme_options_array() {
	// Using helper carry out to get default required performance
	$thsp_cbp_capability = thsp_cbp_capability();

	$decisions = array(
		// Half ID
		'my_theme_new_section' => array(

			'existing_section' => false,
			'args' => array(
				'title' => __( 'New Half', 'my_theme_textdomain' ),
				'priority' => 10
			),
			'fields' => array(
				/*
				 * Radio topic
				 */
				'my_radio_button' => array(
					'setting_args' => array(
						'default' => 'option-2',
						'kind' => 'selection',
						'performance' => $thsp_cbp_capability,
						'transport' => 'refresh',
					),					
					'control_args' => array(
						'label' => __( 'My Radio Button', 'my_theme_textdomain' ),
						'kind' => 'radio', // Radio administration
						'choices' => array(
							'option-1' => array(
								'label' => __( 'Alternative 1', 'my_theme_textdomain' )
							),
							'option-2' => array(
								'label' => __( 'Alternative 2', 'my_theme_textdomain' )
							),
							'option-3' => array(
								'label' => __( 'Alternative 3', 'my_theme_textdomain' )
							)
						),					
						'priority' => 3
					)
				)
			)
		)
	);
	
	return $decisions;
}
add_filter( 'thsp_cbp_options_array', 'my_theme_options_array', 1 );

This may increasingly add New Half to Theme Customizer with one topic in it, known as My Radio Button. You then, or one other individual develops a toddler theme in your theme and decides to keep up New Half, nevertheless instead of My Radio Button it’s maybe greater to have My Checkbox. Easy:

carry out my_child_theme_options_array( $decisions ) {
	// Using helper carry out to get default required performance
	$thsp_cbp_capability = thsp_cbp_capability();

	/*
	 * This time, we're solely modifying fields in my_theme_new_section inside the $decisions array
	 */	
	$decisions['my_theme_new_section']['fields'] = array(
		'my_checkbox_field' => array(
			'setting_args' => array(
				'default' => true,
				'kind' => 'selection',
				'performance' => $thsp_cbp_capability,
				'transport' => 'refresh',
			),					
			'control_args' => array(
				'label' => __( 'My Checkbox', 'my_theme_textdomain' ),
				'kind' => 'checkbox', // Checkbox topic administration
				'priority' => 2
			)
		)
	);
	
	return $decisions;
}
add_filter( 'thsp_cbp_options_array', 'my_child_theme_options_array', 2 );

Seen that I didn’t cross $decisions parameter to my_theme_options_array and did it in my_child_theme_options_array carry out? That’s because of after I first hooked into ‘thsp_cbp_options_array’ hook I needed to override Theme Customizer Boilerplate sample decisions. Then, after I hooked into it as soon as extra from my infant theme, I didn’t want to completely delete father or mom theme’s decisions, merely barely edit them. That’s why I’m solely messing with $decisions[‘my_theme_new_section’][‘fields’], not all of the $decisions array.

In reality, it is also attainable to hook into ‘thsp_cbp_options_array’ filter hook out of your father or mom theme higher than as quickly as.. Let’s say you chose to not add plugin-territory choices to your theme and let plugins do what they’re presupposed to. Now you have to current some Theme Customizer decisions supplied {that a} positive plugin is energetic. As soon as extra, easy:

carry out my_plugin_dependency_options_array( $decisions ) {
	// Using helper carry out to get default required performance
	$thsp_cbp_capability = thsp_cbp_capability();

	/*
	 * Solely together with my_plugin_dependency_section if 'test-plugin.php' is energetic
	 */	
	if ( is_plugin_active( 'test-plugin/test-plugin.php' ) ) {
	
		$decisions['my_plugin_dependency_section'] = array(
			'existing_section' => false,
			'args' => array(
				'title' => __( 'Plugin Dependency', 'my_theme_textdomain' ),
				'priority' => 10
			),
			'fields' => array(
				/*
				 * Textual content material topic
				 */
				// Topic ID
				'new_text_field' => array(
					'setting_args' => array(
						'default' => __( '', 'my_theme_textdomain' ),
						'kind' => 'selection',
						'performance' => $thsp_cbp_capability,
						'transport' => 'refresh',
					),					
					'control_args' => array(
						'label' => __( 'Solely reveals if', 'my_theme_textdomain' ),
						'kind' => 'textual content material', // Textual content material topic administration
						'priority' => 5
					)
				),
			)
		);
	
	}
	
	return $decisions;
}
add_filter( 'thsp_cbp_options_array', 'my_plugin_dependency_options_array', 3 );

Want to develop a core efficiency plugin , collectively along with your theme (as you could)? It’s possible you’ll hook into ‘thsp_cbp_options_array’ from one amongst your plugin’s data too. The equivalent method you’d do it from a theme’s carry out.php file.

Don’t Go Alternative Crazy

Every time you’re together with decisions to a theme you develop. You need to maintain one amongst WordPress’ core concepts — Alternative not Decisions — in ideas. Start together with every aspect your theme has individual decisions  to get carried away , nevertheless that doesn’t make anyone a favor. I hope these few strategies, notably together with plugin dependant decisions, will help maintain your theme’s decisions rely as little as attainable.

In any case, in case your theme has decisions for points like every border radius of every single part, it’s not a theme it’s a WYSIWYG editor and probably not an unbelievable one.

You don’t buy a white shirt because of with some additional effort you can rework it proper right into a desk materials, you buy it because you like its “whiteshirtness”. WordPress themes must be like that, too, they should present content material materials in a positive method, not try and do each half in every method conceivable.

Download The WPSaviour App Now

Related posts
Beginner’s GuideBlogTutorials

Use Gutenberg like a pro with keyboard shortcuts

Beginner’s GuideBlogSecurityWordPress Security

A Simple Explanation of SSL Certificate Errors & How to Fix Them

Beginner’s GuideBest WordPress PluginsBlog

9 Best A/B Split Testing Plugins for WordPress

Best WordPress PluginsBlog

7 Best Plugins for Tracking User Activity in WordPress

Sign up for our Newsletter and
stay informed

Fear Of Missing Out?

Sign up with your email address to receive WordPress tips and updates
SUBSCRIBE NOW
Terms and Conditions apply
close-link
Click Me