Extending Tab Override

A common problem with WordPress themes and plugins is users making custom modifications to them only to have their work erased when the theme or plugin is updated. To avoid this, you can create a child theme that extends the original theme or a plugin that modifies another plugin. Tab Override version 3.0 provides a global object and hooks just for this purpose.

The $tab_override Global Object

As soon as the plugin code is executed, a global object named $tab_override is created. This object is an instance of the Tab_Override class. You can use this object to check if the Tab Override plugin is installed and activated. Normally to check for another plugin you would do something like this:

if ( in_array(
	'tab-override/tab-override.php',
	get_option( 'active_plugins', array() )
) ) {
	add_action( 'plugins_loaded', 'my_plugin_function' );
}

If you just need to check for version 3 or higher, you can do this instead:

function my_plugin_function() {
	global $tab_override;
	if ( $tab_override ) {
		// Tab Override is installed and activated
	}
}
add_action( 'plugins_loaded', 'my_plugin_function' );

An alternative, even better solution, would be to use the plugin hooks.

Using the Plugin Action Hooks

As of version 3.0, two action hooks are provided for use by other plugins. They are tab_override_init and tab_override_add_scripts. They can be used by calling the add_action function just like any other action hooks in WordPress.

The tab_override_init Action Hook

This hook is fired when Tab Override has successfully initialized itself. A simpler way to check for the Tab Override plugin is to use this hook like this:

function my_plugin_function() {
	// Tab Override is installed and activated
}
add_action( 'tab_override_init', 'my_plugin_function' );

This has a couple advantages. Primarily, my_plugin_function will only be executed when Tab Override is running. This means only in the admin area and when the plugin is installed and activated. The previous examples would run on every page load and check for the plugin, even on the front-end of the site.

The other notable advantage is that this hook is only fired once the plugin has initialized itself, meaning that all the primary actions and filters have been added. If you plan on modifying or removing any of these actions or filters, it's better to rely on this hook than try to guess another appropriate one or you may end up trying to remove something that hasn't been added yet.

Here is another example that disables the visual editor when Tab Override is activated. Deactivating the Tab Override plugin will automatically re-enable the visual editor.

function my_plugin_disable_visual_editor() {
	// the __return_false function is available in WP 3.0+
	add_filter( 'user_can_richedit', '__return_false' );
}
add_action( 'tab_override_init',
	'my_plugin_disable_visual_editor' );

The tab_override_add_scripts Action Hook

This hook is fired after JavaScript is added to the page. You can use this hook to insert additional scripts that change the way Tab Override works. Here is a simple example that outputs the settings to the console when Tab Override is in use:

function my_plugin_add_script() {
	wp_enqueue_script(
		'display_tab_override_settings',
		plugins_url( 'js/display-settings-1.0.js', __FILE__ ),
		array( 'jquery', 'tab-override', 'tab-override-setup' ),
		'1.0',
		true
	);
}
add_action( 'tab_override_add_scripts',
	'my_plugin_add_script' );

display-settings-1.0.js:

// display Tab Override settings in the console
(function ($) {
    var tabSize = $.fn.tabOverride.getTabSize();
    if (window.console && console.log) {
        console.log(
            'Tab Override Settings\n' +
            '\tTab Size: ' + (tabSize || 'tab') + '\n' +
            '\tAuto Indent: ' + ($.fn.tabOverride.autoIndent ? 'enabled' : 'disabled')
        );
    }
}(jQuery));

Example Plugin

This is an example plugin that removes the Tab Override submenu from the Settings menu. It uses the tab_override_init hook and the $tab_override global object.

<?php
/*
Plugin Name: Tab Override Plus
Description: Removes the Tab Override submenu.
*/

function tab_override_plus_remove_menu() {
	global $tab_override;
	remove_action( 'admin_menu',
		array( $tab_override, 'create_menu' ) );
}
add_action( 'tab_override_init',
	'tab_override_plus_remove_menu' );

Editing the main plugin files is dangerous, as you risk losing your changes whenever the plugin is updated. Using the above methods provides a safer, cleaner way to modify the plugin functionality.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>