WordPress.org

Make WordPress Core

Opened 11 months ago

Last modified 5 months ago

#38636 new enhancement

Allow data attributes to be added to WP Admin Bar menu items

Reported by: keraweb Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Toolbar Keywords: has-patch dev-feedback
Focuses: accessibility Cc:

Description

It is currently not possible to add your own parameters to WP Admin Bar.
For improved flexibility with javascript I think this could be a good feature.

Example:

<?php
$wp_admin_bar->add_node(
        'id'     => '',
        'parent' => '',
        'title'  => '',
        'href'   => '',
        'meta'   => array(
                'title' => '',
                'data'  => array(
                        'test' => 'data-test content'
                )
        ),
) );

Example code for class-wp-admin-bar.php (L 497 - 533)

<?php
if ( ! empty( $node->meta['data'] ) && ! is_array( $node->meta['data'] ) ) {
        $node->meta['data'] = array( 'data' => (string) $node->meta['data'] );
}
?>

<li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $menuclass; ?>><?php
        if ( $has_link ):
                ?><a class="ab-item"<?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php
                        if ( ! empty( $node->meta['onclick'] ) ) :
                                ?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php
                        endif;
                if ( ! empty( $node->meta['target'] ) ) :
                        ?> target="<?php echo esc_attr( $node->meta['target'] ); ?>"<?php
                endif;
                if ( ! empty( $node->meta['title'] ) ) :
                        ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
                endif;
                if ( ! empty( $node->meta['rel'] ) ) :
                        ?> rel="<?php echo esc_attr( $node->meta['rel'] ); ?>"<?php
                endif;
                if ( ! empty( $node->meta['lang'] ) ) :
                        ?> lang="<?php echo esc_attr( $node->meta['lang'] ); ?>"<?php
                endif;
                if ( ! empty( $node->meta['dir'] ) ) :
                        ?> dir="<?php echo esc_attr( $node->meta['dir'] ); ?>"<?php
                endif;
                if ( ! empty( $node->meta['data'] ) ) :
                        foreach ( $node->meta['data'] as $attr => $data ) :
                        ?> data-<?php echo $attr; ?>="<?php echo esc_attr( $data ); ?>"<?php
                        endforeach;
                endif;
                ?>><?php
        else:
                ?><div class="ab-item ab-empty-item"<?php echo $aria_attributes;
                if ( ! empty( $node->meta['title'] ) ) :
                        ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
                endif;
                if ( ! empty( $node->meta['lang'] ) ) :
                        ?> lang="<?php echo esc_attr( $node->meta['lang'] ); ?>"<?php
                endif;
                if ( ! empty( $node->meta['dir'] ) ) :
                        ?> dir="<?php echo esc_attr( $node->meta['dir'] ); ?>"<?php
                endif;
                if ( ! empty( $node->meta['data'] ) ) :
                        foreach ( $node->meta['data'] as $attr => $data ) :
                                ?> data-<?php echo $attr; ?>="<?php echo esc_attr( $data ); ?>"<?php
                        endforeach;
                endif;
                ?>><?php
        endif;

Attachments (2)

38636.patch (1.4 KB) - added by keraweb 11 months ago.
Allow passing data parameters to WP Admin Bar
38636.1.patch (4.0 KB) - added by pbiron 5 months ago.
the inline docs for WP_Admin_Bar::add_node() need some work

Download all attachments as: .zip

Change History (6)

@keraweb
11 months ago

Allow passing data parameters to WP Admin Bar

#1 @keraweb
11 months ago

  • Keywords has-patch dev-feedback added

#2 @afercia
11 months ago

  • Milestone changed from Awaiting Review to Future Release
  • Summary changed from Allow passing data parameters to WP Admin Bar to Allow data attributes to be added to WP Admin Bar menu items
  • Version trunk deleted

Makes sense to me, worth considering in the next release cycle. Changing a bit the summary to clarify it's about data attributes (yes they're used to pass parameters :) just sounds a bit more clear to me).

#3 @pbiron
5 months ago

  • Focuses accessibility added

I really like this idea, but think a more general solution that would arbitrary HTML attributes would be even better (thus loosening the paternalistic nature of the current code), ala

$wp_admin_bar->add_node(
	'id'     => '',
	'parent' => '',
	'title'  => '',
	'href'   => 'https://nl.wikipedia.org/wiki/Nederland',
	'meta'   => array(
		'type' => 'text/html',
		'hreflang' => 'nl',
		'data-test' => 'data-test content'
		'onmousedown' => 'some_js_func()',
		'aria-describedby' => 'some_html_id',
                ),
        ),
) );

Related: #40359, #40666

@pbiron
5 months ago

the inline docs for WP_Admin_Bar::add_node() need some work

#4 @keraweb
5 months ago

@pbiron
Nice! That sounds even better!

Note: See TracTickets for help on using tickets.