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 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案