If you want to add items dynamically at every page call, you can look this answer :
Dynamically add sub-categories to any category in the menu
And to add an element directly, you can try that for a custom link e.g. :
// create the new element
$post = [
'post_title' => "StackExchange " . date("H:i:s"),
"menu_order" => 700, // position in the menu
'post_type' => 'nav_menu_item',
'post_status' => 'publish',
];
$menu_item_db_id = wp_insert_post( $post );
update_post_meta( $menu_item_db_id, 'url', "https://wordpress.stackexchange.com/");
update_post_meta( $menu_item_db_id, 'type', "custom");
update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', 0); // change to create a sub menu
// association to the menu
$menuLocations = get_nav_menu_locations();
$menuID = (int) $menuLocations['primary'];
wp_set_object_terms($menu_item_db_id, $menuID, "nav_menu");
Related Posts:
- add_menu_page() with different name for first submenu item
- How to add sub-menu to a menu generated by wp_nav_menu by using plugin
- Change the_title() of a page dynamically
- How do I programatically insert a new menu item?
- Add Dividers or Separators Between Nav Menu Items
- Nav Menu meta failing to import
- Overwriting Core WordPress Functions with Plugins
- Some nav-menu filters do nothing
- Determine which theme location a wp_get_nav_menu_items is for
- Can I add pages to my custom menu via script?
- Replacing WordPress menu functionality with a plugin
- Hook event for upload image in the menu
- Why isn’t the Settings API designed to work for plugins using custom admin menus? [duplicate]
- How do I approach removing menu items on the fly based on settings in my plugin?
- What is the difference between current_page_parent and current_page_ancestor?
- Delete a specific item menu when I deactivate my plugin
- Create a Page via plugin
- Add Submenu Link in add_submenu_page That Opens in a New Window
- How to assign a WP 3.0 custom nav menu to a theme’s navigation menu location via script?
- Header Button Chance Polylang Elementor
- PHP 7 – Class Method Compatibility Issue
- How to add pages to custom menus on the fly [duplicate]
- How do I add a custom sublevel menu specified in one directory to a custom top level menu specified in another directory?
- Add notification bubble notice in navigation using transients
- Get Current Menu Location inside Nav_Walker
- Adding custom fields to WordPress nav menus
- Sub Menu content is being duplicated
- Plugin menu addition in multisite
- Remove unwanted elements for a wp_nav_menu
- Is this best practice for Dynamically adding items to WordPress menus?
- Custom code for WordPress dynamic menu
- How do I access the menus produced by Dashboard > Appearance > Menus
- wordpress add_submenu_page adds broken link
- WP Enqueue style on all plug-in pages
- Add child pages to submenu automatically
- Customize existing menu item
- Menu page with minimum capability as ‘Subscriber’ doesn’t allow ‘Admin’ to access it?
- Issue with plugin sub menu and pages
- How to add Plugin functionality in WordPress Frontend Menus
- How to hide page links from theme menu
- WP_NAV_MENU filter targets all menus
- How do I add a menu item to a Pods admin menu?
- Add user managable titles to custom menus?
- How to create archive page to add in menu
- How to show only the last two categories in a menu?
- Always hide a page from the menu
- remove different admin menu for specific users
- How to get an array out of a nav menu if it’s a plugin?
- What happens when you create/edit a menu
- Plugin admin list pages as submenu
- Adding parent custom post type menu option
- Create and style menu
- Change the class of wordpress menu
- Invalid Menu Items
- Declaration of mandoe_menu_walker::start_el(&$output, $item, $depth, $args) must be compatible with Walker::start_el(…)
- Custom Nav Walker $item->url producing malformed hyperlinks
- Displaying an Uploaded Image as a Custom Avatar in WordPress
- Adding a navigation with wp_nav_menu() to a custom block in the site editor
- Getting wrong relationship value in $args in wp_Query?
- wp_schedule_event in a class
- custom XMLRPC method plus authentication of user & WooCommerce order
- How to extend LINK TO functionality in ATTACHMENT DISPLAY SETTINGS
- How do I Make a Theme “plugin-ready”?
- WordPress Terminology Options Vs. Settings
- Display only certain posts based on visitor’s country?
- Plugin Sidebar is not Saving Meta Attribute to Post/Page after “Update”
- Possible to preset a widget’s contents via a plugin script?
- Synchronize custom post type tags with WordPress default post type tags
- What for the tables ending with the meta used in database of wordpress?
- How to create custom field in worpdress default widgets?
- Ensure function has completed before allowing another Ajax call
- Loading scripts with wp_register_script and wp_enqueue_script
- Is Using WordPress Supplied WYSIWYG Advisable?
- How do I add a filter to wp_list_categories() to make links nofollow?
- How to hook into publish post events in wordpress 4.7.5
- What is the best way to embed an JS App in a WordPress Page?
- Plugin development – How to use a specific wp_options into the real rendered code in the front end?
- Show disclaimer with accept button before redirecting to the payment gateway website
- Why is the temporary upload always 4.1K
- Problem with dinamic add_rewrite_rule()
- Trying to code a simple plugin, infinite loop?
- Update own Plugin Repository on Multisite
- Inserting code to HTML view from a pop up initiated from visual view
- CSS not affecting widget output
- Using a custom shortcode from within the template of a shortcode plugin
- Trying to get class to instantiate ONCE (or, “why do I keep punching myself in the head?”)
- How to create a WordPress Plugin that has it’s own “page”?
- Create Widget or Enable Shortcodes in Sidebar
- how to create category with code in wordpress using form
- Is it possible to have instead mypage.com/?page_id=81 to have mypage.com/cool/?
- User Data Handling between two plugins
- Woocommerce place order update shipping price
- ajax response strips multidimensional array and unable to decode
- Using AJAX to submit and return data inside the WordPress Plugin Boiler Plate framework
- Pass javascript result to shortcode executer function
- Plugins and how to assign urls to content
- How to make auto installer Plugin?
- Pass results of custom query to loop – when writing a plugin
- user can login from single account detail from multiple locations(computer) at the same time [closed]
- WordPress plugin boilerplate AJAX functionality