dongmei9508 2019-07-27 13:45
浏览 48


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>

        $to      = '';
        $subject = 'XXX';
        $body = 'XXX';
        $headers = 'Content-type: text/html;charset=utf-8' . "
        $headers .= 'From: XXX <>' . "

        wp_mail( $to, $subject, $body, $headers );

        echo "<script type='text/javascript'>alert('Email Sent');</script>";
        submit_button('Invia notifica');
    ?>  <form action="" method="post">

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">
        $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; ?>

    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 商城生产日期批次库存问题
  • ¥15 esp8266控制共阳极wrgb灯板无法关闭所有led灯
  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题
  • ¥15 您好 这个API接口该怎么弄 网站搭建好了 API也有 现在就不知道该怎么填写API 不知道怎么用
  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle