Custom Attributes To WordPress Menus

They are such a large number of instructional exercises on the web to clarify "how to add a custom field to WordPress menus" however the vast majority of them treat about how to utilize the default CSS field of the default Portrayal field. None of them gives a genuine strategy to add a genuine new field to menus. In this way, today we will make a basic module that will include a "subtitle" field to any WordPress menu. 

Here is the thing that the last outcome is in the WordPress organization: 

Stage 1: The Module Creation 

We should go, as we found in one of my past post, making a module isn't unpredictable. You simply need to make another organizer under wp-content/modules and called it "sweet-custom-menu", and to make within it a document called "sweet-custom-menu.php". At that point open this document and include this code: 

```

/* 

Module Name: Custom Menu Reallexi 

Module URL: http://reallexi.com/

Portrayal: Custom WordPress menus 

Adaptation: 0.1 

Creator: Sam Elayyoub

Creator URI: http://reallexi.com 

Content Area: custom 

Area Way: dialects 

*/ 

This code is all we have to characterize another module. Presently we will make a custom PHP class containing a constructor and the capacities we should make the module function admirably. 

Here is the manner by which to make the class: 

class reallexi_custom_menu { 

/* - - - * 

* Constructor 

* - - - */ 

/** 

* Introduces the module by setting limitation, channels, and organization capacities. 

*/ 

work __construct() { 

}/end constructor 

/* All capacities will be put here */ 

$GLOBALS['reallexi_custom_menu'] = new reallexi_custom_menu(); 

```

The last line instantiates the class and include the entire class in a worldwide variable. 

Stage 2: Including Custom Fields Channel 

Since we have our class, we will make custom capacities. The primary capacity to include is the one that will enroll the "sub-title" custom field we need to be incorporated as a propelled menu quality. To do as such, here the capacity to make, put this code rather than/* All capacities will be set here */: 

```

/** 

* Add custom fields to $item nav protest 

* with a specific end goal to be utilized as a part of custom reallexi 

* @access open 

* @since 1.0 

* @return void 

*/ 

work custom_add_custom_nav_fields( $menu_item ) { 

$menu_item->subtitle = get_post_meta( $menu_item->ID, '_menu_item_subtitle', genuine ); 

return $menu_item; 

```

At that point, we have to advise WordPress to consider our capacity, so put the code underneath in the __construct() work: 

/add custom menu fields to menu 

add_filter( 'wp_setup_nav_menu_item', exhibit( $this, 'custom_add_custom_nav_fields' ) ); 

Stage 3: Sparing Custom Fields 

Regardless of whether the custom "sub-title" field doesn"t truly exist we should make a capacity that will spare its incentive on menu release. Menu components are in two wards custom post writes, so we can utilize the custom post compose APIs and post_meta strategy. Along these lines, to spare custom menu field esteem, include this capacity under custom_add_custom_nav_fields(): 

```
/** 

* Spare menu custom fields 

* @access open 

* @since 1.0 

* @return void 

*/ 

work custom_update_custom_nav_fields( $menu_id, $menu_item_db_id, $args ) { 

/Check if component is legitimately sent 

in the event that ( is_array( $_REQUEST['menu-thing subtitle']) ) { 

$subtitle_value = $_REQUEST['menu-thing subtitle'][$menu_item_db_id]; 

update_post_meta( $menu_item_db_id, '_menu_item_subtitle', $subtitle_value ); 


```


In this capacity, we are checking if the custom field esteem is sent from the frame we ar going to make and after that we basically store its esteem. Presently we need to add the capacity to the proper snare. To do as such, add this lines to __construct(): 

/spare menu custom fields 

```
add_action( 'wp_update_nav_menu_item', exhibit( $this, 'custom_update_custom_nav_fields'), 10, 3 ); 

```

Stage 4: The Shape 

On the off chance that you took after this instructional exercise well ordered, you'll presumably figure that we haven't made the frame that must contain our sub-title field. This part is more mind boggling than the past ones. This time we need to manage reallexi. I truly urge you to peruse the codex about the reallexi class, this will truly help you to comprehend what it is and what it does. By the way they're numerous incredible instructional exercises wherever on the web to give you more insights about this class, so please look at them! In most piece of the time, reallexis are utilized to adjust the HTML yield of a menu. Here, we are taking a shot at the administrator menu yield shape. Just add this capacity to your primary class: 

```
/** 

* Characterize new reallexi alter 

* @access open 

* @since 1.0 

* @return void 

*/ 

work custom_edit_reallexi($reallexi,$menu_id) { 

return 'Reallexi_Nav_Menu_Edit_Custom'; 

```

and after that this to the constructor: 

/alter menu reallexi 

add_filter( 'wp_edit_nav_menu_reallexi', exhibit( $this, 'custom_edit_reallexi'), 10, 2 ); 

What this does is supplanting the default administrator alter menu shape by a custom one. Since the channel, is included, duplicate these two lines at the base of the sweet-custom-menu.php record, outside the reallexi_custom_menu class: 

```
include_once( 'edit_custom_reallexi.php' ); 

include_once( 'custom_reallexi.php' ); 

```

We are going to incorporate two documents. The first "edit_custom_reallexi.php" is the one that will adjust the default frame to alter the menu. It's in this record we will include the sub-title field. 

The second one is the welker utilized on the site frontend, this is the document that will adjust the menu yield to your guests. 

As the "edit_custom_reallexi.php" is somewhat long, I am not going to glue the entire code. You can see the entire code here. The main code I added to it is from line 174 to line 185. Here is the additional code: 

```

 

 


 

 

 

 

 
```

 

On the off chance that you need to add some more fields to the menu, just copy these lines and duplicate and glue them. When this progression is done, the menu ought to work in the organization. You should now have the capacity to see the new "sub-title" field while adding another component to a menu. In the event that it's not the situation, make you took after the instructional exercise well ordered. It's presently time to yield the subtitle an incentive on the frontend. 

Stage 5: Custom Field Yield 

In the event that everything is working fine in the organization, you likely now need to show the subtitle on the frontend. Open custom_reallexi.php and include thise code ot it: 

```

/** 

* Custom reallexi 

* @access open 

* @since 1.0 

* @return void 

*/ 

class custom_reallexi expands reallexi_Nav_Menu 

work start_el(&$output, $item, $depth, $args) 

worldwide $wp_query; 

$indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; 

$class_names = $value = ''; 

$classes = discharge( $item->classes ) ? cluster() : (exhibit) $item->classes; 

$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ); 

$class_names = ' class="'. esc_attr( $class_names ) . '"'; 

$output .= $indent . '

  • '; 

     

    $attributes = ! discharge( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : ''; 

    $attributes .= ! discharge( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : ''; 

    $attributes .= ! discharge( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : ''; 

    $attributes .= ! discharge( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : ''; 

    $prepend = ''; 

    $append = ''; 

    $description = ! discharge( $item->description ) ? ''.esc_attr( $item->description ).'' : ''; 

    if($depth != 0) 

    $description = $append = $prepend = ""; 

    $item_output = $args->before; 

    $item_output .= ''; 

    $item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append; 

    $item_output .= $description.$args->link_after; 

    $item_output .= ' '.$item->subtitle.''; 

    $item_output .= $args->after; 

    $output .= apply_filters( 'reallexi_nav_menu_start_el', $item_output, $item, $depth, $args ); 

    ```

    This code is quite standard. The main essential thing here is this line: 

    ```
    $item_output .= ' '.$item->subtitle.''; 

    ```

    Stage 6: Call The reallexi! 

    In the last advance you should open the header.php document in your topic organizer. It's the place most piece of the time the menu is called. If not, at that point contact the subject maker to know where it's called. 

    Discover the wp_nav_menu() work. 

    At that point just include the reallexi parameter: 

    ```
    'reallexi' => new custom_reallexi 
    ```


    That is it !

By: Mutasem Elayyoub