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.

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

报告相同问题?

悬赏问题

  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器