Hook of the Month: Customize Modules, Shortcodes, and Widgets

Whether you’re a blogger, a business owner, or a developer – Jetpack comes with many different features you can use to build your site, write your posts, and promote them. However, you may not use all of these features. This month, we’ll discover how to use filters to customize the list of features added by Jetpack.

Photo: Andrewfhart / Flickr

Photo: Andrewfhart / Flickr

Control the list of modules available under Jetpack > Settings

Currently Jetpack includes 36 different modules. Among those, there might be a few that you don’t use, and even a few that you know you’ll never use. It could be because you already developed or integrated that feature in another plugin, for example.

Jetpack includes a management interface so you can deactivate what you don’t need. You can manage all modules under Jetpack > Settings in your dashboard. Once a module is deactivated, its code won’t run on the site.

You can also use a filter, jetpack_get_available_modules, to completely remove some modules from the Jetpack interface. The modules won’t appear there, and you (or anyone else) will never be able to activate them.

In the example below, we’ll only show seven specific modules in the Jetpack Settings page. All the other modules won’t be shown.

/**
 * Let's activate 7 specific modules, and nothing more.
 */
function jeherve_only_seven_modules( $modules, $min_version, $max_version ) {
    $my_modules = array(
        'stats',
        'photon',
        'related-posts',
        'markdown',
        'sso',
        'custom-content-types',
        'custom-css',
    );
    return array_intersect_key( $modules, array_flip( $my_modules ) );
}
add_filter( 'jetpack_get_available_modules', 'jeherve_only_seven_modules', 20, 3 );

Note: this code won’t deactivate modules. If you had previously activated one of the features that is now hidden, it will remain active and you won’t be able to deactivate it, as it is now hidden from the list of available modules.

Control the list of widgets available in Jetpack’s Extra Sidebar Widgets module

You can use Jetpack’s Widgets module to add a Facebook Like Box, a Twitter Timeline, a widget displaying your most popular posts, and more. These widgets are available under Appearance > Widgets in your dashboard, as soon as you activate the Extra Sidebar Widgets module.

In some cases, you might not want to see one of the modules in your dashboard, because you know you’ll never use it. That’s when the jetpack_widgets_to_include becomes handy. It allows you to edit the list of Jetpack widgets that will be available under Appearance > Widgets.

In the example below, we’ll remove the Google+ Badge widget from the list of available widgets:

// Search for the Google+ Badge Widget.
function jeherve_googleplus_badge_search( $widget ) {
	return strpos( $widget, 'googleplus-badge' ) === false;
}

// Remove the Google+ Badge Widget.
function jeherve_remove_googleplus_widget( $widgets ) {
	$widgets = array_filter( $widgets, 'jeherve_googleplus_badge_search' );
	return $widgets;
}
add_filter( 'jetpack_widgets_to_include', 'jeherve_remove_googleplus_widget' );

Control the list of shortcodes available in Jetpack’s Shortcode Embeds module

Jetpack comes with multiple shortcodes (33 at the time of writing) you can use to embed videos, forms, audio players, and many other elements into your site.

If you don’t need all 33 shortcodes, you can use the jetpack_shortcodes_to_include filter to remove some of them. In the example below, we’ll remove the Slideshow shortcode from the list of available shortcodes:

// Remove the slideshow shortcode
function jeherve_remove_jp_shortcode( $shortcodes ) {
	$jetpack_shortcodes_dir = WP_CONTENT_DIR . '/plugins/jetpack/modules/shortcodes/';

	$shortcodes_to_unload = array( 'slideshow.php' );

	foreach ( $shortcodes_to_unload as $shortcode ) {
		if ( $key = array_search( $jetpack_shortcodes_dir . $shortcode, $shortcodes ) ) {
				unset( $shortcodes[$key] );
		}
	}

	return $shortcodes;
}
add_filter( 'jetpack_shortcodes_to_include', 'jeherve_remove_jp_shortcode' );

That’s it for this month! Do you want to dive deeper into Jetpack, and discover other useful hooks to help you customize Jetpack? Make sure to follow this blog and discover new hooks every month!

This entry was posted in Code snippets, Tips & Tricks and tagged , , , , , . Bookmark the permalink.

Comments

  1. Great post.. these codes are very useful. Thanks for sharing.

    Like

Say Hello to 200 Themes

Jetpack Professional now bundles 200+ Premium WordPress themes alongside business-class security tools and marketing automation.

Learn More
%d bloggers like this: