What hook can I use to change the redirect after posting a question?

3.90K viewsGeneral
0

When a question is submitted, the browser then goes to the question’s page. I’d like to redirect somewhere else. Is there a hook I can use to do this?

0
		$post_id = wp_insert_post( $question_array );

		if ( $post_id ) {

			// Update Custom Meta
			if ( ! is_user_logged_in() && ap_opt( 'allow_anonymous' ) && ! empty( $fields['name'] ) ) {
				update_post_meta( $post_id, 'anonymous_name', $fields['name'] ); }

			$this->redirect = get_permalink( $post_id );

			$this->result = array(
				'action' 		=> 'new_question',
				'message'		=> 'question_submitted',
				'redirect_to'	=> get_permalink( $post_id ),
				'do'			=> 'redirect',
			);
		}

You can see it is hardcoded, also look at this stack.

I have no idea how exactly actions work in wordpress. I guess they are executed right away, that would mean all that stack will be executed in place where “$post_id = wp_insert_post( $question_array );” is called. That would mean there is currently no hook to change the redirect, because it is assigned after every hook is done.

Easy way for you to achieve this would be to add a new line into the sources, after code I have in my example.

apply_filters( 'ap_after_processed_ask_form', $this ); // This code most probably will not work like it is right now.

You’d have to google how to apply filters, I must have done something wrong here.
Or even plug your code here:

		$this->process_form();

		if ( ! empty( $this->redirect ) ) {
			wp_redirect( $this->redirect );
			exit;
		}

, don’t forget to pass not just $this->redirect to your newly created filter, but more context, like whole $_POST maybe. (or is it available there by default? forgive me being a newbie)

And then after your filter works, you could submit it to https://github.com/anspress/anspress, from my experience it will be pulled into master during one work day.