With inspiration from this post and using the theme activation/deactivation hook hack by Krishna Sharma I'm trying to unregister a predefined nav_menu upon theme deactivation.
What is the proper way to use the unregister_nav_menu hook?
In summary here is what I'm doing now:
function onlyiol_deactivate() {
global $_wp_registered_nav_menus;
$dappm_onlyiol = 'dappm_onlyiol';
unregister_nav_menu( $dappm_onlyiol );
}
The function below (taking from the inspiration post) adds the menu properly when the theme is activated but I cannot get it to remove the menu upon deactivation.
function onlyiol_activate() {
if( function_exists( 'wp_nav_menu' ) ){
register_nav_menu( 'dappm_onlyiol', 'Default BuddyPress Menu' );
add_action( 'widgets_init', 'dappact_addmenu_onlyiol' );
}
}
wp_register_theme_activation_hook('onlyiol', 'onlyiol_activate');
function onlyiol_deactivate() {
global $_wp_registered_nav_menus;
$dappm_onlyiol = 'dappm_onlyiol';
unregister_nav_menu( $dappm_onlyiol );
}
wp_register_theme_deactivation_hook('onlyiol', 'onlyiol_deactivate');
function dappact_addmenu_onlyiol(){
$menuname = 'IOL Navigation Menu';
$bpmenulocation = 'dappm_onlyiol';
// Does the menu exist already?
$menu_exists = wp_get_nav_menu_object( $menuname );
// If it doesn't exist, let's create it.
if( !$menu_exists){
$menu_id = wp_create_nav_menu($menuname);
// Set up default BuddyPress links and add them to the menu.
wp_update_nav_menu_item($menu_id, 0, array(
'menu-item-title' => __('Home'),
'menu-item-classes' => 'home',
'menu-item-url' => home_url( '/' ),
'menu-item-status' => 'publish'));
wp_update_nav_menu_item($menu_id, 0, array(
'menu-item-title' => __('Find Us'),
'menu-item-classes' => 'findus',
'menu-item-url' => home_url( '/findus/' ),
'menu-item-status' => 'publish'));
}
}
For convenience (and @Jan Fabry asked for this in this reference post) this is the activation/deactivation snippet. I am running this in a plugin.
/**
* Provides activation/deactivation hook for wordpress theme.
*
* Usage:
* ----------------------------------------------
* Include this file before this line.
* ----------------------------------------------
* function my_theme_activate() {
* // code to execute on theme activation
* }
* wp_register_theme_activation_hook('mytheme', 'my_theme_activate');
*
* function my_theme_deactivate() {
* // code to execute on theme deactivation
* }
* wp_register_theme_deactivation_hook('mytheme', 'my_theme_deactivate');
* ----------------------------------------------
*
* @author Krishna Kant Sharma (http://www.krishnakantsharma.com)
*/
/**
*
* @desc registers a theme activation hook
* @param string $code : Code of the theme. This can be the base folder of your theme. Eg if your theme is in folder 'mytheme' then code will be 'mytheme'
* @param callback $function : Function to call when theme gets activated.
*/
function wp_register_theme_activation_hook($code, $function) {
$optionKey="theme_is_activated_" . $code;
if(!get_option($optionKey)) {
call_user_func($function);
update_option($optionKey , 1);
}
}
/**
* @desc registers deactivation hook
* @param string $code : Code of the theme. This must match the value you provided in wp_register_theme_activation_hook function as $code
* @param callback $function : Function to call when theme gets deactivated.
*/
function wp_register_theme_deactivation_hook($code, $function) {
// store function in code specific global
$GLOBALS["wp_register_theme_deactivation_hook_function" . $code]=$function;
// create a runtime function which will delete the option set while activation of this theme and will call deactivation function provided in $function
$fn=create_function('$theme', ' call_user_func($GLOBALS["wp_register_theme_deactivation_hook_function' . $code . '"]); delete_option("theme_is_activated_' . $code. '");');
// add above created function to switch_theme action hook. This hook gets called when admin changes the theme.
// Due to wordpress core implementation this hook can only be received by currently active theme (which is going to be deactivated as admin has chosen another one.
// Your theme can perceive this hook as a deactivation hook.)
add_action("switch_theme", $fn);
}
According to A HitchHackers guide through WordPress this is the function that the unregister_nav_menu hook calls...
function unregister_nav_menu( $location ) {
global $_wp_registered_nav_menus;
if ( is_array( $_wp_registered_nav_menus ) && isset( $_wp_registered_nav_menus[$location] ) ) {
unset( $_wp_registered_nav_menus[$location] );
return true;
}
return false;
}