Make WordPress Core

Opened 4 months ago

Last modified 3 months 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: dev-feedback has-patch
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 (2)

#1 @nirajgirixd
4 months ago

  • Keywords dev-feedback added
  • Version set to 6.6

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


3 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

Note: See TracTickets for help on using tickets.