Roger
Roger

Reputation: 83

Why this wp_redirect function is not working?

I am trying to do a redirection inside my plugin, I want to send the user directly to the edit post page, instead it is redirecting to the post list.

wp_redirect( get_edit_post_link( $post_id ) ); exit;

For your information, get_edit_post_link() is returning the right value: http://localhost/wp-admin/post.php?post=63&action=edit.

Upvotes: 1

Views: 1111

Answers (3)

Piotr Małłek
Piotr Małłek

Reputation: 71

If someone is still looking for this, try:

wp_redirect( get_edit_post_link( $post_id, '' ) ); exit;

From WP docs:

context (string) (optional) How to write ampersands. Defaults to 'display' which will encode as '&'. Passing any other string (including an empty string), will encode ampersands as '&'. Default: 'display'

Upvotes: 6

tomyam
tomyam

Reputation: 389

I can't tell you why get_edit_post_link doesn't play nice within wp_redirect, but if you pass the edit post link path to admin_url it works:

wp_redirect( admin_url( '/post.php?post=' . get_the_ID() . '&action=edit' ) ); exit;

Upvotes: 1

Lax Mariappan
Lax Mariappan

Reputation: 146

Check the role of the user you are testing with.

If its author, an author / contributor cannot edit other's post.

Upvotes: 0

Related Questions