i have a function to duplicate a site in a multisite installation.
I have a function to remove the custom menu in the duplicated site
This is the function :
$allmenus = wp_get_nav_menus();
if($allmenus)
{
foreach($allmenus as $menu)
{
$needle = 'menutab'; //the name of the custom menu is like that : menutab19 (menutab+$blog_id)
$needle_length = strlen($needle);
if(strstr($menu->name, $needle) !== false)
{
$menu_id = substr($menu->name, $needle_length);
$menu_id = (int)$menu_id;
if($menu_id != $blog_id)
{
wp_delete_nav_menu($menu->name);
}
}
}
}
instead of delete the menu i want to rename the menu
str_replace ($menu_id,$blog_id,$menu->name)
items of "menutab+$blog_id" will be the same of "menutab+$menu_id"
Francisco Javier Carazo Gil answers:
Hi Sébastien,
I have not proved that, but I'm going to try to make it posible:
// first we save the items
$menu_items = wp_get_nav_menu_items($menu->term_id);
// we delete the menu
wp_delete_nav_menu($menu->name);
// we create a new one
$new_name = str_replace ($menu_id,$blog_id,$menu->name);
$new_menu = wp_update_nav_menu_object( 0, array( 'menu-name' => $new_name ) );
// insert items
foreach($menu_items as $item)
wp_save_nav_menu_items( $new_menu->id, $item);
That's all. Hope it helps!
Sébastien | French WordpressDesigner comments:
there is an error message :
Fatal error: Call to undefined function wp_save_nav_menu_items() in /home/xxx/public_html/wp-content/themes/xxx/functions.php on line 145
Francisco Javier Carazo Gil comments:
Sébastien,
As Just Me gives the source code. Take the trac directly:
http://core.trac.wordpress.org/browser/tags/3.1/wp-admin/includes/nav-menu.php#L941
Sébastien | French WordpressDesigner comments:
you forfeit?
Sébastien | French WordpressDesigner comments:
$new_menu is an ID
so $new_menu->id doesn't exist...
and
$new_name = str_replace ($menu_id,$blog_id,$menu->name);
must be placed above
wp_delete_nav_menu($menu->name);
i think.
Francisco Javier Carazo Gil comments:
Sebastién,
You should have declared this function in /wp-admin/includes/nav-menu.php.
You can try:
if(!function_exists(wp_save_nav_menu_items))
{
function wp_save_nav_menu_items( $menu_id = 0, $menu_data = array() ) {
$menu_id = (int) $menu_id;
$items_saved = array();
if ( 0 == $menu_id || is_nav_menu( $menu_id ) ) {
// Loop through all the menu items' POST values
foreach( (array) $menu_data as $_possible_db_id => $_item_object_data ) {
if (
empty( $_item_object_data['menu-item-object-id'] ) && // checkbox is not checked
(
! isset( $_item_object_data['menu-item-type'] ) || // and item type either isn't set
in_array( $_item_object_data['menu-item-url'], array( 'http://', '' ) ) || // or URL is the default
! ( 'custom' == $_item_object_data['menu-item-type'] && ! isset( $_item_object_data['menu-item-db-id'] ) ) || // or it's not a custom menu item (but not the custom home page)
! empty( $_item_object_data['menu-item-db-id'] ) // or it *is* a custom menu item that already exists
)
) {
continue; // then this potential menu item is not getting added to this menu
}
// if this possible menu item doesn't actually have a menu database ID yet
if (
empty( $_item_object_data['menu-item-db-id'] ) ||
( 0 > $_possible_db_id ) ||
$_possible_db_id != $_item_object_data['menu-item-db-id']
) {
$_actual_db_id = 0;
} else {
$_actual_db_id = (int) $_item_object_data['menu-item-db-id'];
}
$args = array(
'menu-item-db-id' => ( isset( $_item_object_data['menu-item-db-id'] ) ? $_item_object_data['menu-item-db-id'] : '' ),
'menu-item-object-id' => ( isset( $_item_object_data['menu-item-object-id'] ) ? $_item_object_data['menu-item-object-id'] : '' ),
'menu-item-object' => ( isset( $_item_object_data['menu-item-object'] ) ? $_item_object_data['menu-item-object'] : '' ),
'menu-item-parent-id' => ( isset( $_item_object_data['menu-item-parent-id'] ) ? $_item_object_data['menu-item-parent-id'] : '' ),
'menu-item-position' => ( isset( $_item_object_data['menu-item-position'] ) ? $_item_object_data['menu-item-position'] : '' ),
'menu-item-type' => ( isset( $_item_object_data['menu-item-type'] ) ? $_item_object_data['menu-item-type'] : '' ),
'menu-item-title' => ( isset( $_item_object_data['menu-item-title'] ) ? $_item_object_data['menu-item-title'] : '' ),
'menu-item-url' => ( isset( $_item_object_data['menu-item-url'] ) ? $_item_object_data['menu-item-url'] : '' ),
'menu-item-description' => ( isset( $_item_object_data['menu-item-description'] ) ? $_item_object_data['menu-item-description'] : '' ),
'menu-item-attr-title' => ( isset( $_item_object_data['menu-item-attr-title'] ) ? $_item_object_data['menu-item-attr-title'] : '' ),
'menu-item-target' => ( isset( $_item_object_data['menu-item-target'] ) ? $_item_object_data['menu-item-target'] : '' ),
'menu-item-classes' => ( isset( $_item_object_data['menu-item-classes'] ) ? $_item_object_data['menu-item-classes'] : '' ),
'menu-item-xfn' => ( isset( $_item_object_data['menu-item-xfn'] ) ? $_item_object_data['menu-item-xfn'] : '' ),
);
$items_saved[] = wp_update_nav_menu_item( $menu_id, $_actual_db_id, $args );
}
}
return $items_saved;
}
}
Francisco Javier Carazo Gil comments:
Sébastien,
wp_update_nav_menu_object I have read returns a menu object, not an id.
And yes, $new_name = str_replace ($menu_id,$blog_id,$menu->name) must be placed above
wp_delete_nav_menu($menu->name).
Sébastien | French WordpressDesigner comments:
this function is well declared in /wp-admin/includes/nav-menu.php
but i have the error message...
when i refresh the page with the error message, i can see the result : the menu is not renamed and items are not here...
Sébastien | French WordpressDesigner comments:
$new_menu return just a number which seems to be an ID...
and $new_menu->ID return nothing...
Sébastien | French WordpressDesigner comments:
i use this code
echo "new menu";
print_r($new_menu);
echo "<BR>";
// insert items
foreach($menu_items as $item) {wp_save_nav_menu_items( $new_menu->id, $item);
echo "new menu ID";
print_r($new_menu->id);
echo "<BR>";
and the output is just
new menu224
Sébastien | French WordpressDesigner comments:
sorry
the output is
new menu224
Fatal error: Call to undefined function wp_save_nav_menu_items() in /home/site2/public_html/wp-content/themes/auditionconseil-V2-modif/functions.php on line 124
Sébastien | French WordpressDesigner comments:
have you an idea ?
Francisco Javier Carazo Gil comments:
Sébastien,
Sorry for my delay but I was not in of the PC.
So, if the error is that, you have to redeclare the function (use function_exists to avoid errors).
And yes, I have just read that wp_update_nav_menu_object ([[LINK href="http://wpseek.com/wp_update_nav_menu_object/"]]http://wpseek.com/wp_update_nav_menu_object/[[/LINK]]) returns an ID (I thought I have read that returns an object). So, you have to construct the object with the ID.
The function to do this is this one: wp_get_nav_menu_object( $id_menu ) (you can see [[LINK href="http://wpseek.com/source/wp/latest/nav.html?wp-includes/nav-menu.php.html#wp_update_nav_menu_object"]]his code[[/LINK]]).
Sébastien | French WordpressDesigner comments:
ok i have add
$new_menu = wp_get_nav_menu_object($new_menu);
so now the code is
if($allmenus)
{
foreach($allmenus as $menu)
{
$menu_items = wp_get_nav_menu_items($menu->term_id);
// print_r ($menu_items);
// foreach($menu_items as $item) {echo $item->ID;}
$needle = 'menuonglet';
$needle_length = strlen($needle);
if(strstr($menu->name, $needle) !== false)
{
$menu_id = substr($menu->name, $needle_length);
$menu_id = (int)$menu_id;
if($menu_id != $blog_id)
{
// first we save the items
$menu_items = wp_get_nav_menu_items($menu->term_id);
// we delete the menu
wp_delete_nav_menu($menu->name);
// we create a new one
$new_name = str_replace ($menu_id,$blog_id,$menu->name);
$new_menu = wp_update_nav_menu_object( 0, array( 'menu-name' => $new_name ) );
$new_menu = wp_get_nav_menu_object($new_menu);
// insert items
foreach($menu_items as $item) {
if(is_array($item)) {echo "is table";} else {echo "is not table";}
wp_save_nav_menu_items( $new_menu->id, $item);
}
}
}
}
}
i have already the error message : Fatal error: Call to undefined function wp_save_nav_menu_items() in /home/site2/public_html/wp-content/themes/auditionconseil-V2-modif/functions.php on line 126
i think that i have find the reason :
in this function :
wp_save_nav_menu_items( $new_menu->id, $item);
the second parameter must be an array, but that's not an array...
Sébastien | French WordpressDesigner comments:
it seems that $item is an OBJECT
could you help me
now i do all ;-)
Sébastien | French WordpressDesigner comments:
i try this
foreach($menu_items as $item) {
$item = (array) $item;
if(is_array($item)) {echo "is table";} else {echo "is not table";}
wp_save_nav_menu_items( $new_menu->id, $item);
}
the output is :
is table
Fatal error: Call to undefined function wp_save_nav_menu_items() in /home/site2/public_html/wp-content/themes/auditionconseil-V2-modif/functions.php on line 127
Sébastien | French WordpressDesigner comments:
the line 127 is
wp_save_nav_menu_items( $new_menu->id, $item);
Francisco Javier Carazo Gil comments:
Sébastien,
It's strange the error, but you can declare the function just above the function that causes the problem.
I'm trying to find a good example about how to call the function.
Sébastien | French WordpressDesigner comments:
i use this code
$menu_items = objectToArray($menu_items);
foreach($menu_items as $slh) {
$slh = objectToArray($slh);
below i add your code :
if(!function_exists(wp_save_nav_menu_items))
{
function wp_save_nav_menu_items( $menu_id = 0, $menu_data = array() ) {
$menu_id = (int) $menu_id;
$items_saved = array();
if ( 0 == $menu_id || is_nav_menu( $menu_id ) ) {
// Loop through all the menu items' POST values
foreach( (array) $menu_data as $_possible_db_id => $_item_object_data ) {
if (
empty( $_item_object_data['menu-item-object-id'] ) && // checkbox is not checked
(
! isset( $_item_object_data['menu-item-type'] ) || // and item type either isn't set
in_array( $_item_object_data['menu-item-url'], array( 'http://', '' ) ) || // or URL is the default
! ( 'custom' == $_item_object_data['menu-item-type'] && ! isset( $_item_object_data['menu-item-db-id'] ) ) || // or it's not a custom menu item (but not the custom home page)
! empty( $_item_object_data['menu-item-db-id'] ) // or it *is* a custom menu item that already exists
)
) {
continue; // then this potential menu item is not getting added to this menu
}
// if this possible menu item doesn't actually have a menu database ID yet
if (
empty( $_item_object_data['menu-item-db-id'] ) ||
( 0 > $_possible_db_id ) ||
$_possible_db_id != $_item_object_data['menu-item-db-id']
) {
$_actual_db_id = 0;
} else {
$_actual_db_id = (int) $_item_object_data['menu-item-db-id'];
}
$args = array(
'menu-item-db-id' => ( isset( $_item_object_data['menu-item-db-id'] ) ? $_item_object_data['menu-item-db-id'] : '' ),
'menu-item-object-id' => ( isset( $_item_object_data['menu-item-object-id'] ) ? $_item_object_data['menu-item-object-id'] : '' ),
'menu-item-object' => ( isset( $_item_object_data['menu-item-object'] ) ? $_item_object_data['menu-item-object'] : '' ),
'menu-item-parent-id' => ( isset( $_item_object_data['menu-item-parent-id'] ) ? $_item_object_data['menu-item-parent-id'] : '' ),
'menu-item-position' => ( isset( $_item_object_data['menu-item-position'] ) ? $_item_object_data['menu-item-position'] : '' ),
'menu-item-type' => ( isset( $_item_object_data['menu-item-type'] ) ? $_item_object_data['menu-item-type'] : '' ),
'menu-item-title' => ( isset( $_item_object_data['menu-item-title'] ) ? $_item_object_data['menu-item-title'] : '' ),
'menu-item-url' => ( isset( $_item_object_data['menu-item-url'] ) ? $_item_object_data['menu-item-url'] : '' ),
'menu-item-description' => ( isset( $_item_object_data['menu-item-description'] ) ? $_item_object_data['menu-item-description'] : '' ),
'menu-item-attr-title' => ( isset( $_item_object_data['menu-item-attr-title'] ) ? $_item_object_data['menu-item-attr-title'] : '' ),
'menu-item-target' => ( isset( $_item_object_data['menu-item-target'] ) ? $_item_object_data['menu-item-target'] : '' ),
'menu-item-classes' => ( isset( $_item_object_data['menu-item-classes'] ) ? $_item_object_data['menu-item-classes'] : '' ),
'menu-item-xfn' => ( isset( $_item_object_data['menu-item-xfn'] ) ? $_item_object_data['menu-item-xfn'] : '' ),
);
$items_saved[] = wp_update_nav_menu_item( $menu_id, $_actual_db_id, $args );
}
}
return $items_saved;
}
}
below i add
wp_save_nav_menu_items( $new_menu->id, $slh);
the function objectToArray() transform an object into array
function objectToArray( $object )
{
if( !is_object( $object ) && !is_array( $object ) )
{
return $object;
}
if( is_object( $object ) )
{
$object = get_object_vars( $object );
}
return array_map( 'objectToArray', $object );
}
/*** convert the array to object ***/
$array = objectToArray( $obj );
/*** show the array ***/
print_r( $array );
so, the resultat : i have no error message.
the menu is created, but it contains no item.
Francisco Javier Carazo Gil comments:
Sébastien,
I cannot see from here what it's happening but I tell you.
You have to look which is the structure of $menu_items and then, prepare them to convert into this structure (the structure of the array used in wp_save_nav_menu_items:
$_item_object_data['menu-item-db-id']
$_item_object_data['menu-item-object-id']
$_item_object_data['menu-item-object']
$_item_object_data['menu-item-parent-id']
$_item_object_data['menu-item-position']
$_item_object_data['menu-item-type']
$_item_object_data['menu-item-title']
$_item_object_data['menu-item-url']
$_item_object_data['menu-item-attr-title']
$_item_object_data['menu-item-target']
$_item_object_data['menu-item-classes']
$_item_object_data['menu-item-xfn']
Sébastien | French WordpressDesigner comments:
$menu_items is your function wp_get_nav_menu_items($menu->term_id);
i use this code like that :
$allmenus = wp_get_nav_menus();
if($allmenus)
{
foreach($allmenus as $menu)
{
$menu_items = wp_get_nav_menu_items($menu->term_id);
print_r ($menu_items);
and the output is :
Array
(
)
Array
(
[0] => stdClass Object
(
[ID] => 1056
[post_author] => 2
[post_date] => 2011-04-21 11:28:44
[post_date_gmt] => 2011-04-21 09:28:44
[post_content] =>
[post_title] => Accueil
[post_excerpt] => Audition conseil Strasbourg
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => accueil-2
[to_ping] =>
[pinged] =>
[post_modified] => 2011-11-24 16:13:23
[post_modified_gmt] => 2011-11-24 14:13:23
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://testseb-alsace.fr/appareils-auditifs-alsace/accueil-2
[menu_order] => 1
[post_type] => nav_menu_item
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
[db_id] => 1056
[menu_item_parent] => 0
[object_id] => 1056
[object] => custom
[type] => custom
[type_label] => Lien
[title] => Accueil
[url] => http://testseb-alsace.fr
[target] =>
[attr_title] => Audition conseil Strasbourg
[description] =>
[classes] => Array
(
[0] =>
)
[xfn] =>
)
[1] => stdClass Object
(
[ID] => 1607
[post_author] => 2
[post_date] => 2011-11-24 16:13:23
[post_date_gmt] => 2011-11-24 14:13:23
[post_content] =>
[post_title] => test pour le clonage
[post_excerpt] =>
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => test-pour-le-clonage
[to_ping] =>
[pinged] =>
[post_modified] => 2011-11-24 16:13:23
[post_modified_gmt] => 2011-11-24 14:13:23
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://www.testseb-alsace.site2-auxaneconcept.fr/?p=1607
[menu_order] => 2
[post_type] => nav_menu_item
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
[db_id] => 1607
[menu_item_parent] => 1056
[object_id] => 1607
[object] => custom
[type] => custom
[type_label] => Lien
[title] => test pour le clonage
[url] => http://test.com
[target] =>
[attr_title] =>
[description] =>
[classes] => Array
(
[0] =>
)
[xfn] =>
)
[2] => stdClass Object
(
[ID] => 1057
[post_author] => 2
[post_date] => 2011-04-21 11:28:44
[post_date_gmt] => 2011-04-21 09:28:44
[post_content] =>
[post_title] => Appareils auditifs
[post_excerpt] => Protheses auditives Selestat
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => appareils-auditifs-5
[to_ping] =>
[pinged] =>
[post_modified] => 2011-11-24 16:13:24
[post_modified_gmt] => 2011-11-24 14:13:24
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://testseb-alsace.fr/appareils-auditifs-alsace/appareils-auditifs-5
[menu_order] => 3
[post_type] => nav_menu_item
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
[db_id] => 1057
[menu_item_parent] => 0
[object_id] => 1057
[object] => custom
[type] => custom
[type_label] => Lien
[title] => Appareils auditifs
[url] => http://testseb-alsace.fr/category/appareils-auditifs-alsace
[target] =>
[attr_title] => Protheses auditives Selestat
[description] =>
[classes] => Array
(
[0] =>
)
[xfn] =>
)
[3] => stdClass Object
(
[ID] => 1545
[post_author] => 3
[post_date] => 2011-11-14 12:43:52
[post_date_gmt] => 2011-11-14 10:43:52
[post_content] => Protheses auditives Strasbourg
Audioprothese Obernai
Appareils auditifs contours d’oreilles Alsace
Appareils auditifs Molsheim
Appareil auditif contours oreilles Schirmeck
Aide auditive Celestat
Prothese auditive contour d’oreille Obernai
Aides auditives contours d’oreilles Alsace
Audioprothèse contour d’oreille Strasbourg
Appareil auditif contour d’oreille Molsheim
[post_title] => Contours d'oreilles
[post_excerpt] => Appareils Auditifs Molsheim
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => contours-doreilles
[to_ping] =>
[pinged] =>
[post_modified] => 2011-11-24 16:13:24
[post_modified_gmt] => 2011-11-24 14:13:24
[post_content_filtered] =>
[post_parent] => 3
[guid] => http://testseb-alsace.fr/?p=1545
[menu_order] => 4
[post_type] => nav_menu_item
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
[db_id] => 1545
[menu_item_parent] => 0
[object_id] => 5
[object] => category
[type] => taxonomy
[type_label] => Catégorie
[url] => http://f9.site2-auxaneconcept.fr/category/appareils-auditifs-alsace/contours-oreilles-alsace
[title] => Contours d'oreilles
[target] =>
[attr_title] => Appareils Auditifs Molsheim
[description] => Protheses auditives Strasbourg
Audioprothese Obernai
Appareils auditifs contours d’oreilles Alsace
Appareils auditifs Molsheim
Appareil auditif contours oreilles Schirmeck
Aide auditive Celestat
Prothese auditive contour d’oreille Obernai
Aides auditives contours d’oreilles Alsace
Audioprothèse contour d’oreille Strasbourg
Appareil auditif contour d’oreille Molsheim
[classes] => Array
(
[0] =>
)
[xfn] =>
)
[4] => stdClass Object
(
[ID] => 1547
[post_author] => 3
[post_date] => 2011-11-14 12:43:52
[post_date_gmt] => 2011-11-14 10:43:52
[post_content] => Aides auditives Alsace
Appareils auditifs Strasbourg
Appareil auditif Obernai
Audioprothese Schirmeck
Protheses auditives Molsheim
Aide auditive intra auriculaires Selestat
Audioprothèses intra auriculaires Alsace
Appareil auditif Strasbourg
Aide auditive intra auriculaire Obernai
[post_title] =>
[post_excerpt] => Aides auditives Selestat
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => 1547
[to_ping] =>
[pinged] =>
[post_modified] => 2011-11-24 16:13:24
[post_modified_gmt] => 2011-11-24 14:13:24
[post_content_filtered] =>
[post_parent] => 3
[guid] => http://testseb-alsace.fr/?p=1547
[menu_order] => 5
[post_type] => nav_menu_item
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
[db_id] => 1547
[menu_item_parent] => 0
[object_id] => 4
[object] => category
[type] => taxonomy
[type_label] => Catégorie
[url] => http://f9.site2-auxaneconcept.fr/category/appareils-auditifs-alsace/intra-auriculaire-alsace
[title] => Intra auriculaires
[target] =>
[attr_title] => Aides auditives Selestat
[description] => Aides auditives Alsace
Appareils auditifs Strasbourg
Appareil auditif Obernai
Audioprothese Schirmeck
Protheses auditives Molsheim
Aide auditive intra auriculaires Selestat
Audioprothèses intra auriculaires Alsace
Appareil auditif Strasbourg
Aide auditive intra auriculaire Obernai
[classes] => Array
(
[0] =>
)
[xfn] =>
)
[5] => stdClass Object
(
[ID] => 1546
[post_author] => 3
[post_date] => 2011-11-14 12:43:52
[post_date_gmt] => 2011-11-14 10:43:52
[post_content] => Aides auditives écouteur déporté Alsace
Protheses auditives Obernai
Audioprothese ecouteur deporte Strasbourg
Appareils auditifs Schirmeck
Appareil auditif Selestat
Appareil auditif à écouteur déporté Strasbourg
Appareil auditif ecouteur deporte Molsheim
[post_title] =>
[post_excerpt] => Prothèses auditives Obernai
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => 1546
[to_ping] =>
[pinged] =>
[post_modified] => 2011-11-24 16:13:24
[post_modified_gmt] => 2011-11-24 14:13:24
[post_content_filtered] =>
[post_parent] => 3
[guid] => http://testseb-alsace.fr/?p=1546
[menu_order] => 6
[post_type] => nav_menu_item
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
[db_id] => 1546
[menu_item_parent] => 0
[object_id] => 64
[object] => category
[type] => taxonomy
[type_label] => Catégorie
[url] => http://f9.site2-auxaneconcept.fr/category/appareils-auditifs-alsace/ecouteur-deporte-alsace
[title] => Ecouteur déporté
[target] =>
[attr_title] => Prothèses auditives Obernai
[description] => Aides auditives écouteur déporté Alsace
Protheses auditives Obernai
Audioprothese ecouteur deporte Strasbourg
Appareils auditifs Schirmeck
Appareil auditif Selestat
Appareil auditif à écouteur déporté Strasbourg
Appareil auditif ecouteur deporte Molsheim
[classes] => Array
(
[0] =>
)
[xfn] =>
)
[6] => stdClass Object
(
[ID] => 1548
[post_author] => 3
[post_date] => 2011-11-14 12:43:52
[post_date_gmt] => 2011-11-14 10:43:52
[post_content] => Audioprotheses open fit
Appareils auditifs open fit Vaucluse
Protheses auditives open fit Orange
Audioprothese open fit Strasbourg
Aide auditive open fit Strasbourg
Appareils auditifs Orange
Appareil auditif Vaucluse
Aides auditives open fit Strasbourg
Prothèses auditives Villeneuve les Strasbourg
[post_title] =>
[post_excerpt] => Audioprothése Strasbourg
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => 1548
[to_ping] =>
[pinged] =>
[post_modified] => 2011-11-24 16:13:24
[post_modified_gmt] => 2011-11-24 14:13:24
[post_content_filtered] =>
[post_parent] => 3
[guid] => http://testseb-alsace.fr/?p=1548
[menu_order] => 7
[post_type] => nav_menu_item
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
[db_id] => 1548
[menu_item_parent] => 0
[object_id] => 10
[object] => category
[type] => taxonomy
[type_label] => Catégorie
[url] => http://f9.site2-auxaneconcept.fr/category/appareils-auditifs-alsace/open-fit
[title] => Open fit
[target] =>
[attr_title] => Audioprothése Strasbourg
[description] => Audioprotheses open fit
Appareils auditifs open fit Vaucluse
Protheses auditives open fit Orange
Audioprothese open fit Strasbourg
Aide auditive open fit Strasbourg
Appareils auditifs Orange
Appareil auditif Vaucluse
Aides auditives open fit Strasbourg
Prothèses auditives Villeneuve les Strasbourg
[classes] => Array
(
[0] =>
)
[xfn] =>
)
[7] => stdClass Object
(
[ID] => 1058
[post_author] => 2
[post_date] => 2011-04-21 11:28:45
[post_date_gmt] => 2011-04-21 09:28:45
[post_content] =>
[post_title] => Protections auditives
[post_excerpt] => Protections auditives Schirmeck
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => protections-auditives-5
[to_ping] =>
[pinged] =>
[post_modified] => 2011-11-24 16:13:24
[post_modified_gmt] => 2011-11-24 14:13:24
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://testseb-alsace.fr/appareils-auditifs-alsace/protections-auditives-5
[menu_order] => 8
[post_type] => nav_menu_item
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
[db_id] => 1058
[menu_item_parent] => 0
[object_id] => 1058
[object] => custom
[type] => custom
[type_label] => Lien
[title] => Protections auditives
[url] => http://testseb-alsace.fr/category/protection-auditive-alsace
[target] =>
[attr_title] => Protections auditives Schirmeck
[description] =>
[classes] => Array
(
[0] =>
)
[xfn] =>
)
[8] => stdClass Object
(
[ID] => 1549
[post_author] => 3
[post_date] => 2011-11-14 12:46:25
[post_date_gmt] => 2011-11-14 10:46:25
[post_content] => Bouchons auditifs Alsace
Bouchon anti bruit Obernai
Bouchons anti bruits Strasbourg
Bouchons oreilles Schirmeck
Bouchons oreilles Selestat
Bouchon auditif Molsheim
Protection auditive Strasbourg
Protections auditives Alsace
Protections auditives Obernai
[post_title] =>
[post_excerpt] => Appareils Auditifs Strasbourg
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => 1549
[to_ping] =>
[pinged] =>
[post_modified] => 2011-11-24 16:13:24
[post_modified_gmt] => 2011-11-24 14:13:24
[post_content_filtered] =>
[post_parent] => 67
[guid] => http://testseb-alsace.fr/?p=1549
[menu_order] => 9
[post_type] => nav_menu_item
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
[db_id] => 1549
[menu_item_parent] => 0
[object_id] => 77
[object] => category
[type] => taxonomy
[type_label] => Catégorie
[url] => http://f9.site2-auxaneconcept.fr/category/protection-auditive-alsace/bouchons-auditifs-alsace
[title] => Bouchons auditifs Alsace
[target] =>
[attr_title] => Appareils Auditifs Strasbourg
[description] => Bouchons auditifs Alsace
Bouchon anti bruit Obernai
Bouchons anti bruits Strasbourg
Bouchons oreilles Schirmeck
Bouchons oreilles Selestat
Bouchon auditif Molsheim
Protection auditive Strasbourg
Protections auditives Alsace
Protections auditives Obernai
[classes] => Array
(
[0] =>
)
[xfn] =>
)
[9] => stdClass Object
(
[ID] => 1059
[post_author] => 2
[post_date] => 2011-04-21 11:28:45
[post_date_gmt] => 2011-04-21 09:28:45
[post_content] =>
[post_title] => Produits d'entretien
[post_excerpt] => Entretien audition Obernai
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => produits-dentretien-5
[to_ping] =>
[pinged] =>
[post_modified] => 2011-11-24 16:13:24
[post_modified_gmt] => 2011-11-24 14:13:24
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://testseb-alsace.fr/appareils-auditifs-alsace/produits-dentretien-5
[menu_order] => 10
[post_type] => nav_menu_item
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
[db_id] => 1059
[menu_item_parent] => 0
[object_id] => 1059
[object] => custom
[type] => custom
[type_label] => Lien
[title] => Produits d'entretien
[url] => http://testseb-alsace.fr/category/produits-entretien-appareils-auditif-alsace
[target] =>
[attr_title] => Entretien audition Obernai
[description] =>
[classes] => Array
(
[0] =>
)
[xfn] =>
)
)
Francisco Javier Carazo Gil comments:
Sébastien,
I have to go out to another job. I can continue tomorrow but you have the structure of the array and you only have to pair for example: menu-item-url with url, any element with his corresponding field in other array.
Sébastien | French WordpressDesigner comments:
ok... so you have no solution ?
Francisco Javier Carazo Gil comments:
Sébastien,
I have not said I have no solution. I have said that you have to map the items between the array you obtain and the array you pass as an argument to create a new menu.
My mapping:
'menu-item-db-id' -> [db_id]
'menu-item-object-id' -> [object_id]
'menu-item-object' -> [object]
'menu-item-parent-id' -> [post_parent]
'menu-item-position' -> [menu_item_parent]
'menu-item-type' -> [type]
'menu-item-title' -> [title]
'menu-item-url' -> [url]
'menu-item-attr-title' -> [attr_title]
'menu-item-target' -> [target]
'menu-item-classes' -> [classes]
'menu-item-xfn' -> ¿?
You have to create a new element of one kind using this mapping.
Sébastien | French WordpressDesigner comments:
i understand but i need a code. Not a process.
Could you write the code, or not ?
Francisco Javier Carazo Gil comments:
Sébastien,
Foreach menu element in $menu_items = wp_get_nav_menu_items($menu->term_id);
You have to create an array:
foreach($menu_items as $menu_item)
{
$new = array();
$new['menu-item-db-id'] = $menu_item['db_id'];
...
wp_save_nav_menu_items( $new_menu->id, $new);
}
Sébastien | French WordpressDesigner comments:
Thanks francisco ! :-)
I use this code :
add_action( 'init', 'register_navmenus' );
function register_navmenus() {
global $blog_id;
global $cat1_link,$cat2_link,$cat3_link,$cat4_link,$centre_link,$news_link,$contact_link,$plan_link;
wp_delete_nav_menu('Menu_onglets');
wp_delete_nav_menu('menuonglet');
wp_delete_nav_menu('menu_onglet');
$allmenus = wp_get_nav_menus();
if($allmenus)
{
foreach($allmenus as $menu)
{
$menu_items = wp_get_nav_menu_items($menu->term_id);
// foreach($menu_items as $item) {echo $item->ID;}
$needle = 'menuonglet';
$needle_length = strlen($needle);
if(strstr($menu->name, $needle) !== false)
{
$name_id = substr($menu->name, $needle_length);
$name_id = (int)$name_id;
if($name_id != $blog_id)
{
// first we save the items
$menu_items = wp_get_nav_menu_items($menu->term_id);
$new_name = str_replace ($name_id,$blog_id,$menu->name);
// we delete the menu
wp_delete_nav_menu($menu->name);
// we create a new one
$new_menu = wp_update_nav_menu_object( 0, array( 'menu-name' => $new_name ) );
$new_menu = wp_get_nav_menu_object($new_menu);
$menu_items = objectToArray($menu_items);
foreach($menu_items as $menu_item) {
$menu_item = objectToArray($menu_item);
$new = array();
$new['menu-item-db-id'] = $menu_item['db_id'];
$new['menu-item-object-id'] = $menu_item['object_id'];
$new['menu-item-object'] = $menu_item['object'];
$new['menu-item-parent-id'] = $menu_item['post_parent'];
$new['menu-item-position'] = $menu_item['menu_item_parent'];
$new['menu-item-type'] = $menu_item['type'];
$new['menu-item-title'] = $menu_item['title'];
$new['menu-item-url'] = $menu_item['url'];
$new['menu-item-attr-title'] = $menu_item['attr_title'];
$new['menu-item-target'] = $menu_item['target'];
$new['menu-item-classes'] = $menu_item['classes'];
$new['menu-item-xfn'] = $menu_item['xfn'];
wp_save_nav_menu_items( $new_menu->id, $new);
}
}
}
}
}
}
AND... there is no item in the menu... :-((((
Francisco Javier Carazo Gil comments:
Sébastien,
WP_SAVE_NAV_MENU_ITEMS returns array The database IDs of the items saved.
Have you seen what happen in your database? Have you seen what returns the function? Are you seeing the data into the function?
I think I have helped enough for $10.
Sébastien | French WordpressDesigner comments:
Francisco
your function doesn't work.
i have work on this task and i have find a solution very simple.
I use this function : wp_update_nav_menu_object()
it's exactly what i need.
$allmenus = wp_get_nav_menus();
if($allmenus)
{
foreach($allmenus as $menu)
{
$needle = 'menuonglet';
$needle_length = strlen($needle);//taille de $needle
if(strstr($menu->name, $needle) !== false)//retourne la partie de $menu->name qui commence par $needle
{
//echo substr('abcdef', 1); retourne bcdef
$name_id = substr($menu->name, $needle_length);//retourne ce qui suite menuonglet, donc si le nom du menu est menuonglet19, retourne : 19
$name_id = (int)$name_id;
$new_name = $menu->name;
if($name_id != $blog_id)
{
$menu_id = $menu->term_id;
$new_name = str_replace ($name_id,$blog_id,$menu->name);
if ( !is_nav_menu( $new_name )) wp_update_nav_menu_object( $menu_id, array( 'menu-name' => $new_name ) );
}
}
}
}
Just Me answers:
Yep, the update menu hook function should work.
Take a look at this link, you may want to do some more checking, like, if a menu with that name already exists or something.
http://hitchhackerguide.com/2011/02/12/wp_update_nav_menu_object/
Sébastien | French WordpressDesigner comments:
sorry, i wait a solution. Not a link. please.