Custom Widget to overide WP default widget

Hello there.

I have had a look all over the forum and could not see anything that would help me fix my issue.

I am trying to override one of the default wordpress widgets. Specifically the custom menu widget. A bit of background.

I have had no issues creating a new sidebar by adding it to setup.php and then placing the default cusom menu.nav widget into my sidebar. It shows fine.

However it ads styling to the widget which I am trying to get rid of. I have specified some:

'before_widget' => '<li class="footer-sitemap-section">',
        'after_widget'  => '</li>',
        'before_title'  => '<p>',
        'after_title'   => '</p>'

But then the custom menu/nav ads it own into the ul specifically into the li & a tag which i want to get rid of.

This is what its adding:

<li class="nav-item menu-item menu-application"><a class="nav-link" href="#">Application</a></li>

and this is how i would like it:

<li><a href="#">Application</a></li>

I want to get rid of all the classes in that li and a tag. I know this is a WordPress thing So i created my custom menu widget to override the default WordPress one:

<?php

namespace App;

/**
 * Navigation Menu widget class
 *
 * @since 3.0.0
 */
 class Updated_WP_Nav_Menu_Widget extends WP_Widget {

    function __construct() {
        $widget_ops = array( 'description' => __('Add a custom menu to your sidebar TEST.') );
        parent::__construct( 'nav_menu', __('Custom Menu TEST'), $widget_ops );
    }

    function widget($args, $instance) {
        // Get menu
        $nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;

        if ( !$nav_menu )
            return;

        /** This filter is documented in wp-includes/default-widgets.php */
        $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );

        echo $args['before_widget'];

        if ( !empty($instance['title']) )
            echo $args['before_title'] . $instance['title'] . $args['after_title'];

        wp_nav_menu( array( 'fallback_cb' => '', 'menu' => $nav_menu ) );

        echo $args['after_widget'];
    }

    function update( $new_instance, $old_instance ) {
        $instance['title'] = strip_tags( stripslashes($new_instance['title']) );
        $instance['nav_menu'] = (int) $new_instance['nav_menu'];
        return $instance;
    }

    function form( $instance ) {
        $title = isset( $instance['title'] ) ? $instance['title'] : '';
        $nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';

        // Get menus
        $menus = wp_get_nav_menus( array( 'orderby' => 'name' ) );

        // If no menus exists, direct the user to go and create some.
        if ( !$menus ) {
            echo '<p>'. sprintf( __('No menus have been created yet. <a href="%s">Create some</a>.'), admin_url('nav-menus.php') ) .'</p>';
            return;
        }
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:') ?></label>
            <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('nav_menu'); ?>"><?php _e('Select Menu:'); ?></label>
            <select id="<?php echo $this->get_field_id('nav_menu'); ?>" name="<?php echo $this->get_field_name('nav_menu'); ?>">
                <option value="0"><?php _e( '&mdash; Select &mdash;' ) ?></option>
        <?php
            foreach ( $menus as $menu ) {
                echo '<option value="' . $menu->term_id . '"'
                    . selected( $nav_menu, $menu->term_id, false )
                    . '>'. esc_html( $menu->name ) . '</option>';
            }
        ?>
            </select>
        </p>
        <?php
    }
}

add_action('widgets_init', create_function('', 'return register_widget("Updated_WP_Nav_Menu_Widget");'));

I then added that to my resources/funtions file speifcally here:

['helpers', 'setup', 'filters', 'admin', 'walker', 'customwidgetmenu']);

When i then go to the front-end OR back-end I am greeted with:

Fatal error: Class 'App\WP_Widget' not found in /srv/www/example1.com/current/web/app/themes/TEST/app/customwidgetmenu.php on line 10
Call Stack
#	Time	Memory	Function	Location
1	0.2145	390832	{main}( )	.../index.php:0
2	0.2309	392784	require( '/srv/www/example1.com/current/web/wp/wp-blog-header.php' )	.../index.php:5
3	0.2473	405928	require_once( '/srv/www/example1.com/current/web/wp/wp-load.php' )	.../wp-blog-header.php:13
4	0.2747	408072	require_once( '/srv/www/example1.com/current/web/wp-config.php' )	.../wp-load.php:42
5	0.6407	658752	require_once( '/srv/www/example1.com/current/web/wp/wp-settings.php' )	.../wp-config.php:9
6	5.2827	20205768	include( '/srv/www/example1.com/current/web/app/themes/TEST/resources/functions.php' )	.../wp-settings.php:426
7	5.6830	20588264	array_map ( )	.../functions.php:61
8	6.4494	21020424	{closure:/srv/www/example1.com/current/web/app/themes/TEST/resources/functions.php:56-61}( )	.../functions.php:61
9	6.4494	21020480	locate_template( )	.../functions.php:58
10	6.4712	21020608	load_template( )	.../template.php:647
11	6.5167	21040136	require_once( '/srv/www/example1.com/current/web/app/themes/TEST/app/customwidgetmenu.php' )	.../template.php:688

I know its sayong that the issue lies in line 10 but not sure what that is. I thought i referenced the app namespace correctly.

Sorry moved from an older version of sage (roots) theme and this one is a bit over my head.

Can any one point out what I am doing wrong? OR a better way to accomplish what I am trying to do?

Thanks in advanceā€¦

WP_Widget does not exist in the App namespace, so you need to ā€œget backā€ to the global namespace when you extend it: class Updated_WP_Nav_Menu_Widget extends \WP_Widget {

2 Likes

sorry, what would the global app namespace be? learning as I goā€¦

Precisely what @alwaysblank said: put a backslash in front of WP_Widget so that it reads \WP_Widget

Thank you very much. So simple :):grinning: