Using WordPress ‘navigation_widgets_format’ PHP filter

The navigation_widgets_format filter allows you to modify the HTML format of widgets with navigation links in your WordPress theme.

Usage

add_filter( 'navigation_widgets_format', 'your_custom_function' );

function your_custom_function( $format ) {
    // Your custom code here
    return $format;
}

Parameters

  • $format (string) – The type of markup to use in widgets with navigation links. Accepts ‘html5’, ‘xhtml’.

Examples

Change Navigation Widget Format to XHTML

add_filter( 'navigation_widgets_format', 'change_navigation_widgets_to_xhtml' );

function change_navigation_widgets_to_xhtml( $format ) {
    $format = 'xhtml';
    return $format;
}

This code changes the format of navigation widgets to XHTML.

Force HTML5 Format for Navigation Widgets

add_filter( 'navigation_widgets_format', 'force_html5_navigation_widgets' );

function force_html5_navigation_widgets( $format ) {
    $format = 'html5';
    return $format;
}

This code forces the format of navigation widgets to always use HTML5.

Conditionally Change Format Based on Post Type

add_filter( 'navigation_widgets_format', 'change_format_based_on_post_type' );

function change_format_based_on_post_type( $format ) {
    if ( is_singular( 'custom_post_type' ) ) {
        $format = 'xhtml';
    } else {
        $format = 'html5';
    }
    return $format;
}

This code changes the navigation widgets format to XHTML if the current post is a custom post type, otherwise, it uses HTML5.

Change Format Based on User Role

add_filter( 'navigation_widgets_format', 'change_format_based_on_user_role' );

function change_format_based_on_user_role( $format ) {
    if ( current_user_can( 'editor' ) ) {
        $format = 'xhtml';
    } else {
        $format = 'html5';
    }
    return $format;
}

This code changes the navigation widgets format to XHTML for users with the ‘editor’ role, and HTML5 for other users.

Change Format Based on Widget Area

add_filter( 'navigation_widgets_format', 'change_format_based_on_widget_area', 10, 2 );

function change_format_based_on_widget_area( $format, $args ) {
    if ( 'footer' == $args['id'] ) {
        $format = 'xhtml';
    } else {
        $format = 'html5';
    }
    return $format;
}

This code changes the navigation widgets format to XHTML if the widget area is the footer, otherwise, it uses HTML5.