I am trying to redirect frontend users after deleting their post from frontend. I am using the get_delete_post_link and then redirecting by using the following code:
add_action( 'trashed_post', 'dex_redirect_after_trashing', 10 );
function dex_redirect_after_trashing() {
$authorName = get_the_author_meta( 'user_nicename' );
$myWeb = home_url('/');
$targetUrl = $myWeb . $authorName;
wp_redirect( $targetUrl );
exit;
}
I am trying to redirect to a url format like this "www.mysite.com/$author
"
Currently its redirecting to the home url.
Desclaimer: I am a newbie.
CodePudding user response:
You are missing the /author/
part in the Link.
add_action( 'trashed_post', 'dex_redirect_after_trashing', 10 );
function dex_redirect_after_trashing() {
$authorName = get_the_author_meta( 'user_nicename' );
$myWeb = home_url('/');
$targetUrl = $myWeb . 'author/' . $authorName;
wp_redirect( $targetUrl );
exit;
CodePudding user response:
As pointed by CBroe above in the comments, the get_the_author_meta
needed author ID as a second parameter.
Here is the revised code:
add_action( 'trashed_post', 'dex_redirect_after_trashing', 10 );
function dex_redirect_after_trashing() {
$authorID = get_post_field( 'post_author', get_the_ID() );
$authorName = get_the_author_meta( 'user_nicename', $authorID );
$myWeb = home_url('/');
$targetUrl = $myWeb . $authorName;
wp_redirect( $targetUrl );
exit;
}
The code is working as it supposed to be. Any further improvement or critic is appreciated.