dongshang6790 2016-07-01 10:40
浏览 19
已采纳

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

I'm building an options page in my WordPress plugin, I've got a form with to, from, subject & message with a save and send button.

The idea is you fill in the to, from, subject and message click save then click send to send the message. I'm having trouble trying to run the send function on button click.

I've checked out: Wordpress Plugin: Call function on button click in admin panel

Also checked out Is this the right way to execute some PHP in my plugin after a button click on an options page?

My code is as follows:

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>

So my question is: How would I run the sendMail() function when the send button is clicked in my form with the supplied values from the form ?

  • 写回答

1条回答 默认 最新

  • douzhong1907 2016-07-01 11:39
    关注

    You can check for post submission within your function:

    <?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);
        }
    }
    

    And on your form, remove the action value - keep it blank:

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

    Change you submit_button too so that it can post send value as a key as we are looking for $_POST['send'] to send the mail:

    <?php submit_button('Send', 'primary', 'send'); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败