Site logo

Archived topic

Turn off Secondary Menu on Mobile without relying on css

19 replies · Started by David on November 19, 2020

Viewing posts 1–15 of 20

How can I turn off Secondary Menu on Mobile without relying on css.

Why?

Because on desktop Google PageSpeed Insights drops my score from 100 to 93 for a Cumulative Layout Shift (CLS) greater than 1. While the mobile version still scores 100.

Hi there,

Can you give this PHP snippet a shot?

add_filter( 'option_generate_secondary_nav_settings', function( $settings ) {
    if ( wp_is_mobile() ) {
        $options['secondary_nav_position_setting'] = '';
    }

    return $settings;
} );

Adding PHP: https://docs.generatepress.com/article/adding-php/

Leo,

I have three distinct menus:

Mobile Menu (using the code below)
Primary Menu
Secondary Menu

Here is the code I am using:

add_action( 'init', function() {
    register_nav_menu( 'mobile-menu', __( 'Mobile Menu' ) );
} );
add_filter( 'generate_mobile_header_theme_location', function() {
    return 'mobile-menu';
} );
add_filter( 'option_generate_secondary_nav_settings', function( $settings ) {
    if ( wp_is_mobile() ) {
        $options['option_generate_secondary_nav_settings'] = '';
    }
    return $settings;
} );

I have 0 for the Cumulative Layout Shift for mobile PageSpeed Insights.
On Mobile I have two menus showing and I only want one.
Hiding using css is not an option.

I have above 1 for the Cumulative Layout Shift for desktop PageSpeed Insights (which is a red flag) whenever I use the css to display:none for secondary nav.

The code above does not remove the secondary nav as hoped.

Is there anything else to try?

Thank you for your time.

Dave

The idea situation to me would be to use php to have

Desktop: Include Primary and secondary menus
Mobile: Include only the mobile menu.

Using css to hide and show menu creates Cumulative Layout Shifts at the top of the webpage which Google marks with a red flag.

where is the code?

I tried this but it doesn't work

even if I remove the wp_is_mobile() it doesn't work

add_filter( 'option_generate_secondary_nav_settings', function( $settings ) {
//if ( wp_is_mobile() ) {
$options['option_generate_secondary_nav_settings'] = '';
//}
return $settings;
} );

Strange.

I just tested this one and it works to remove the secondary navigation:

add_filter( 'option_generate_secondary_nav_settings','lh_remove_sec_nav' );
function lh_remove_sec_nav( $options ) {
    $options[ 'secondary_nav_position_setting' ] = '';
    
    return $options;
}

Can you test it on your end?

If so what about this one?

add_filter( 'option_generate_secondary_nav_settings','lh_remove_sec_nav' );
function lh_remove_sec_nav( $options ) {
    if ( wp_is_mobile() ) {
        $options[ 'secondary_nav_position_setting' ] = '';
    }
    return $options;
}

When I use this code:

add_filter( 'option_generate_secondary_nav_settings','lh_remove_sec_nav' );
function lh_remove_sec_nav( $options ) {
	$options[ 'secondary_nav_position_setting' ] = '';
	return $options;
}

There is no secondary menu. However there is still a lot of extra css associated with the secondary menu. How can I remove that as well?

When I use this code:

add_filter( 'option_generate_secondary_nav_settings','lh_remove_sec_nav' );
function lh_remove_sec_nav( $options ) {
    if ( wp_is_mobile() ) {
        $options[ 'secondary_nav_position_setting' ] = '';
    }
    return $options;
}

The secondary nav is included on BOTH.

I am using code snippets.

Hmm I'm not sure if there is a way to make this work with wp_is_mobile() but have forwarded to Tom and see if he has an idea :)

OK, so I use WP Rocket. There is an option of "Separate cache files for mobile devices".

Leo, it seems the above code does work, when I am logged in (no caching) but does not work when I view a cached page.

Because I usually leave this option above as unchecked.

Could this be the reason?

wp_is_mobile is a terrible idea.

It's not a good idea to cloak your website to impress Google.

Mathieu: what do you mean?

Well, you don't want to output a different page for mobile and desktop, it justs confuse the robots.

That's what CSS&JS is for. Not sure why you don't want CSS.

This archived topic is closed to new replies.