0
if (is_author('admin')){
    add_action('template_redirect', 'wpse_redirect_posts_to_new_page');
    function wpse_redirect_posts_to_new_page() {
        // if this is not a single post or not of type post, do nothing
        if ( ! is_single() || get_post_type() != 'post')
            return;

        $url = parse_url(get_the_permalink());
        $newdomain = 'https://www.example.com';
        wp_redirect($newdomain . $url['path']);
        exit;
    }
}

currently, it's redirecting all users post to new url

1 Answer 1

0

First of all... [is_author][1] doesn't check if given user is author of current post. Straight from Codex:

This Conditional Tag checks if an Author archive page is being displayed. This is a boolean function, meaning it returns either TRUE or FALSE.

So it won't help you.

Another problem with your code is that you perform this check to early. If this code is pasted in functions.php, then the if condition will be checked before query is parsed.

So how should it really look like? Something like this:

function wpse_redirect_posts_to_new_page() {
    // if this is not a single post or not of type post, do nothing
    if ( ! is_single() || 'post' != get_post_type() ) {
        return;
    }

    $post = get_queried_object();  // it's single, so this will be a post
    if ( AUTHOR_ID != $post->post_author ) { // change AUTHOR_ID to real ID of the author
        return;
    }

    $url = parse_url(get_the_permalink());
    $newdomain = 'https://www.example.com';
    wp_redirect($newdomain . $url['path']);
    exit;
}
add_action('template_redirect', 'wpse_redirect_posts_to_new_page');
3
  • where to declare the author id?
    – mia2
    May 23, 2018 at 13:38
  • @charchit wherever you want. You can even replace this constant with number. May 23, 2018 at 15:42
  • it's not working for me, can you test it? if you can share the specific code it 'll be helpful.thanks
    – mia2
    May 23, 2018 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.