Laimonas Sutkus
Laimonas Sutkus

Reputation: 3607

Wordpress redirect when clicked on post

This is what I have / trying to achieve:

  1. I have some posts.
  2. When someone clicks on the post i do not want it to be opened.
  3. Instead, I want that the user would be redirected to url that is associated with the post (url points to external apis).

Example, I have post:

  1. Title - 'ABC'
  2. Subtitle - 'ABCABC'
  3. Text - 'AAAAAABBBBBCCCC'
  4. (Trying to achive) Redirect url - 'http://test.com/123'

When user clicks on my 'ABC' post I would like him to be redirected to a corresponding url - 'http://test.com/123'.

Is there a plugin for that or maybe I could use some PHP code / DB modifications for that? Examples and suggestions are welcome!

P.S. I want the redirection to happen on every single post.

P.P.S. It is not suitable for me to redirect to the same constant url.

P.P.P.S. I really haven't found any good solutions on the web.

Upvotes: 2

Views: 1863

Answers (1)

Xhynk
Xhynk

Reputation: 13840

There's a few plugins that can do what you're looking for.

One I wrote a while ago that gives you the option to bring the external content into your site -or- 301 redirect to it: Content Mask

One that works similarly, but just redirects: Page Links To

Or one that lets you handle it without going into each post: Simple 301 Redirects.

Alternatively, you could set a Custom Field on your posts with the external URL. Then, check if that value is set, and if so, redirect to the external URL, something like the following:

add_action( 'template_redirect', 'redirect_externally' );
function redirect_externally(){
    $redirect = get_post_meta( get_the_ID(), 'redirect_to_here', true );

    if( $redirect ){
        wp_redirect( $redirect );
    }
}

Upvotes: 3

Related Questions