Php 如何在点击按钮时发送wp_邮件?

Php 如何在点击按钮时发送wp_邮件?,php,wordpress,Php,Wordpress,我正在我的WordPress插件中构建一个选项页面,我有一个带有to、from、subject和message的表单,还有一个保存和发送按钮 您可以填写收件人、发件人、主题和消息,然后单击“保存”,然后单击“发送”发送消息。我尝试在单击按钮时运行发送功能时遇到问题 我已经退房了: 也签了 我的代码如下: add_action('admin_menu', 'custom_mail_menu'); function custom_mail_menu() { //create new top-lev

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

您可以填写收件人、发件人、主题和消息,然后单击“保存”,然后单击“发送”发送消息。我尝试在单击按钮时运行发送功能时遇到问题

我已经退房了:

也签了

我的代码如下:

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>
添加操作(“管理菜单”、“自定义邮件菜单”);
功能自定义邮件菜单(){
//创建新的顶级菜单
添加选项页面(“自定义邮件设置”、“自定义邮件设置”、“管理员”、“文件”、“自定义邮件设置页面”);
//呼叫寄存器设置功能
添加操作(“管理初始化”、“自定义邮件设置”);
}
功能自定义邮件设置(){
//注册我们的设置
注册设置('custom-mail-settings-group-15167','custom-mail-to');
注册设置('custom-mail-settings-group-15167','custom-mail-from');
注册设置('custom-mail-settings-group-15167','custom-mail-sub');
注册设置('custom-mail-settings-group-15167',custom_UMail\u message');
}
函数sendMail(){
$sendto=esc_attr(获取选项(“自定义邮件收件人”);
$sendfrom=esc_attr(获取选项(“自定义邮件发件人”);
$sendsub=esc_attr(获取选项(“自定义邮件”);
$sendmass=esc_attr(获取选项(“自定义邮件消息”);
$headers=“From:Wordpress”;
wp_邮件($sendto、$sendsub、$sendmesss、$headers);
}
功能自定义\邮件\设置\页面(){
如果(!当前用户可以('manage_options')){
wp_die(_uu('您没有足够的盗贼来访问此页面');
}
?>
自定义邮件设置
到

您可以在您的职能范围内检查post提交:

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

如何保存字段并同时选择发送,这样它将有一个保存按钮和一个发送按钮。使用if语句更改表单操作?是的。使用if语句执行不同的操作。@mrjosh
<form method="post" action="">
<?php submit_button('Send', 'primary', 'send'); ?>