You could try the following code snippet to override the primary menu:
/**
* Override the primary menu.
*
* @param string $nav_menu The HTML content for the navigation menu.
* @param array $args Array of wp_nav_menu() arguments.
* @return string $nav_menu
*/
function wpse_150003( $nav_menu, $args )
{
//Override the primary menu:
if( 'primary' === $args->theme_location )
$nav_menu = 'My new menu';
return $nav_menu;
}
add_filter( 'wp_nav_menu', 'wpse_150003', PHP_INT_MAX, 2 );
where we find the input arguments info from the source.
Notice that we return the $nav_menu
value, instead of echoing it.
You could also filter my other attributes, for example menu_id
.
You might also consider these filters:
wp_nav_menu_items
wp_nav_menu_{$menu->slug}_items
I hope this helps.
Related Posts:
- How to add sub-menu to a menu generated by wp_nav_menu by using plugin
- Some nav-menu filters do nothing
- Determine which theme location a wp_get_nav_menu_items is for
- How do I access the menus produced by Dashboard > Appearance > Menus
- add_menu_page() with different name for first submenu item
- Difference Between Filter and Action Hooks?
- How to create an API for my plugin?
- How can I load a page template from a plugin?
- Change the_title() of a page dynamically
- How do I programatically insert a new menu item?
- Using query_vars filter
- Add Dividers or Separators Between Nav Menu Items
- Nav Menu meta failing to import
- How to get Post ID with the Add Filter Function
- Explanation of the “posts_join” and “posts_fields” filter hooks?
- How do you use the plugin boilerplate loader class to hook actions and filters?
- add_filter OO with parameters
- How Can I Add a Filter to Class Instance Only?
- Is it possible to remove next-post / previous-post with out creating a custom template?
- How do I Make a Theme “plugin-ready”?
- How to remove unwanted panels inside InspectorControls from core blocks in Gutenberg
- function triggered by “manage_users_custom_column” filter not working
- Overwriting Core WordPress Functions with Plugins
- Proper way to use apply_filters() with class functions?
- add_filter and remove_filter added before and after wp_query
- How to modify post content before writing to database?
- Actions or filters fired when data is saved in a custom table
- Filter on the_content ignores shortcodes
- How to change wordpress post title?
- Display only certain posts based on visitor’s country?
- What’s the difference between hooks, filters and actions? [duplicate]
- How to use filter hook ‘post_updated_messages’ in coherence with action hook ‘save_post’
- Conditional add_filter?
- Can I add pages to my custom menu via script?
- Replacing WordPress menu functionality with a plugin
- Is it possible to create an action hook using do_action() within add_action()?
- Hook for post permalink update
- Dynamically Override Fancy Title
- Calling apply_filters on non-documented hooks
- Hook event for upload image in the menu
- WordPress Media mime type filter problem 4.0
- How to allow Unfiltered HTML in a wordpress multisite install
- 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?
- How to add custom content under plugin row in WordPress admin plugin list?
- Changing bloginfo description from a plugin
- What is the difference between current_page_parent and current_page_ancestor?
- Is there a way to verified if an add_filter is already applied?
- Define a function outside a class and call the function using action or filter hook
- Does add_filter work outside functions.php
- Synchronize Custom post type tags to WordPress default posts tags
- Delete a specific item menu when I deactivate my plugin
- multiple functions with same filter
- Woocommerce – Hide a Column in Cart Table
- 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?
- Version upgrade: can my plugin filter the SQL statements issued during a database upgrade?
- 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
- add_filter : Passing an array instead of the callback function?
- How do I alter the comment form ‘allowed tags’ text in a plugin?
- WordPress after content Hook & external template part
- Namespaced action and filter tags
- Two functions utilizing registration_errors filter
- Is there a Filter that I can use to filter the tags before they are inserted in to the database?
- WordPress is automatically linking plain text email addresses
- best practice for query string values – get_query_var always empty for my value supplied in query string
- get_current_screen() return null
- Get Current Menu Location inside Nav_Walker
- Problems with: manage_${post_type}_posts_columns
- How To Change Logout Screen Title
- Add code inside specific wordpress standard function
- Adding custom fields to WordPress nav menus
- Sub Menu content is being duplicated
- Check for template part, else filter content
- Plugin menu addition in multisite
- Why doesn’t my simple the_title filter get applied?
- How to filter the_content() & include content from template
- Adding rewrite rule dynamically
- Prefixing plugin hooks (actions/filters) with a wrapper class or functions
- Replace youtube embed in wordpress
- Remove unwanted elements for a wp_nav_menu
- Can’t get woocommerce_get_price_html to work [closed]
- Is this best practice for Dynamically adding items to WordPress menus?
- Search and Filter
- How to remove or add submenu item on plugin activate or deactive
- How can I replace content in the WP Admin area before an admin page is rendered?
- How to filter content for specific content variable
- WordPress custom taxonomy check box to dropdown
- Custom code for WordPress dynamic menu
- Using ob_get_content to get_search_form puts into infinite loop
- Valid filenames for add_action’s first parameter
- WordPress class, using add_action to call member function does not work
- Redirect to another page using contact form 7? [closed]
- How to get all queries’s results after they have executed?
- How to get the post excerpt using post object?