Php 8,函数.php的AJAX邮件表单不起作用

时间:2021-05-20 作者:para_bit

我有将消息发送到电子邮件的代码,它在PHP 7.2下工作,但在我们将版本更改为PHP 8后,它就不工作了。也许有人有想法怎么了?

作用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"
        });

    }
});
当我们使用debug时,请参见

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

1 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

这就是问题的原因:

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

}
You shouldn\'t nest function definitions! 将其移出函数,如下所示:

function true_add_ajaxform(){
    ...
}

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