• Resolved Opariti

    (@opariti)


    Hi,
    I want to override a theme function handling an AJAX call, which that is NOT present in the functions.php file but in a file inside a directory.
    Here it is:

    Parent folder:
    ~parent_theme/functions/ajax.php – here I have a function called ‘parent_ajax_send_email
    The function is attached to the event wp_ajax_nopriv_parent_send_email, built in the wp-admin/admin-ajax.php file, and run with the do_action( ‘wp_ajax_nopriv_’ . $_REQUEST[‘action’] ).

    Child folder:
    I have copied the parent file into the mirror location, i.e. child/functions/ajax.php file, changed the function name to ‘child_ajax_send_email‘ then added the following in the functions.php file:

    // Replaces the parent theme ajax send email function
    function replace_parent_ajax_send_email() {
    	remove_action('wp_ajax_parent_send_email', 'parent_ajax_send_email');
    	remove_action('wp_ajax_nopriv_parent_send_email', 'parent_ajax_send_email');
    
    	//add_action('wp_ajax_child_send_email', 'child_ajax_send_email');
    	//add_action('wp_ajax_nopriv_child_send_email', 'child_ajax_send_email');
    }
    // Call 'remove_parent_ajax_send_email' during WP initialization
    add_action('init','replace_parent_ajax_send_email');

    NOTE: I have also tried with this (with same result):
    add_action('after_setup_theme','replace_parent_ajax_send_email');

    Of course, I have changed in the JavaScript calling function the name of the action (which works because, I’ve checked, it goes to wp-admin/admin-ajax.php.

    What happens is that the call goes with the right action request (modified to the child’s one) to wp-admin/admin-ajax.php, but the function do_action( ‘wp_ajax_nopriv_’ . $_REQUEST[‘action’] ) there returns ‘null‘, showing that the replacing function has not been registered with WP.

    I believe there is a wrong new function registration with WP (because is not in the child’s root directory), but no idea how to solve it without touching the parent’s code.

Viewing 1 replies (of 1 total)
  • Thread Starter Opariti

    (@opariti)

    Solved.
    All above is correct. I just had to include the /functions/ajax.php file into the child theme main functions.php file (in the theme child root directory)

Viewing 1 replies (of 1 total)
  • The topic ‘Override theme function in child’ is closed to new replies.