繁体   English   中英

如何在按钮点击时发送wp_mail?

[英]How to send wp_mail on button click?

我正在我的WordPress插件中构建一个选项页面,我有一个包含to,from,subject和message的表单,带有一个保存和发送按钮。

想法是你填写to,from,subject和message点击保存然后点击发送发送消息。 我在按下单击按钮时尝试运行发送功能时遇到问题。

我已经签出: Wordpress插件:点击管理面板中的按钮点击呼叫功能

还检查了这是在按钮单击选项页面后在我的插件中执行某些PHP的正确方法吗?

我的代码如下:

add_action('admin_menu', 'custom_mail_menu');
function custom_mail_menu() {

//create new top-level menu
add_options_page('Custom Mail Settings', 'Custom Mail Settings', 'administrator', __FILE__, 'custom_mail_settings_page');

//call register settings function
add_action( 'admin_init', 'custom_mail_settings' );
}

function custom_mail_settings() {
//register our settings
register_setting( 'custom-mail-settings-group-15167', 'custom_mail_to' );
register_setting( 'custom-mail-settings-group-15167', 'custom_mail_from' );
register_setting( 'custom-mail-settings-group-15167', 'custom_mail_sub' );
register_setting( 'custom-mail-settings-group-15167', 'custom_mail_message' );
}

function sendMail() {
$sendto = esc_attr( get_option('custom_mail_to') );
$sendfrom =  esc_attr( get_option('custom_mail_from') );
$sendsub = esc_attr( get_option('custom_mail_sub') );
$sendmess = esc_attr( get_option('custom_mail_message') );
$headers = "From: Wordpress <" . $sendfrom . ">";
wp_mail($sendto, $sendsub, $sendmess, $headers);

}

function custom_mail_settings_page() {

if (!current_user_can('manage_options'))  {
    wp_die( __('You do not have sufficient pilchards to access this page.')    );
}


?>
<div class="wrap">
<h2>Custom Mail Settings</h2>

<form method="post" action="options.php">
    <?php settings_fields( 'custom-mail-settings-group-15167' ); ?>
    <?php do_settings_sections( 'custom-mail-settings-group-15167' ); ?>
    <table class="form-table" style="width: 50%">
        <tr valign="top">
            <th scope="row">To</th>
            <td>
                <input style="width: 100%" type="text" name="custom_mail_to" value="<?php echo esc_attr( get_option('custom_mail_to') ); ?>" />
            </td>
        </tr>

        <tr valign="top">
            <th scope="row">From</th>
            <td>
                <input style="width: 100%" type="text" name="custom_mail_from" value="<?php echo esc_attr( get_option('custom_mail_from') ); ?>" />
            </td>
        </tr>

        <tr valign="top">
            <th scope="row">Subject</th>
            <td>
                <input style="width: 100%" type="text" name="custom_mail_sub" value="<?php echo esc_attr( get_option('custom_mail_sub') ); ?>" />
            </td>
        </tr>
        <tr valign="top">
        <th scope="row">Message</th>
            <!-- <td><input type="text" name="custom_mail_message" value="?php echo esc_attr( get_option('custom_mail_message') ); ?>" /></td> /-->
        <td>
            <textarea style="text-align: left;" name="custom_mail_message" rows="10" cols="62"><?php echo esc_attr( get_option('custom_mail_message') ); ?></textarea>
        </td>
        </tr>
        <tr>
            <td></td>
            <td><?php submit_button('Save'); ?><td><?php submit_button('send'); ?></td></td>

        </tr>
    </table>
  </form>
</div>

所以我的问题是:当我在表单中使用表单中提供的值单击发送按钮时,如何运行sendMail()函数?

您可以在功能中检查帖子提交:

<?php
function sendMail() {
    if($_POST['send']) {
        $sendto = esc_attr( get_option('custom_mail_to') );
        $sendfrom =  esc_attr( get_option('custom_mail_from') );
        $sendsub = esc_attr( get_option('custom_mail_sub') );
        $sendmess = esc_attr( get_option('custom_mail_message') );
        $headers = "From: Wordpress <" . $sendfrom . ">";
        wp_mail($sendto, $sendsub, $sendmess, $headers);
    }
}

在您的表单上,删除操作值 - 保持空白:

<form method="post" action="">

更改你的submit_button以便它可以send值作为键,因为我们正在寻找$_POST['send']来发送邮件:

<?php submit_button('Send', 'primary', 'send'); ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM