Title: Any API/function to add redirection?
Last modified: January 21, 2020

---

# Any API/function to add redirection?

 *  Resolved [catherinek](https://wordpress.org/support/users/catherinek/)
 * (@catherinek)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/any-api-function-to-add-redirection/)
 * I’d like to add 410 redirections to deleted posts, possibly add it in the “after_delete_post”
   hook.
    Is there any documentation on this?

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

 *  Plugin Author [Rank Math SEO](https://wordpress.org/support/users/rankmath/)
 * (@rankmath)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/any-api-function-to-add-redirection/#post-12404854)
 * Hello [@catherinek](https://wordpress.org/support/users/catherinek/)
 * Thank you for contacting the support and please accept our apologies for missing
   your support topic and thus the delayed response.
 * Please try adding the following code in your theme’s functions.php file to create
   a redirection rule when a post is trashed:
 *     ```
       add_action( 'wp_trash_post', function( $post_id ){
       	$sources[] = [
       	    'pattern'    => get_the_permalink( $post_id ),
       	    'comparison' => 'exact',
       	];
       	$args = [
       	    'sources'     => $sources,
       	    'url_to'      => '',
       	    'header_code' => '410',
       	];
       	RankMath\Redirections\DB::add( $args );
       });
       ```
   
 * Please let us know if that helps.
 *  Thread Starter [catherinek](https://wordpress.org/support/users/catherinek/)
 * (@catherinek)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/any-api-function-to-add-redirection/#post-12411212)
 * Right on! Thanks a lot!
 *  Plugin Author [Rank Math SEO](https://wordpress.org/support/users/rankmath/)
 * (@rankmath)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/any-api-function-to-add-redirection/#post-12411220)
 * Hello [@catherinek](https://wordpress.org/support/users/catherinek/)
 * We are super happy that this resolved your issue. If you have any other questions
   in the future, know that we are here to help you.
 * If you don’t mind me asking, could you please leave us a review (if you haven’t
   already) on [https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post](https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post)
   about your overall experience with Rank Math? We appreciate your time and patience.
 * If you do have another question in the future, please feel free to create a new
   forum topic, and it will be our pleasure to assist you again.
 * Thank you.

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

The topic ‘Any API/function to add redirection?’ 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/)

 * 3 replies
 * 2 participants
 * Last reply from: [Rank Math SEO](https://wordpress.org/support/users/rankmath/)
 * Last activity: [6 years, 4 months ago](https://wordpress.org/support/topic/any-api-function-to-add-redirection/#post-12411220)
 * Status: resolved