在我的插件的一个短代码中,我在提交帖子的同时即时处理用户登录/注册。所以整个事情都是在短代码中发生的,比如:
<?php
function this_shortcode() {
ob_start();
//if isset(login)... process login, get $user_id
//else if isset(registration)... process registration, get $user_id
//else if already registered, get $current_user->ID as $user_id
/**
* if !empty($user_id){
* $p_id = wp_insert_post();
* if !empty($p_id) {
* echo success;
* **I NEED ONLY WHEN THE REDIRECTION SHOULD TAKE PLACE**
* } else {
* echo error;
* }
* } else {
* }
*/
<form>
<!-- registration/login + post insertion form here -->
</form>
return ob_get_clean();
}
add_shortcode( \'my-shortcode\', \'this_shortcode\' );
我已经知道了,
wp_redirect()
不是从短代码重定向的正确方法。所以,我想做的是:
function project_registration_login_redirection(){
global $post;
if( has_shortcode( $post->post_content, \'my-shortcode\' ) ) {
if ( isset( $_POST[\'my_submit\'] ) ) {
wp_redirect( home_url(\'/that-page\') );
exit();
}
}
}
add_action( \'template_redirect\', \'project_registration_login_redirection\' );
但很明显,这一个在没有任何检查的情况下重定向,即使表单验证在那里显示错误
wp_insert_post()
正在返回空。
How can I process the form somewhere, but do the redirection only when the form returns a valid feedback?
我试过:
global $post, $p_id;
if ( isset( $_POST[\'my_submit\'] ) && !empty( $p_id ) ) {
但它不起作用。