Mak*_*ijs 5 php wordpress plugins
如何让 PHP 在 else 语句之后显示确切的错误,而不是回显文本?
这是我使用的代码,我想知道发送邮件后到底出了什么问题:
function deliver_mail() {
// if the submit button is clicked, send the email
if ( isset( $_POST['cf-submitted'] ) ) {
// sanitize form values
$name = sanitize_text_field( $_POST["cf-name"] );
$email = sanitize_email( $_POST["cf-email"] );
$subject = sanitize_text_field( $_POST["cf-subject"] );
$message = esc_textarea( $_POST["cf-message"] );
// get the blog administrator's email address
$to = get_option( 'admin_email' );
$headers = "From: $name <$email>" . "\r\n";
// If email has been process for sending, display a success message
if ( wp_mail( $to, $subject, $message, $headers ) ) {
echo '<div>';
echo '<p>You have sent the mail!</p>';
echo '</div>';
} else {
echo 'error occurred';
}
}
Run Code Online (Sandbox Code Playgroud)
Ray*_*y A 14
看看这个:
https://developer.wordpress.org/reference/hooks/wp_mail_failed/
使用这个简单的方法调试 wp_mail() 会容易得多。它会显示比 Wordpress 默认情况下更有用的错误消息(原始 phpmailer 错误)。只需添加此函数即可显示真正的 wp_mail() 错误。但仅将其用于调试。
// show wp_mail() errors
add_action( 'wp_mail_failed', 'onMailError', 10, 1 );
function onMailError( $wp_error ) {
echo "<pre>";
print_r($wp_error);
echo "</pre>";
}
Run Code Online (Sandbox Code Playgroud)