Add Custom Menu Link Via Code In WordPress

We can add our custom menu link any menu location via code. For this, we have “wp_nav_menu_items” filter which help us to add the custom menus.

add_filter( ‘wp_nav_menu_items’, ‘add_custom_nav_menus’, 10, 2 );

this filter returns two parameters, $items and $args.

$items is a variable which contains the menu list which we have set from wp-admin->menus.

$args is an array which contains the arguments data like menu name, menu link, menu order etc.
How to use this filter ?

add_filter( 'wp_nav_menu_items', 'add_custom_nav_menus', 10, 2 );
function add_custom_nav_menus($items, $args ) {
 global $wpdb;
   if(in_array($args->theme_location,array('primary') )) {
         $items .= '<li class=”menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children has_children”>';
         $items .= '<a href=”'.get_site_url().'/my-account/” >My Account</a>';
         $items .= '<p class=”dropdownmenu”></p>';
         $items .= '<ul role=”menu” class=”sub-menu”>';
         $items .= '<li class=”menu-item menu-item-type-custom menu-item-object-custom”>';
         $items .= '<a href=”‘.wp_logout_url().'” >Logout</a>';
         $items .= '</li>';
         $items .= '</ul>';
         $items .= '</li>';
         $items .= '<li class=”menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children has_children”>';
         $items .= '<a href=”‘.get_site_url().’/login/” >Login</a>';
         $items .= '<p class=”dropdownmenu”></p>';
         $items .= '<ul role=”menu” class=”sub-menu”&gt';
         $items .= '<li class=”menu-item menu-item-type-custom menu-item-object-custom”>';
         $items .= '<a href=”‘.get_site_url().’/register/” >Register</a>';
         $items .= '</li>';
         $items .= '</ul>';
         $items .= '</li>';
 return  $items;

in above code, I’m adding for custom menu links which are login, register, my account and log out.

Why am I using custom menu ?

I want that if the user is login then only my account and log out menu should be shown. and if a user is not logged in then only login and register menu should be shown.

this can be done via wp-admin because I can’t apply a logical condition on that.

now by this if(in_array($args->theme_location,array(‘primary’) )) condition i am adding custom menu only on primary menu. the keyword “primary” is not fixed it always changes as per your theme. you need to check the menu location in which you want to add the custom menu links.

now as you can in code, i am just appending the HTML code in $items variable and returning the $items variable in this function.

You can apply our own logic to adding the custom menu as per your need.

Thanks for your reading.


Update Order Review Via Ajax On WooCommerce Checkout Page

Many Times we need to apply some custom condition for calculating the cart total. For example, applying a different type of shipping and calculating the new cart total and showing in order review.

But you want to do it via AJAX. it means you don’t want to reload the checkout page.

For this, there is an action “woocommerce_update_order_review” which we have to use with WooCommerce Ajax Call.

Here is the code :


function custom_checkbox_checker () 
		if ( is_checkout() ) 
			wp_enqueue_script( 'jquery' ); ?>
			jQuery(document).ready( function (e) 
				var $ = jQuery;
				if ( typeof wc_checkout_params === ‘undefined’ )
				return false;

				var updateTimer,dirtyInput = false,xhr;

				function update_shipping(billingstate,billingcountry) 

					if ( xhr ) xhr.abort();

					$( '#order_methods, #order_review' ).block({ message: null, overlayCSS: { background: '#fff url(' + wc_checkout_params.ajax_loader_url + ') no-repeat center', backgroundSize:'16px 16px', opacity: 0.6 } });

						var data = {

						action: 'woocommerce_update_order_review',

						security: wc_checkout_params.update_order_review_nonce,

						billing_state: billingstate,

						billing_country : billingcountry,

						post_data: $( 'form.checkout' ).serialize()


						xhr = $.ajax({

						type: 'POST',

						url: wc_checkout_params.ajax_url,

						data: data,

						success: function( response ) {

						var order_output = $(response);

						$( '#order_review' ).html( response['fragments']['.woocommerce-checkout-review-order-table']+response['fragments']['.woocommerce-checkout-payment']);



						error: function(code){





			jQuery('.state_select').change(function(e, params){



<?php }


    add_action( 'wp_footer', 'custom_checkbox_checker', 50 );

In Above code, I am updating order review on change of state.

First of all I’ve created a function “custom_checkbhox_checker();” which has been initialize in “wp_footer” action.
after that, i’m checking for checkout page “if ( is_checkout() )”, if checkout page found then only apply this script otherwise not.

then after I’m passing parameter for AJAX in which we have used “action: ‘woocommerce_update_order_review’,” this action. also, you can pass additional data as per your need.

For Example, we have used “billing_state: billingstate, billing_country : billingcountry” as parameters.

in success response of Ajax, you’ll get the update HTML code for order review total and shipping method which you have to put into “#order_review” ID.

Register a Custom Hook and use it in WordPress

In WordPress, we have the option to register a custom hook for our use. For this, we have “do_action();” which register a hook in WordPress.


you have right above code line to register a hook. you can choose you to hook name and parameters which you want to pass in that hook. Hook name should be unique.

How to use this hook ?

For using this hook we have another function is “add_action();” in this function we need to specify the hook name in which you want to perform the action and the function name in which we’ll write our performing code.


10 is the priority of action and 1 is the no of a parameter which you wanna get via this hook.