Thursday, September 15, 2022
HomeWordPress Developmentplugin improvement - Customized Nav Walker $item->url producing malformed hyperlinks

plugin improvement – Customized Nav Walker $item->url producing malformed hyperlinks


Utilizing the next:

class mandoe_menu_walker extends Walker_Nav_Menu {
    perform start_el(&$output, $merchandise, $depth = 0, $args = array(), $current_object_id = 0) {
        international $wp_query;

        $output .= '<li id="menu-item-'. $item->ID .'>';

        $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
        $attributes .= ! empty( $item->goal )     ? ' goal="' . esc_attr( $item->goal     ) .'"' : '';
        $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
        $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';

        $description  = ! empty( $item->description ) ? '<span class="mm-mm-subtext">'.esc_attr( $item->description ).'</span>' : '';

        $prepend = '<sturdy>';
        $append = '</sturdy>';

        if($depth != 0) {
            $description = $append = $prepend = "";
        }

        $temp_class = reset($item->courses);

        $item_output = $args->earlier than;
        $item_output .= '<a'. $attributes .'><img src="' . get_stylesheet_directory_uri() . '/photographs/icon-' . $temp_class .'.svg" alt="' . $item->title . '" class="mm-mm-icon">';
        $item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
        $item_output .= $description.$args->link_after;
        $item_output .= '</a>';
        $item_output .= $args->after;

        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $merchandise, $depth, $args );
    }
}

after which:

wp_nav_menu( array(
    'menu' => 'Mega Menu - Product - IDS - Left',
    'container' =>false,
    'menu_class' => 'nav',
    'echo' => true,
    'earlier than' => '',
    'after' => '',
    'link_before' => '',
    'link_after' => '',
    'depth' => 0,
    'walker' => new mandoe_menu_walker())
);

The menu outputs wonderful, however the <a> aspect will get output as <a href=" https:="" mandoe.native="" options="" "=""> as a substitute of <a href="https://mandoe.native/options/">

Assist appreciated.

EDIT

If I exploit $hyperlink = esc_attr($item->url); after which:

    $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
    $attributes .= ! empty( $item->goal )     ? ' goal="' . esc_attr( $item->goal     ) .'"' : '';
    $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
    $attributes .= ' href="' . $hyperlink .'"';

I obtain: <a href=" https:="" mandoe.native="" options="" "=""> nonetheless.

Why is esc_attr($item->url) not well-formed?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments