Create With Plugin

Function Arguments

Estimated reading: 2 minutes

All the hooks are getting 2 arguments:

  1. Menu item object:
array (
  'id' => 42, // An integer ID of item
  'title' => 'Laptops',
  'link' => 'http://.../product-category/computers/laptops/',
  'parent' => 40, // An integer ID of parent item
  'is_current' => false, // Is current or not
  'is_term_includes_post' => false,
  'is_page_includes_post' => false,
  'is_expanded' => true, // Is expanded or collapsed
  'posts_count' => 3,
  'is_post' => false, // Is WP Post (i.e. product) or not
  'target' => NULL, // Can be "_self" / "_blank" / NULL
  'children' => array (
    0 => 88,
    1 => 544,
    2 => 542,
    ...
  ),
  'is_expanded_current' => true, // (Optional) Is expanded due to the current
  'is_expanded_initial' => false, // (Optional) Is expanded with the "Initial expansion" settings
)
  1. An integer number of the item’s level

Usage examples


Adding WooCommerce “Add to cart” button for products

add_action( 'iwpm-all-item', function ($item, $level) {
    if ($item["is_post"]) { // Checking for a post item
        $id = $item["id"]; // Getting an ID from item argument
        $url = get_permalink() . '?add-to-cart=' . $id;
        echo "<a class='button' href='$url'>Add to cart</a>";
        // PS: We can get the Product object here like that: $product = wc_get_product($id);
    }
}, 10, 2 ); // Do not forget to set "2" for arguments count

Adding full width element for each menu item

Add an element with 100% width:

add_action( 'iwpm-all-item', function ($item, $level) {
    echo "<div style='width: 100%;>100% width element</div>";
}, 10, 2 );

And add these styles:

.iwpm-term__inner {
    flex-wrap: wrap;
}

Adding an element for specific menu item

add_action( 'iwpm-all-item', function ($item, $level) {
    if ($item["id"] === 456) {
        echo "<div>Example element</div>";
    }
}, 10, 2 );

Change the order of an added element for specific menu:

add_action( 'iwpm-636-item_link', function ($term, $level) {
    echo '<div style="color: cornflowerblue;">HERE</div>';
}, 10, 2 );

Default “order” for all elements is “0”.

So add these styles to increment the order of the other elements:

.iwpm-term__text {
    order: 1;
}

.iwpm-term__posts-count {
     order: 2;
}
EASY ACCESS