在小部件内部,确保name
对于提交按钮:
<button type="submit" name="widget_submit">Submit</button>
在中
functions.php
:
function wpse20150815_processing_widget() {
if( !isset( $_POST[\'widget_submit\'] ) )
return;
//set an array for storing errors
$errors = array();
//process form data, and store errors in the $errors array
if( empty( $_POST[\'pincode\'] ) ) {
$errors[] = __( \'Pincode cannot be empty\', \'textdomain\' );
}
//if there\'s an error in our data validation, don\'t proceed further
if( !empty($errors) )
return;
//do what you want with the processed data
//don\'t forget to sanitize form fields before storing data into database
//i.e. inserting post content using the form data
$post_id = wp_insert_post(...); //dummy presentation, use proper sanitization etc.
if( !is_wp_error( $post_id ) ) {
//if the post is inserted successfully, then redirect
wp_redirect(\'path/you/desire/\');
exit;
}
}
add_action( \'template_redirect\', \'wpse20150815_processing_widget\' );
如果您想感谢某人提供了此代码块,请对@Sisir表示感谢
his answer to me 从那里我学到了这一点。