How to redirect outgoing links via my own website

How can redirect outgoing links first to my own site? for example i got a recipe post and i got a outgoing link to another website this link is stored in a metafield

$recipe_url = https://recipes.com/applepie;

but in the post i dont want to href recipes.com i want my own link like:

$displayed_url = https://myownsite.com/redirect/recipe/123;

does someone know how to archive this in wordpress?

Topic php Wordpress

Category Web


Here is a simple solution..

  1. Create a page that you want to use as your redirect handler and get the ID of that page.

  2. This goes into your theme's functions.php or any of your plugin file.

    function wpse405640_redirect() {
         if ( is_page(123) ) { //Assuming your page id is 123
             $redirect = isset($_GET['url']) && !empty($_GET['url']) ? $_GET['url'] : home_url('/');
             wp_redirect( $redirect );
             exit;
         }
     }
     add_action('template_redirect', 'wpse405640_redirect');

The URL structure should be https://myownsite.com/redirect/?url=https://recipes.com/applepie.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.