mardi 24 mars 2015

admin_post_nopriv did not processing submited form


I am creating WordPress widget to generate a form. When user submit the form so it will be processed to send an email to me.


The code works well when the submiter is a logged in user. It sent an email to me. But, when the submitter is non logged in user, the code won't send any email to me.


This is my code for generating the form,



public function widget($args, $instance) {
global $wp;
$title = apply_filters('widget_title', $instance['title']);

if (is_user_logged_in()) {
global $current_user;
get_currentuserinfo();
$member_id = get_current_user_id();
$member = array(
'nama_lengkap' => get_cimyFieldValue($member_id, 'NAMALENGKAP'),
'perusahaan' => get_cimyFieldValue($member_id, 'PERUSAHAAN'),
'alamat' => get_cimyFieldValue($member_id, 'ALAMAT'),
'handphone' => get_cimyFieldValue($member_id, 'HANDPHONE'),
'telepon' => get_cimyFieldValue($member_id, 'TELEPON')
);
}
// Mendapatkan URL halaman saat ini.
$redirect = home_url(add_query_arg(array()));

// Menampilkan widget
if (is_single()) {
echo $args['before_widget'];
if (!empty($title)) {
echo $args['before_title'] . $title . $args['after_title'];
}
include(plugin_dir_path(__FILE__) . 'tpl/widget-frontend.php');
echo $args['after_widget'];
}
}


This is the processing form code,



public function kirim_email() {
if ($_POST['penawaran'] == '1') {
// Sanitasi data formulir
$nama_lengkap = sanitize_text_field($_POST['nama-lengkap']);
$email = sanitize_email($_POST['email']);
$perusahaan = sanitize_text_field($_POST['perusahaan']);
$handphone = sanitize_text_field($_POST['handphone']);
$telepon = sanitize_text_field($_POST['telepon']);
$alamat = esc_textarea($_POST['alamat']);
$pesan = esc_textarea($_POST['pesan']);
$produk = sanitize_text_field($_POST['produk']);
$kuantitas = sanitize_text_field($_POST['kuantitas']);
$redirect = sanitize_text_field($_POST['redirect']);
}

//$tujuan = get_option('admin_email');
$tujuan = sanitize_email('haeriadi.jvm@gmail.com'); // percobaan dulu
$headers = "From: $nama_lengkap <$email>" . "\r\n";
$subyek = "Permintaan penawaran dari $nama_lengkap";

ob_start();
include(plugin_dir_path(__FILE__) . 'tpl/email-permintaan.php');
$body = sprintf(ob_get_clean());

if ($pesan != '') {
$body .= "Pesan tambahan:" . "\r\n" . $pesan;
}

wp_mail($tujuan, $subyek, $body, $headers);
wp_redirect($redirect);
exit;

// "request handler" harus dimatikan jika tugasnya terselesaikan.
die;
}


This is the class constructor,



public function __construct() {
parent::__construct(
'Penawaranku',
'Permintaan Penawaran',
['description' => 'Formulir permintaan penawaran.']
);

add_action('wp_enqueue_scripts', [$this, 'skripku']);
add_action('admin_post_penawaran', [$this, 'kirim_email']);
add_action('admin_post_nopriv_penawaran', [$this, 'kirim_email']);
}


I guess that the problem is related to admin_post_nopriv action hook. I am searching admin_post_nopriv documentation but I just found admin_post_ documentation.


Anyone here can help me or tell me some references about it?





Aucun commentaire:

Enregistrer un commentaire