Make WordPress Core

Opened 6 weeks ago

Last modified 5 weeks ago

#62763 new defect (bug)

get_calendar(): IDs may be duplicated

Reported by: wildworks's profile wildworks Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords:
Focuses: Cc:

Description

I discovered this while looking into the following Gutenberg issue: https://github.com/WordPress/gutenberg/issues/68451

The get_calendar() function applies the following two IDs to the generated HTML:

  • table element: wp-calendar
  • Today's date cell: today

These IDs cannot be changed, so the IDs will be duplicated if multiple calendars are output.

Using the wp_unique_id() function will help us avoid duplicate IDs, but there are themes or plugins that rely on existing IDs, so we may need to use a different approach.

Change History (4)

#1 @vineet2003
5 weeks ago

We want to make the IDs used in the get_calendar() function unique to avoid duplicates when multiple calendars are shown on the same page. We'll also allow developers to customize these IDs if needed.

Can we do something like this ?

// The get_calendar() function generates a calendar in HTML with specific IDs for the table and today's date cell.
// However, if multiple calendars are displayed on the same page, these IDs could be duplicated, causing issues.
// To prevent this, we use the wp_unique_id() function to create unique IDs for each calendar element.
// Additionally, we provide filters that allow developers to customize these IDs if needed.

// Generate unique IDs for the calendar elements
$calendar_table_id = apply_filters('get_calendar_table_id', 'wp-calendar-' . wp_unique_id());
$today_cell_id = apply_filters('get_calendar_today_id', 'today-' . wp_unique_id());

// Example of how these IDs might be used in the HTML
$calendar_html = '<table id="' . esc_attr($calendar_table_id) . '">';
$calendar_html .= '<tr><td id="' . esc_attr($today_cell_id) . '">Today</td></tr>';
$calendar_html .= '</table>';

return $calendar_html;

#2 @hbhalodia
5 weeks ago

Hi @vineet2003, I guess this is a straightforward approach, But it may have some implications.

For example, We need to find an approach that plugin/theme developers does not need to do extra thing like to add the filter, if ultimately they need to, then we can just use wp_unique_id without any additional filters, and ask plugin/theme developer to change their code to use the generated ID may be in JS. So we would not introduce any extra filter to the core codebase.

So, we need to find an approach that plugin/theme developer would not need to change/add something to their codebase, and we can achieve the desired results or, if ultimately they need to change, then we should use directly wp_unique_id() and ask them to use new ID.

Last edited 5 weeks ago by hbhalodia (previous) (diff)

#3 @hbhalodia
5 weeks ago

Also, @wildworks, If plugin or theme developer have used the IDs to add some of their logic, then probably they would not have use calendar ID to add some logic, instead they may have use classes for the logic.

We just need to check if calendar is use single time on page and if they have used the ID for some internal logic.

#4 @vineet2003
5 weeks ago

Hi @hbhalodia,

Thanks for your input! I understand the desire to keep things simple and avoid adding extra filters. However, I believe adding filters to customize the calendar IDs (as I suggested) would provide more flexibility in the long run. Some developers might rely on the default wp-calendar and today IDs for their JavaScript or CSS, and changing them without a way to customize could break their existing functionality.

Using wp_unique_id() directly is a great solution for preventing duplicates, but I think offering the option to customize the IDs via filters would give developers the control they need while avoiding potential conflicts.

Let me know your thoughts on striking a balance between simplicity and flexibility!

Thanks

Note: See TracTickets for help on using tickets.