[wp-trac] [WordPress Trac] #61710: Introduce a new action hook for ajax requests for both logged-in and non-logged-in users

WordPress Trac noreply at wordpress.org
Fri Jul 19 11:21:08 UTC 2024


#61710: Introduce a new action hook for ajax requests for both logged-in and non-
logged-in users
-------------------------+-----------------------------
 Reporter:  nirajgirixd  |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 Currently, to handle AJAX requests for both logged-in and non-logged-in
 users in WordPress, developers need to define two separate action hooks:

 {{{#!php
 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.

 {{{#!php
 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.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/61710>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list