Skip to content

Filters

rogerlos edited this page Jun 16, 2017 · 20 revisions

The following filters are accessible via add_filter(). Included are sample closures, with comments

Within Flexwalker Class

Configuration and HTML construction outside of the actual WordPress Menu.

Menu Arguments: Append Numeric?

When merging $args and $this->cfg, append (true) or overwrite (false) array values which have a numeric key?

add_filter( 'flexwalker_menu_numeric', function( $bool ) {

    // always append
    return TRUE;
}, 10, 1 );

Menu Arguments: Values

The merged array of arguments being used by Flexwalker.

add_filter( 'flexwalker_menu_args', function( $array ) {

    // change walker container_id argument
    $array['walker']['container_id'] = 'mynewid';

    return $array;
}, 10, 1 );

Menu Structure: Template

The template being used by Flexwalker. Note, this needs to be in the 'template' format, see JSON: templates. If it's empty, Flexwalker will return nothing.

add_filter( 'flexwalker_menu_template', function( $array ) {

    // change depth
    $array['depth'] = 1;

    return $array;
}, 10, 1 );

Menu ID

Flexwalker by default generates a random ID to assign to the menu.

add_filter( 'flexwalker_menu_id', function( $string ) {

    // change to your own value.
    return 'myniftyid';
}, 10, 1 );

HTML: Structure Tags

This is structure of the HTML returned, see JSON: templates for more information. There should be a special walker item in this array if you are actually calling a menu.

add_filter( 'flexwalker_navbar_tags', function( $tags_array, $template_id ) {

    // be careful here, see JSON: templates

    return $tags_array;
}, 10, 2 );

HTML Build: Opening or Closing Tag Array

Use 'close' instead of 'open' within the filter name to get the same information for the closing tag.

// $key is the numeric key corresponding to the array shown in the previous filter
add_filter( 'flexwalker_navbar_open_tag', function( $array, $key, $template_id ) {

    // $array is a tag array as seen in JSON: tags

    return $array;
}, 10, 3 );

HTML Build: Opening or Closing Tag HTML

Use 'close' instead of 'open' within the filter name to get the same information for the closing tag.

// $key is the numeric key corresponding to the array shown in the previous filter
add_filter( 'flexwalker_navbar_open_html', function( $string, $key, $template_id ) {

    // $array is a tag array as seen in JSON: tags
    // $string will include any content configured within the tag array

    return $string;
}, 10, 3 );

Finished Menu HTML

This is the completely rendered HTML string.

add_filter( 'flexwalker_navbar_html', function( $string, $template_id ) {

    // this will include the Flexwalker_Walker html output 
    // within other configured structure

    return $string;
}, 10, 2 );

Nav Menu Arguments

The arguments sent to wp_nav_menu.

add_filter( 'flexwalker_get_walker', function( $array ) {

    // change menu_class
    $array['menu_class'] = 'my-menu-class';

    return $array;
}, 10, 1 );

Nav Menu Wrapper Tag

Flexwalker looks for a tag with the id of 'menu', and this will be empty otherwise.

add_filter( 'flexwalker_wrapper_tag', function( $string ) {
    // should be an un-bracketed tag, like 'div' or 'p'
    return $string;
}, 10, 1 );

Nav Menu Wrapper HTML

Specifically the sprint string sent as the items_wrap argument

add_filter( 'flexwalker_wrapper', function( $string ) {
    return $string;
}, 10, 1 );

Array Sent to wp_nav_menu_args Filter

This is how Flexwalker gets its tag information into the walker class.

add_filter( 'flexwalker_walker_filter', function( $array ) {
    // $array:
    // [
    //    'flextags' => [],
    //    'flextemp' => [],
    //    'nid'      => '',
    // ]
    return $array;
}, 10, 1 );

Javascript Localization

Filter the array of data which is being made available to flexwalker.js. Note that arbitrarily changing the structure or keys will probably break things.

/* 
 * $array contains the contents of $this->cfg['display'] as modified by the 
 * passed arguments when flexwalker( $args ). $this->cfg['display'] is initially
 * the contents of display.json
 */
add_filter( 'flexwalker_localize', function( $array ) {

    // turn off resize hiding
    $array['resizehide']['use'] = false;

    return $array;
}, 10, 1 );

Within Flexwalker_Walker Class

Note that these regular WP filters are still available within this class:

  • nav_menu_css_class (within start_el)
  • nav_menu_item_id (start_el)
  • the_title (start_el)

Walker: Build Tags Argument

Build tags is called by both el and lvl methods within the walker class.

add_filter( 'flexwalker_walker_build_tags_arguments', function( $array ) {
    // 'key': (string) 'open', 'close'
    // 'topkey': (string) indicator of top-level item this is, or is child of
    // 'struct': (string) corresponds to 'struct' property within 'check'
    // 'tags': (array) tags extracted from 'struct'
    return $array;
}, 10, 1 );

Walker: Opening or Closing Tag Array

Same functionality as seen in HTML, above. $topkey is an internal walker property corresponding to the top-level nav item position this is or is a child of.

add_filter( 'flexwalker_walker_open_tag', function( $array, $key, $topkey ) {
    return $array;
}, 10, 3 );

Walker: Opening or Closing Tag HTML

// $key is the numeric key corresponding to the array shown in the previous filter
add_filter( 'flexwalker_walker_open_html', function( $string, $key, $topkey ) {

    // $array is a tag array as seen in JSON: tags
    // $string will include any content configured within the tag array

    return $string;
}, 10, 3 );

Walker: Accumulated HTML

Called at the end of the internal build tags call.

add_filter( 'flexwalker_walker_build_tags_html', function( $string, $args_array ) {

    // $args_array, as filtered above

    return $string;
}, 10, 2 );