我需要在配置文件.php页面中为后端中的每个用户添加一个按钮,以便通过php发送电子邮件。当我单击我登录的配置文件用户中的按钮时,我设法正确执行此操作,但是当我尝试在其他用户中执行此操作时,我收到wordpress错误"无效的用户ID"我show_user_profile正确地使用了钩子并edit_user_profile,但我认为 POST 方法存在一些问题。解决此问题后,我需要使用我所在的用户电子邮件动态更改电子邮件。
function fileEmailNotification(WP_User $user) {
?>
<h2>Invia notifica file</h2>
<?php
if(isset($_POST['submit']))
{
$to = 'XXX@email.it';
$subject = 'XXX';
$body = 'XXX';
$headers = 'Content-type: text/html;charset=utf-8' . "rn";
$headers .= 'From: XXX <XXX@XXX.it>' . "rn";
wp_mail( $to, $subject, $body, $headers );
echo "<script type='text/javascript'>alert('Email Sent');</script>";
}
submit_button('Invia notifica');
?> <form action="" method="post">
</form>
<?php
}
add_action('show_user_profile', 'fileEmailNotification');
add_action('edit_user_profile', 'fileEmailNotification');
它仅在您编辑自己的个人资料时有效,因为当您编辑其他人(不是登录用户(的个人资料时,表单中缺少两个参数:
这两个字段:user_id和_wp_http_referer
您可以像这样将它们添加到表单中:
<form action="" method="POST">
<?php
$logged_in_user_id = get_current_user_id();
if ($logged_in_user_id != $user->ID) : ?>
<input type="hidden" name="user_id" value="<?= $user->ID ?>"
<?= wp_referer_field(); ?>
<?php endif; ?>
</form>
但是,我注意到此代码在默认配置文件编辑表单中添加了一个表单,我认为这会给您带来麻烦。
我建议您使用链接到 ajax 调用的简单按钮。