Defaulting to index.php after form submit
-
I have a site with some custom taxonomies, which work fine. Then it also has a front end posting function, which is processed via functions.php when the form is submitted without errors it redirects to the new post fine.
However if there are validation errors it displays them on the page, however in the process the page defaults to using the index.php template file instead of the correct one. The function is below:-
$moan_error = false; function process_my_form() { global $moan_error, $theme_options; if ( wp_verify_nonce( $_POST['moan_nonce_field'], 'moan_nonce' ) ) : if(trim($_POST['moan_text']) != "" or trim($_POST['moan_text']) == $theme_options['default_textarea']) : if(trim($_POST['moan_category']) != "") : if(strlen(trim($_POST['moan_text'])) < $theme_options['moan_char']) : $current_user = wp_get_current_user(); $moan_status = "publish"; $bad_words = array_map('trim', explode("\r", get_settings('moderation_keys'))); $moan_text = wp_strip_all_tags( $_POST['moan_text'] ); $matches = array(); $match_found = preg_match_all("/\b(" . implode($bad_words,"|") . ")\b/i", $moan_text, $matches); if($match_found > 0) $moan_status = "pending"; $moan_info = array( 'post_title' => $moan_text, 'post_content' => "", 'post_type' => 'moans', 'post_status' => $moan_status, 'post_author' => $current_user->ID, 'tax_input' => array("moan_category" => $_POST['moan_category']) ); $moan_id = wp_insert_post($moan_info); $moan_topics = text_to_tags($moan_text); wp_set_post_terms($moan_id, $moan_topics, 'moan_tags'); if($moan_id): if($match_found > 0) : //naughty words $headers[] = 'From: MCM Admin <'.get_settings('admin_email').'>'; wp_mail(get_settings('admin_email'),'New Moan Pending Moderation','Please login and either approve, edit of trash.', $headers); wp_redirect(site_url('/') . "moan-pending"); exit; else : //success wp_redirect(site_url('/') . "?p=". $moan_id . "&new=true"); exit; endif; else : //couldn't save post $moan_error = $theme_options['save_fail']; endif; else : //too much text $moan_error = $theme_options['too_much_text'] . $theme_options['moan_char']; endif; else : //no category $moan_error = $theme_options['no_category']; endif; else : //no text $moan_error = $theme_options['no_text']; endif; endif; } add_action( 'init', 'process_my_form' );
The topic ‘Defaulting to index.php after form submit’ is closed to new replies.