在WordPress管理员用户表中添加向用户发送电子邮件的按钮

时间:2019-04-15 作者:Sadiq11111

我想在wordpress管理区的用户页面中为每个用户添加一个按钮,单击该按钮时,将向该特定用户发送电子邮件。

代码向表中的每个用户添加操作链接“发送验证电子邮件”。但由于某些原因,电子邮件只发送给第一行的用户。当我单击其他用户行上的“发送验证电子邮件”时,它不会发送给其他用户。

但奇怪的是,当我在开发网站上使用此代码时,电子邮件会发送给那里的每个用户。我在开发网站上只注册了大约6个用户,而我的主网站有200多个用户。我不确定注册用户的数量是否导致了问题,因为当单击某个用户所在行上的按钮时,电子邮件会发送给该用户。

我做错了什么导致了这个问题。有什么想法吗?

P、 我不是一个程序员,我在网上看到了这段代码,只想根据我的要求修改它。如果您能为我提供一个有效的解决方案,而不仅仅是一个评论,我将不胜感激。

// Adds "Send verification email" action to Users page
function send_verification_email_link($actions, $user_object) {
    if( ($_GET[\'action\'] == \'send_verification_email\') && ($_GET[\'user\'] == $user_object->user_email) ) {

    $my_logo = "<img src=\'https://example.com/\' alt=\'logo\' />";
    $sendto = $_GET[\'user\'];
    $subject = \'Your Example Account is not yet verified\';
    $headers = array(\'Content-Type: text/html; charset=UTF-8\');

        $message = \'<p style="text-align: center;">\'.$my_logo.\'</p>

        <div style="max-width: 600px; margin: 30px auto; padding: 15px; font-family: Roboto,Open Sans,Helvetica,Arial; border: 1px solid #e8e8e8; line-height: 1.6; text-align: center; background-color: white;">

        <div style="text-align: left;">
        <p>Hi \'.$user_object->first_name.\',</p>

        <p>Thank you for registering your business on Example.com. Your account is not yet verified because you have not provided the verification information or the information you provided is invalid. Please provide the necessary information below to get verified:</p>

        <p><strong>Private person:</strong></p>
        <ul>
        <li>Upload a profile photo.</li>
        <li>Upload an identification document.</li>
        </ul>

        <p><strong>Company:</strong></p>
        <ul>
        <li>Upload a profile photo.</li>
        <li>Upload an identification document.</li>
        <li>Provide your registered company name.</li>
        <li>Provide your company registration number.</li>
        </ul>

        <a style="text-align: center;" href="https://example.com/login/" rel="noopener noreferrer" target="_blank">Click here to login to your account.</a>

        <p>When you are logged in, click the settings icon on your profile page and click edit profile. Scroll to the verification section to provide the verification information.</p>

        <p>Please let us know if you have any questions.</p>

        <p>Kind regards,<br>Example Team</p>

        </div>        
        </div>

    wp_mail($sendto, $subject, $message, $headers);
    echo \'<div class="updated notice"><p>Success! The verification email has been sent to \' . $_GET[\'user\'] . \'.</p></div>\';
          }

    $actions[\'send_rejection\'] = "<a class=\'send_verification_email\' href=\'" . admin_url( "users.php?action=send_verification_email&amp;user=" . $user_object->user_email ) . "\'>" . __( \'Send Verification Email\' ) . "</a>";
    return $actions;
}
add_filter(\'user_row_actions\', \'send_verification_email_link\', 10, 2);

1 个回复
最合适的回答,由SO网友:butlerblog 整理而成

你的方法有缺陷。

您正在使用的过滤器挂钩-user_row_actions - 用于向用户行添加“悬停链接”。它在用户>所有用户屏幕中的每个用户行上运行。如果您使用过滤器只是添加链接,那么该部分本身就可以了但是,您正在使用过滤器来处理发送电子邮件的操作。

该过滤器在用户>所有用户(表视图中显示的默认用户数)的页面加载上运行20次,因此当您这样做时,最终会发送20封电子邮件(或视图中的用户数)。

所以这里有一条经验法则——过滤器是用来改变显示方式的。操作用于运行某种类型的进程。So use the filter to add the link, and an action to process the result if the link is clicked.

继续使用user_row_actions 筛选以添加链接。我建议使用动作钩,如admin_notices 检查您的链接是否已提交,然后对其进行处理,因为您无论如何都需要使用该挂钩来显示“成功”消息。这样,您就可以将这两个过程结合起来。这样,您对单击的链接的处理将只运行一次,而不是20次。

这是按照我上面所述修改的原始代码。我已经加入了一些评论来澄清它在做什么以及为什么。

// Adds "Send verification email" action to Users page
add_filter(\'user_row_actions\', \'send_verification_email_link\', 10, 2);
function send_verification_email_link($actions, $user_object) {
    $actions[\'send_rejection\'] = "<a class=\'send_verification_email\' href=\'" . admin_url( "users.php?action=send_verification_email&amp;user_id=" . $user_object->ID ) . "\'>" . __( \'Send Verification Email\' ) . "</a>";
    return $actions;
}


add_action( \'admin_notices\', \'send_verification_email\' );
function send_verification_email() {

    // Get the current screen so you only move forward if this is the users.php screen.
    $screen = get_current_screen();
    if ( \'users\' == $screen->id ) {

        // Make sure to check if the $_GET value is actually set before you compare the value, 
        // otherwise you may get errors if the page is loaded without it.
        if ( isset( $_GET[\'action\'] ) && \'send_verification_email\' == $_GET[\'action\'] ) {

            // Check that the email was passed too.
            $user_id = ( isset( $_GET[\'user_id\'] ) ) ? $_GET[\'user_id\'] : false;

            // Get the user info object info using the user ID (you\'ll need 
            // this for the email address and other data like "first_name" you use in the email message.
            $user_object = get_user_by( \'ID\', $user_id );

            // If you have a user with an email address.
            if ( $user_object->user_email ) {

                $my_logo = "<img src=\'https://example.com/\' alt=\'logo\' />";

                $subject = \'Your Example Account is not yet verified\';
                $headers = array(\'Content-Type: text/html; charset=UTF-8\');

                $message = \'<p style="text-align: center;">\'.$my_logo.\'</p>

                    <div style="max-width: 600px; margin: 30px auto; padding: 15px; font-family: Roboto,Open Sans,Helvetica,Arial; border: 1px solid #e8e8e8; line-height: 1.6; text-align: center; background-color: white;">

                    <div style="text-align: left;">
                    <p>Hi \'.$user_object->first_name.\',</p>

                    <p>Thank you for registering your business on Example.com. Your account is not yet verified because you have not provided the verification information or the information you provided is invalid. Please provide the necessary information below to get verified:</p>

                    <p><strong>Private person:</strong></p>
                    <ul>
                    <li>Upload a profile photo.</li>
                    <li>Upload an identification document.</li>
                    </ul>

                    <p><strong>Company:</strong></p>
                    <ul>
                    <li>Upload a profile photo.</li>
                    <li>Upload an identification document.</li>
                    <li>Provide your registered company name.</li>
                    <li>Provide your company registration number.</li>
                    </ul>

                    <a style="text-align: center;" href="https://example.com/login/" rel="noopener noreferrer" target="_blank">Click here to login to your account.</a>

                    <p>When you are logged in, click the settings icon on your profile page and click edit profile. Scroll to the verification section to provide the verification information.</p>

                    <p>Please let us know if you have any questions.</p>

                    <p>Kind regards,<br>Example Team</p>

                    </div>        
                    </div>\';

                // Send message.
                wp_mail( $user_object->user_email, $subject, $message, $headers );

                // Set up admin notice
                $class = \'updated notice\'; 
                $admin_notice = \'Success! The verification email has been sent to \' . $user_object->user_email;

            } else {

                // Set up admin notice for error state.
                $class = \'notice notice-error\';
                $admin_notice = "No email address was passed so no email was sent.";
            }

            // Display message.
            printf( \'<div class="%s"><p>%s</p></div>\', $class, $admin_notice );
        }
    }
}

相关推荐

WP-ADMIN ERR_CONNECTION_TIMED_OUT仅在某些网络上

好吧,两天以来我的WordPress博客出现了一个奇怪的问题。该网站在我的办公网络(Wi-Fi)上运行良好,我可以通过笔记本电脑或手机登录仪表板并发布内容。但是,当我回到家尝试登录时,wp admin页面会显示ERR\\u CONNECTION\\u TIMED\\u OUT error,或者有时会重定向到登录。php页面,并显示404未找到错误。我联系了我的主机,他们说服务器没有问题。奇怪的是,当我尝试在我的移动网络上登录网站时,这是一个不同的ISP,两天后我就面临着同样的问题。我清除了缓存,刷新了DN