dongmei9508 2019-07-27 13:45
浏览 48
已采纳

从profile.php页面发送带有wp_mail的电子邮件

I need to add a button in the profile.php pages for every user in the back-end, in order to send an email via php. I managed to do it correctly when I click the button in the profile user I'm logged into, but when I tried to do in other users, I got the wordpress error "Invalid user ID" I used the hooks show_user_profile and edit_user_profile correctly but I think there is some issue with the POST method. After this issue will be solved, I need to change dynamically the email with the user's email I'm into.

    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' . "
";
        $headers .= 'From: XXX <XXX@XXX.it>' . "
";   

        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');
  • 写回答

1条回答 默认 最新

  • doulan1073 2019-07-27 23:07
    关注

    It only works when you edit your own profile because you are missing two parameters in your form when you edit someone else's (not the logged in user)'s profile :

    These 2 fields: user_id and _wp_http_referer

    You can add them to your form like this :

    <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>
    

    However, i noticed that this code adds a form inside the default profile edit form and i think this will get you in trouble.

    I'd suggest you to use a simple button linked to an ajax call.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵