Search code examples
wordpresshookadmin-ajax

How to add/hook new function into admin-ajax.php permanent


I add function 'listing_companions_Ajax' into $core_actions_post of admin-ajax.php. But discover after WP update that function is not present anymore.

$core_actions_post = array('oembed-cache',...,'listing_companions_Ajax');

Question: how can I add / hook this new function listing_companions_Ajax to the file admin-ajax.php without missing it after an WP update?


Solution

  • Here is an example for ajax using on WordPress :

    This is the javascript.

         $.ajax({
            type: 'post',
            url: siteUrl.ajax_url + '?action=listing_companions_ajax',
            data: someDataHere,
            success: function (response) {
              // do something on success here
            }
     } );
    

    WordPress:

    add_action( 'wp_ajax_listing_companions_ajax', 'listing_companions_ajax' );
    add_action( 'wp_ajax_nopriv_listing_companions_ajax', 'listing_companions_ajax' );
    
    function listing_companions_ajax() {
     // do smth here
    }