wordpress multisite
i use this function
add_action( 'init', 'register_navmenus' );
function register_navmenus() {
global $blog_id;
global $cat1_link;
if ( !is_nav_menu( 'menuonglet'.$blog_id )) {
$menu_id = wp_create_nav_menu( 'menuonglet'.$blog_id );
$menu1 = array(
'menu-item-object-id' => '',
'menu-item-object' => '',
'menu-item-parent-id' => 0,
'menu-item-position' => 1,
'menu-item-type' => 'custom',
'menu-item-title' => 'Appareils auditifs' ,
'menu-item-url' => $cat1_link,
'menu-item-description' => '',
'menu-item-attr-title' => '',
'menu-item-status' => 'publish'
);
wp_update_nav_menu_item( $menu_id, 0, $menu1 );
}
}
when i create a new site, this menu is automatically created.
I need another function like that :
"if a custom menu with a name like 'menuonglet'.$number exist delete this menu, except if $number==$blog_id"
Peter Michael answers:
if ( is_nav_menu( 'menuonglet'.$blog_id ) && $number != $blog_id )
{
wp_delete_nav_menu( 'menuonglet'.$blog_id )
}
Peter Michael comments:
Thanks Adam ;-)
jevusi comments:
i had try, that didn't work.
Peter Michael comments:
How did you try? Can we see your full code?
jevusi comments:
add_action( 'init', 'register_navmenus' );
function register_navmenus() {
global $blog_id;
global $cat1_link;
if ( is_nav_menu( 'menuonglet'.$blog_id ) && $number != $blog_id )
{
wp_delete_nav_menu( 'menuonglet'.$blog_id )
}
if ( !is_nav_menu( 'menuonglet'.$blog_id )) {
$menu_id = wp_create_nav_menu( 'menuonglet'.$blog_id );
$menu1 = array(
'menu-item-object-id' => '',
'menu-item-object' => '',
'menu-item-parent-id' => 0,
'menu-item-position' => 1,
'menu-item-type' => 'custom',
'menu-item-title' => 'Appareils auditifs' ,
'menu-item-url' => $cat1_link,
'menu-item-description' => '',
'menu-item-attr-title' => '',
'menu-item-status' => 'publish'
);
wp_update_nav_menu_item( $menu_id, 0, $menu1 );
}
}
jevusi comments:
sorry, mistake
i use this code
add_action( 'init', 'register_navmenus' );
function register_navmenus() {
global $blog_id;
global $cat1_link;
if ( is_nav_menu( 'menuonglet'.$number ) && $number != $blog_id )
{
wp_delete_nav_menu( 'menuonglet'.$number )
}
if ( !is_nav_menu( 'menuonglet'.$blog_id )) {
$menu_id = wp_create_nav_menu( 'menuonglet'.$blog_id );
$menu1 = array(
'menu-item-object-id' => '',
'menu-item-object' => '',
'menu-item-parent-id' => 0,
'menu-item-position' => 1,
'menu-item-type' => 'custom',
'menu-item-title' => 'Appareils auditifs' ,
'menu-item-url' => $cat1_link,
'menu-item-description' => '',
'menu-item-attr-title' => '',
'menu-item-status' => 'publish'
);
wp_update_nav_menu_item( $menu_id, 0, $menu1 );
}
}
Peter Michael comments:
$number is not assigned a value, so $number is NULL
You should add maybe
global $blog_id, $cat1_link;
$number = 1;
...
Peter Michael comments:
Change $number value accordingly:
add_action( 'init', 'register_navmenus' );
function register_navmenus()
{
global $blog_id, $cat1_link;
$number = 1;
if ( is_nav_menu( 'menuonglet'.$number ) && $number != $blog_id )
{
wp_delete_nav_menu( 'menuonglet'.$number )
}
if ( !is_nav_menu( 'menuonglet'.$blog_id ))
{
$menu_id = wp_create_nav_menu( 'menuonglet'.$blog_id );
$menu1 = array
(
'menu-item-object-id' => '',
'menu-item-object' => '',
'menu-item-parent-id' => 0,
'menu-item-position' => 1,
'menu-item-type' => 'custom',
'menu-item-title' => 'Appareils auditifs' ,
'menu-item-url' => $cat1_link,
'menu-item-description' => '',
'menu-item-attr-title' => '',
'menu-item-status' => 'publish'
);
wp_update_nav_menu_item( $menu_id, 0, $menu1 );
}
}
jevusi comments:
if i use $number = 1
if ( is_nav_menu( 'menuonglet'.$number ) && $number != $blog_id )
means
if ( is_nav_menu( 'menuonglet1' ) && 1 != $blog_id )
No ?
but if the menu is menuonglet44 ?
Peter Michael comments:
Fixed, missing ;
add_action( 'init', 'register_navmenus' );
function register_navmenus()
{
global $blog_id, $cat1_link;
$number = 1;
if ( is_nav_menu( 'menuonglet'.$number ) && $number != $blog_id )
{
wp_delete_nav_menu( 'menuonglet'.$number );
}
if ( !is_nav_menu( 'menuonglet'.$blog_id ))
{
$menu_id = wp_create_nav_menu( 'menuonglet'.$blog_id );
$menu1 = array
(
'menu-item-object-id' => '',
'menu-item-object' => '',
'menu-item-parent-id' => 0,
'menu-item-position' => 1,
'menu-item-type' => 'custom',
'menu-item-title' => 'Appareils auditifs' ,
'menu-item-url' => $cat1_link,
'menu-item-description' => '',
'menu-item-attr-title' => '',
'menu-item-status' => 'publish'
);
wp_update_nav_menu_item( $menu_id, 0, $menu1 );
}
}
jevusi comments:
that doesn't work.
It's normal, no ? If number = 1 and the menu is menuonglet44... non ?
Peter Michael comments:
Set $number to 44 and it will delete the menu except if the blog_id is 44 too.
So, maybe there was a misunderstanding, let me try to rephrase your question:
"delete all custom menus with a name pattern like 'menuonglet'.$anynumber except if $anynumber == $blog_id"
Is this correct?
jevusi comments:
yes : if the menu is menuonglet125 i want remove it, except if 125 is the blog_id
In attchment an image with your function