Note that there are some explanatory texts on larger screens.

plurals
  1. POWordpress add_filter doesn't do the job
    text
    copied!<p>I want to add onclick="_gaq.push(..... to the nav menu by adding a filter to the theme's functions.php. Thought it would be a peace of cake, but there is no output, so something must go wrong.</p> <p>This is the Wordpress core start_el function</p> <pre><code>function start_el( &amp;$output, $item, $depth = 0, $args = array(), $id = 0 ) { $indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; $class_names = $value = ''; $classes = empty( $item-&gt;classes ) ? array() : (array) $item-&gt;classes; $classes[] = 'menu-item-' . $item-&gt;ID; $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) ); $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item-&gt;ID, $item, $args ); $id = $id ? ' id="' . esc_attr( $id ) . '"' : ''; $output .= $indent . '&lt;li' . $id . $value . $class_names .'&gt;'; $atts = array(); $atts['title'] = ! empty( $item-&gt;attr_title ) ? $item-&gt;attr_title : ''; $atts['target'] = ! empty( $item-&gt;target ) ? $item-&gt;target : ''; $atts['rel'] = ! empty( $item-&gt;xfn ) ? $item-&gt;xfn : ''; $atts['href'] = ! empty( $item-&gt;url ) ? $item-&gt;url : ''; $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args ); $attributes = ''; foreach ( $atts as $attr =&gt; $value ) { if ( ! empty( $value ) ) { $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); $attributes .= ' ' . $attr . '="' . $value . '"'; } } $item_output = $args-&gt;before; $item_output .= '&lt;a'. $attributes .'&gt;'; $item_output .= $args-&gt;link_before . apply_filters( 'the_title', $item-&gt;title, $item-&gt;ID ) . $args-&gt;link_after; $item_output .= '&lt;/a&gt;'; $item_output .= $args-&gt;after; $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); } </code></pre> <p>and I only want to change it into</p> <pre><code>function start_el( &amp;$output, $item, $depth = 0, $args = array(), $id = 0 ) { $indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; $class_names = $value = ''; $classes = empty( $item-&gt;classes ) ? array() : (array) $item-&gt;classes; $classes[] = 'menu-item-' . $item-&gt;ID; $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) ); $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item-&gt;ID, $item, $args ); $id = $id ? ' id="' . esc_attr( $id ) . '"' : ''; $output .= $indent . '&lt;li' . $id . $value . $class_names .'&gt;'; $atts = array(); $atts['title'] = ! empty( $item-&gt;attr_title ) ? $item-&gt;attr_title : ''; $atts['target'] = ! empty( $item-&gt;target ) ? $item-&gt;target : ''; $atts['rel'] = ! empty( $item-&gt;xfn ) ? $item-&gt;xfn : ''; $atts['href'] = ! empty( $item-&gt;url ) ? $item-&gt;url : ''; $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args ); $attributes = ''; foreach ( $atts as $attr =&gt; $value ) { if ( ! empty( $value ) ) { $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); $attributes .= ' ' . $attr . '="' . $value . '"'; } } $onclick = 'onclick="_gaq.push([\'_trackEvent\', \'Topmenu\', \'Click\',\''.$item-&gt;attr_title.'\'])"'; $item_output = $args-&gt;before; $item_output .= '&lt;a'. $attributes .' '.$onclick.'&gt;'; $item_output .= $args-&gt;link_before . apply_filters( 'the_title', $item-&gt;title, $item-&gt;ID ) . $args-&gt;link_after; $item_output .= '&lt;/a&gt;'; $item_output .= $args-&gt;after; $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); } </code></pre> <p>There are only two adjustments (at the bottom of the code)</p> <p>I added this line (10th line from bottom):</p> <pre><code>$onclick = 'onclick="_gaq.push([\'_trackEvent\', \'Topmenu\', \'Click\',\''.$item-&gt;attr_title.'\'])"'; </code></pre> <p>And I changed the 7th line from the bottom to become:</p> <pre><code>$item_output .= '&lt;a'. $attributes .' '.$onclick.'&gt;'; </code></pre> <p>When I make these changes directly to the core file (wp-includes/nav-menu-template.php), it works. I have tried with <strong>class Description_Walker extends Walker_Nav_Menu</strong> and <strong>add_filter</strong> but both times I didn't succeed.</p> <p>How to get it working?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload