Make WordPress Core

Opened 12 months ago

Last modified 3 weeks ago

#61710 new enhancement

Introduce a new action hook for ajax requests for both logged-in and non-logged-in users

Reported by: nirajgirixd's profile nirajgirixd Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.6
Component: General Keywords: has-patch dev-feedback
Focuses: Cc:

Description

Currently, to handle AJAX requests for both logged-in and non-logged-in users in WordPress, developers need to define two separate action hooks:

add_action('wp_ajax_action_name', 'function_name'); // For logged-in users
add_action('wp_ajax_nopriv_action_name', 'function_name'); // For non-logged-in users

This can be repetitive and cumbersome, especially when dealing with multiple AJAX actions. To simplify this process, I propose introducing a new action hook that applies to both logged-in and non-logged-in users.

Proposed Solution:

Introduce a new hook wp_ajax_all_$action that combines both wp_ajax_$action and wp_ajax_nopriv_$action hooks, allowing developers to define a single action for both user states.

add_action('wp_ajax_all_action_name', 'function_name'); // For all users

Benefits:

  • Simplifies the code for handling AJAX requests.
  • Reduces redundancy and potential errors in defining multiple hooks.
  • Improves developer experience and code maintainability.

Change History (6)

#1 @nirajgirixd
12 months ago

  • Keywords dev-feedback added
  • Version set to 6.6

This ticket was mentioned in PR #7072 on WordPress/wordpress-develop by @nirajgirixd.


12 months ago
#2

  • Keywords has-patch added

## Description

This PR introduces a unified approach to handling AJAX actions in WordPress for both logged-in and non-logged-in users.

## Changes Made

  • Introduced the wp_ajax_all_{$action} hook to allow for a single AJAX action handler that works regardless of user authentication status.

## Why the New Hook?

  • Simplifies the code for handling AJAX requests.
  • Reduces redundancy and potential errors in defining multiple hooks.
  • Improves developer experience and code maintainability.

## Trac ticket: https://core.trac.wordpress.org/ticket/61710

#3 @nirajgirixd
4 weeks ago

The PR has already been approved — could someone please review it and guide me on the next steps?

cc: @SergeyBiryukov @johnbillion @nacin @jorbin @audrasjb @DrewAPicture @swissspidy @azaozz 

#4 @johnbillion
4 weeks ago

  • Keywords close added; dev-feedback removed

Thanks for the ticket and PR @nirajgirixd. I must say I'm not too excited about this new action, I think it might be more confusing for developers since it introduces an extra ajax hook which is functionally no different from using the two existing ones.

The wp_ajax_nopriv_{action} hook has the advantage of explicitly having nopriv in its name which helps developers recognise that this action will fire for non privileged users. wp_ajax_all_{action} does not have this.

Thanks again for the contribution but I think this should be closed.

#5 @nirajgirixd
4 weeks ago

Thank you for the review and feedback @johnbillion

I get the concern about the wp_ajax_all_{action} hook possibly being confusing, especially since it doesn’t explicitly mention nopriv like the existing ones do. However, I’d also like to share why I still believe this could be a helpful addition to the core:

  • Reduced Boilerplate: Requiring developers to register the same callback twice—once for wp_ajax_{action} and once for wp_ajax_nopriv_{action} adds unnecessary duplication, especially in projects handling many shared AJAX endpoints.
  • Cleaner Code, Fewer Mistakes: Combining both into a single wp_ajax_all_{action} hook can help reduce errors (e.g., forgetting to register both hooks or misnaming one), improving maintainability and clarity.
  • Potential Performance Benefit: While minor, consolidating the hook registration can reduce the number of callbacks processed during AJAX initialization. For sites with many AJAX actions, this small improvement could contribute to better performance—especially in high-traffic or API-heavy environments.
  • Optional and Backward-Compatible: This hook is not meant to replace the existing ones but to offer a more ergonomic alternative for cases where the same callback applies to all users. Developers who prefer explicit user-type distinction can continue to use the existing wp_ajax_ and wp_ajax_nopriv_ hooks as before.

That said, I completely respect if this doesn't align with the current vision for core. However, if there's openness to re-evaluating the naming or implementation to make the intention clearer, I’d be more than happy to iterate on the PR based on your guidance.

Thanks again for taking the time to review and share your thoughts.

Last edited 4 weeks ago by nirajgirixd (previous) (diff)

#6 @nirajgirixd
3 weeks ago

  • Keywords dev-feedback added; close removed
Note: See TracTickets for help on using tickets.