admin管理员组文章数量:1391981
I'm adding for the first time a wordpress dashboard widget. I'm creating a simple form to give the user the ability to send support request directly from wp dashboard. I'm not sure if all the code is working, during a test, I've noticed that the form will not submit the mail message. Can anyone help me?
function uptheme_support_dashboard_widgets()
{
global $wp_meta_boxes;
wp_add_dashboard_widget('custom_help_widget', 'Premium Ticket System', 'custom_dashboard_help');
}
function custom_dashboard_help()
{
$current_theme = wp_get_theme( 'uptheme-panel' );
?>
<p><?php _e('Hi! you are using the custom theme '. esc_html($current_theme) ); ?></p>
<p><?php _e('Use the form to request support for your theme.'); ?></p>
<form method="POST">
<p><?php _e('Email'); ?></p>
<input type="text" class="widefat" name="email" id="email" placeholder="" />
<p><?php _e('Request type'); ?></p>
<select name="support_ticket_type">
<option value=""><?php _e('Support request'); ?></option>
<option value=""><?php _e('Modification request'); ?></option>
</select>
<p><?php _e('Message'); ?></p>
<textarea class="widefat" name="support_message"></textarea>
<input type="hidden" name="action" value="submit_support_ticket">
<?php wp_nonce_field( 'submit_support_ticket', 'support_ticket_hash' ); ?>
<button class="btn-primary" type="submit" class=""><?php _e('Invia'); ?></button>
</form>
<small><?php _e('Theme powered by'); ?><a href="#"><?php _e('theme author'); ?></a></small>
<?php
}
add_action('wp_dashboard_setup', 'uptheme_support_dashboard_widgets');
function _submit_support_ticket()
{
if( isset($_POST['support_ticket_hash']) || ! wp_verify_nonce( $_POST['support_ticket_hash'], 'submit_support_ticket' ) ){
//echo '';
exit;
}
$email = $_POST['email'];
$subject = $_POST['support_ticket_type'];
$message = $_POST['support_message'];
$to = '[email protected]';
$headers[] = "From: <$email>";
wp_mail( $to, $subject, $message, $headers );
}
add_action( 'admin_post_submit_support_ticket', '_submit_support_ticket' );
I'm adding for the first time a wordpress dashboard widget. I'm creating a simple form to give the user the ability to send support request directly from wp dashboard. I'm not sure if all the code is working, during a test, I've noticed that the form will not submit the mail message. Can anyone help me?
function uptheme_support_dashboard_widgets()
{
global $wp_meta_boxes;
wp_add_dashboard_widget('custom_help_widget', 'Premium Ticket System', 'custom_dashboard_help');
}
function custom_dashboard_help()
{
$current_theme = wp_get_theme( 'uptheme-panel' );
?>
<p><?php _e('Hi! you are using the custom theme '. esc_html($current_theme) ); ?></p>
<p><?php _e('Use the form to request support for your theme.'); ?></p>
<form method="POST">
<p><?php _e('Email'); ?></p>
<input type="text" class="widefat" name="email" id="email" placeholder="" />
<p><?php _e('Request type'); ?></p>
<select name="support_ticket_type">
<option value=""><?php _e('Support request'); ?></option>
<option value=""><?php _e('Modification request'); ?></option>
</select>
<p><?php _e('Message'); ?></p>
<textarea class="widefat" name="support_message"></textarea>
<input type="hidden" name="action" value="submit_support_ticket">
<?php wp_nonce_field( 'submit_support_ticket', 'support_ticket_hash' ); ?>
<button class="btn-primary" type="submit" class=""><?php _e('Invia'); ?></button>
</form>
<small><?php _e('Theme powered by'); ?><a href="#"><?php _e('theme author'); ?></a></small>
<?php
}
add_action('wp_dashboard_setup', 'uptheme_support_dashboard_widgets');
function _submit_support_ticket()
{
if( isset($_POST['support_ticket_hash']) || ! wp_verify_nonce( $_POST['support_ticket_hash'], 'submit_support_ticket' ) ){
//echo '';
exit;
}
$email = $_POST['email'];
$subject = $_POST['support_ticket_type'];
$message = $_POST['support_message'];
$to = '[email protected]';
$headers[] = "From: <$email>";
wp_mail( $to, $subject, $message, $headers );
}
add_action( 'admin_post_submit_support_ticket', '_submit_support_ticket' );
Share
Improve this question
asked Mar 7, 2020 at 12:37
sialfasialfa
32910 silver badges29 bronze badges
4
|
1 Answer
Reset to default 0There are three issues I see in your code:
Your form doesn't have an
action
set. You should add it and set the value toadmin-post.php
.<form method="POST" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
Because the function which processes the form and then sends the email is hooked to
admin_post_submit_support_ticket
(i.e.admin_post_<action>
) and that hook is fired only onwp-admin/admin-post.php
.In
_submit_support_ticket()
, theisset($_POST['support_ticket_hash'])
should be! isset($_POST['support_ticket_hash'])
because if not, the page would exit even if there was actually a valid nonce.if( ! isset($_POST['support_ticket_hash']) || ! wp_verify_nonce( $_POST['support_ticket_hash'], 'submit_support_ticket' ) ){ //echo ''; exit; }
The email subject is actually empty and that will not send the email. So make sure that your "Request type" drop-down has valid options (currently, the
option
'svalue
is empty).wp_mail( '[email protected]', '', 'Testing' ); // bad; subject is empty wp_mail( '[email protected]', 'Hi', 'Testing' ); // good; subject is good..
In fact, if you look at
wp_mail()
's docs, the subject is a required parameter. So once again, make sure the<option>
has a good subject, although it might be preferred to compose the subject in your PHP and not sent as-is as coming from the form.<p><?php _e('Request type'); ?></p> <select name="support_ticket_type"> <option value="Support request"><?php _e('Support request'); ?></option> <option value="Modification request"><?php _e('Modification request'); ?></option> </select>
And after the email is sent, you should send the user back to the dashboard page to prevent them from seeing a blank page. So after the wp_mail()
call, you could do:
wp_redirect( admin_url() );
exit;
Also, you should always sanitize user-supplied data. Never trust their input even if you actually trust the person...
本文标签: phpdashboard widget form not submit mails
版权声明:本文标题:php - dashboard widget form not submit mails 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744689046a2619878.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
action
? It should submit toadmin-post.php
.. – Sally CJ Commented Mar 7, 2020 at 12:49wp_ajax_
? – sialfa Commented Mar 7, 2020 at 12:51isset($_POST['support_ticket_hash'])
should be! isset($_POST['support_ticket_hash'])
. Otherwise, the page would exit even if there was actually a valid nonce. – Sally CJ Commented Mar 7, 2020 at 13:16wp_ajax_
, I'm not sure what you mean, but if you use that hook, then your AJAX request should be sent toadmin-ajax.php
. Theadmin_post_
hooks are for regular/non-AJAX form submissions, but their syntax is identical to thewp_ajax_
hooks. – Sally CJ Commented Mar 7, 2020 at 13:20