Title: Create redirection programatically
Last modified: January 31, 2022

---

# Create redirection programatically

 *  Resolved [uili](https://wordpress.org/support/users/uili/)
 * (@uili)
 * [4 years, 4 months ago](https://wordpress.org/support/topic/create-redirection-programatically/)
 * I’m trying to create a redirection after a webhook is triggered on my site, that
   create a custom post in wordpress.. could not find any function nativaly from
   Rank Math that could work how i want so i’m doing using $wpdb. But this way some
   stufs like number of access are not been counted and cant categorize that redirection
   too.. this is what I did so far.. is there a better way to do this?
 *     ```
       //get the unique course ID
       $course = get_post_meta($post_id, 'course_id', true);
       //get the course permalink
       $permalink = get_permalink($post_id);
   
       //prepare the source
       $source = array([
               'ignore'     => 'case',
               'pattern'    => $course,
               'comparison' => 'exact',
           ]
       );
   
       //prepare the args
       $rank_redirect = array(
           'custom_id'   => $course,
           'sources'     => serialize($source),
           'url_to'      => $permalink,
           'header_code' => '301',
           'status'      => 'active',
           'created'     => current_time( 'mysql' )
       );
   
       global $wpdb;
       $dbname = $wpdb->dbname;
       $rank_math_redirections = $wpdb->prefix . "rank_math_redirections";
   
       //because there is no column named custom_id in wp_rank_math_redirections, we create if doesn't exist so we can use it to insert or update the redirection later
       $is_status_col = $wpdb->get_results(  "SELECT <code>COLUMN_NAME</code> FROM <code>INFORMATION_SCHEMA</code>.<code>COLUMNS</code>    WHERE <code>table_name</code> = '{$rank_math_redirections}' AND <code>TABLE_SCHEMA</code> = '{$dbname}' AND <code>COLUMN_NAME</code> = 'custom_id'"  );
   
       if( empty($is_status_col) ):
           $add_status_column = "ALTER TABLE <code>{$rank_math_redirections}</code> ADD <code>custom_id</code> VARCHAR(50) NULL DEFAULT NULL AFTER <code>id</code>; ";
           $wpdb->query( $add_status_column );
       endif;
   
       $result = $wpdb->update($rank_math_redirections, $rank_redirect, array('custom_id' => $course));
       //If nothing found to update, it will try and create the record.
       if ($result === FALSE || $result < 1) {
           $wpdb->insert($rank_math_redirections, $rank_redirect);
       }
       ```
   
    -  This topic was modified 4 years, 4 months ago by [uili](https://wordpress.org/support/users/uili/).
    -  This topic was modified 4 years, 4 months ago by [uili](https://wordpress.org/support/users/uili/).

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Thread Starter [uili](https://wordpress.org/support/users/uili/)
 * (@uili)
 * [4 years, 4 months ago](https://wordpress.org/support/topic/create-redirection-programatically/#post-15313103)
 * Was looking for something like this for Yoast:
 * [https://gist.github.com/joshuadavidnelson/3b320988859efc054d0f7ec5aba61519](https://gist.github.com/joshuadavidnelson/3b320988859efc054d0f7ec5aba61519)
 *  Plugin Support [Rank Math Support](https://wordpress.org/support/users/rankmathteam/)
 * (@rankmathteam)
 * [4 years, 4 months ago](https://wordpress.org/support/topic/create-redirection-programatically/#post-15317955)
 * Hello [@uili](https://wordpress.org/support/users/uili/),
 * Thank you for contacting the support.
 * You can use the code mentioned here:
    [https://wordpress.org/support/topic/how-can-i-add-redirection-using-program/#post-15143013](https://wordpress.org/support/topic/how-can-i-add-redirection-using-program/#post-15143013)
 * Hope that helps.
 *  Thread Starter [uili](https://wordpress.org/support/users/uili/)
 * (@uili)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/create-redirection-programatically/#post-15325456)
 * Awesomee!!
    Thanks, you rock!
 * This is my function so far and working as espected:
 *     ```
       function set_rankmath_redirection($source,$target,$category){
   
           //prepare the source
           $sources[] = [
                   'pattern'    => $source,
                   'comparison' => 'exact'
           ];
   
           $rank_redirect = [
                   'id'          => '',
                   'sources'     => $sources,
                   'url_to'      => $target,
                   'header_code' => '301',
                   'status'      => 'active'
               ];
   
           //check if source exists
           $matched = \RankMath\Redirections\DB::match_redirections_source( $source );
   
           if ( $matched  ) {
   
               //add the id to update
               $rank_redirect['id'] = $matched[0]['id']; 
               \RankMath\Redirections\DB::update($rank_redirect);
   
           } else{
   
               //else just create
               \RankMath\Redirections\DB::add($rank_redirect);
   
           }
   
           //add category to the redirection id
           $getID = \RankMath\Redirections\DB::match_redirections_source( $source );
           $rankID = $getID[0]['id'];
           wp_set_object_terms( $rankID, $category, 'rank_math_redirection_category' );
   
       }
       ```
   
 *  Plugin Support [Rank Math Support](https://wordpress.org/support/users/rankmathteam/)
 * (@rankmathteam)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/create-redirection-programatically/#post-15330200)
 * Hello [@uili](https://wordpress.org/support/users/uili/)
 * Glad that helped.
 * If it isn’t too much to ask for – would you mind leaving us a review here?
    [https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post](https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post)
 * It only takes a couple of minutes but helps us tremendously.
 * It would mean so much to us and would go a really long way.
 * Thank you.​​

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Create redirection programatically’ is closed to new replies.

 * ![](https://ps.w.org/seo-by-rank-math/assets/icon.svg?rev=3438330)
 * [Rank Math SEO – AI SEO Tools to Dominate SEO Rankings](https://wordpress.org/plugins/seo-by-rank-math/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/seo-by-rank-math/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/seo-by-rank-math/)
 * [Active Topics](https://wordpress.org/support/plugin/seo-by-rank-math/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/seo-by-rank-math/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/seo-by-rank-math/reviews/)

 * 4 replies
 * 2 participants
 * Last reply from: [Rank Math Support](https://wordpress.org/support/users/rankmathteam/)
 * Last activity: [4 years, 3 months ago](https://wordpress.org/support/topic/create-redirection-programatically/#post-15330200)
 * Status: resolved