1

I have code that send message to email, it works at PHP 7.2, but after we changed version to PHP 8 it didn't works. Maybe someone have ideas whats wrong?

Function.php

function true_add_ajaxform(){

$multiple_to_recipients = array(
    '[email protected]',
);

add_filter( 'wp_mail_content_type', 'set_html_content_type' );

$message = 'text: '.$_POST['page']."\n";

$message .= 'text: '.$_POST['name']."\n";

$message .= 'text: '.$_POST['phone']."\n";

wp_mail( $multiple_to_recipients, $_POST['nameForm'], $message);

remove_filter( 'wp_mail_content_type', 'set_html_content_type' );

function set_html_content_type() {
    return 'text/html';
}

}
 
add_action('wp_ajax_ajaxform', 'true_add_ajaxform'); 
add_action('wp_ajax_nopriv_ajaxform', 'true_add_ajaxform');

JS

var formType = '';
var page = document.location.href;
if (thParent.find('#actHidd .textAr').length > 0) {
    formType = 'author=' + thParent.find('#author').val() + '&comment=' + thParent.find('#actHidd .textAr').val() + '&idComment=' + jQuery('#commentCount').val() + '&rating=' + jQuery(".starRating input[type='radio']:checked").val() + '&action=ajaxcomments';
} else {
    formType = 'name=' + thParent.find('.callName').val() + '&phone=' + thParent.find('.callPhone').val() + '&nameForm=' + thParent.find('.callTitle').text() + '&page=' + page + '&action=ajaxform';
}

jQuery.ajax({
    type: 'POST',
    url: '/wp-admin/admin-ajax.php',
    data: formType,

    success: function(newComment) {
        jQuery('#submit').addClass('clack');
        // console.log('clack');
        console.log(newComment);
        //                    alert(newComment);


        if (jQuery(this).is('#ericSend')) {
            jQuery('#innerEric').css({
                "display": "none"
            });
            thParent.find('.final').css({
                "display": "block"
            });
        } else if (jQuery(this).is('#sendForm')) {
            jQuery('#actHidd').css({
                "display": "none"
            });
            thParent.find('.final').css({
                "display": "block"
            });
        }
        thParent.find('.hiddForm').css({
            "display": "none"
        });
        thParent.find('.final').css({
            "display": "block"
        });

    }
});

When we use debug see this

PHP Fatal error:  Uncaught TypeError: call_user_func_array(): Argument #1 ($function) must be a valid callback, function "set_html_content_type" not found or invalid function name in /public_html/wp-includes/class-wp-hook.php:292
Stack trace:
#0 /public_html/wp-includes/plugin.php(212): WP_Hook->apply_filters('text/plain', Array)
#1 /public_html/wp-includes/pluggable.php(469): apply_filters('wp_mail_content...', 'text/plain')
#2 /public_html/wp-content/themes/honestRepair/functions.php(1234): wp_mail(Array, '\xD0\x9E\xD1\x81\xD1\x82\xD0\xB0\xD0\xB2\xD0\xB8\xD1\x82\xD1...', '\xD0\xA1\xD1\x82\xD1\x80\xD0\xB0\xD0\xBD\xD0\xB8\xD1\x86\xD0...')
#3 /public_html/wp-includes/class-wp-hook.php(292): true_add_ajaxform('')
#4 /public_html/wp-includes/class-wp-hook.php(316): WP_Hook->apply_filters('', Array)
#5 /public_html/wp-includes/plugin.php(484): WP_Hook->do_action(Array)
#6 /public_html/wp-admin/admin-ajax.php(187): do_action('wp_ajax_ajaxfor...')
#7 {main}
  thrown in /home/p/progress55/public_html/wp-includes/class-wp-hook.php on line 292
7
  • have you considered using the modern REST API instead of the old admin ajax API? A REST API endpoint has a pretty URL and will try to tell you in english what went wrong with the AJAX request rather than failing silently Commented May 20, 2021 at 16:30
  • thank you for answer, yes we are think about it, but it fallback if this way doesn't work Commented May 20, 2021 at 17:07
  • 1
    I would consider abandoning admin AJAX in favour of the REST API. I also notice that you have defined a function inside another function, and that this was obscured because the code is not indented correctly. This is very bad, and likely causing problems on PHP v8, don't do that Commented May 20, 2021 at 20:15
  • thank you for answer, but debug says another information, we added that it say Commented May 21, 2021 at 4:59
  • It cannot find the function because it is defined inside another function, you need to move it outside and stop nesting php functions Commented May 21, 2021 at 8:30

1 Answer 1

0

This is the cause of your problem:

function true_add_ajaxform(){
    ...
    function set_html_content_type() {
        return 'text/html';
    }

}

You shouldn't nest function definitions! Move it out of the function like this:

function true_add_ajaxform(){
    ...
}

function set_html_content_type() {
    return 'text/html';
}
3
  • it very interesting because when we add //wp_mail( $multiple_to_recipients, $_POST['nameForm'], $message); our code work without errors and if you was reading debug information you can see errors about wp_mail function Commented May 21, 2021 at 9:05
  • sorry, you are absolutely right, its works, thank you for your help Commented May 21, 2021 at 10:27
  • that's because when wp_mail is commented out the hook does not run so the problem isn't triggered Commented May 21, 2021 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.