Title: Create new redirect through PHP
Last modified: August 30, 2016

---

# Create new redirect through PHP

 *  Resolved [snyzio](https://wordpress.org/support/users/snyzio/)
 * (@snyzio)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/create-new-redirect-through-php/)
 * Which function do I call to create a new redirect with a post?
 * Backstory:
 * I have a webinar plugin that creates two posts to correspond to the webinar. 
   One is for the custom post type ‘webinars’ and the other is for my tribe events
   calendar so that they will be able to view that on the events calendar also. 
   I want to create a redirect for the events post type to point to the webinars
   post type each time a new webinar is created.
 * Which function do I need to call and what information do I need to pass to it
   in order for it to create a redirect from ‘/events/’ to ‘/webinars/’?
 * [https://wordpress.org/plugins/quick-pagepost-redirect-plugin/](https://wordpress.org/plugins/quick-pagepost-redirect-plugin/)

Viewing 1 replies (of 1 total)

 *  [Don Fischer](https://wordpress.org/support/users/prophecy2040/)
 * (@prophecy2040)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/create-new-redirect-through-php/#post-6426808)
 * Hi snyzio,
    The Individual Redirects are just meta data for the post.
 * So, to create a redirect programatically, you can do something like below.
 * Put this in your `functons.php` file (or some file that will be called when the
   post is created):
 *     ```
       function create_redirect_for_webinar( $atts = array() ){
       	if( !is_array( $atts ) )
       		return;
       	$defaults = array(
       		'post_id' 	=> '0',
       		'active' 	=> 1,
       		'url'		=> '',
       		'type' 		=> '301',
       		'newwindow'	=> 0,
       		'nofollow'	=> 0,
       		'rewrite'	=> 0
       	);
       	extract( shortcode_atts($defaults, $atts) );
       	if( $post_id == '0' || $url == '' )
       		return;
       	// some validation
       	$type 		= !in_array( $type, array( '301', '302', '307', 'meta' ) ) ? '301' : $type;
       	$active 	= (int) $active == 1 ? 1 : 0;
       	$newwindow 	= (int) $newwindow == 1 ? 1 : 0;
       	$nofollow 	= (int) $nofollow == 1 ? 1 : 0;
       	$rewrite 	= (int) $rewrite == 1 ? 1 : 0;
       	// set required meta
       	add_post_meta( $post_id, '_pprredirect_url', $url );
       	add_post_meta( $post_id, '_pprredirect_type', $type );
       	add_post_meta( $post_id, '_pprredirect_active', $active );
       	//set optional meta
       	if( $rewrite == 1 )
       		add_post_meta( $post_id, '_pprredirect_rewritelink', 1 );
       	if( $newwindow == 1 )
       		add_post_meta( $post_id, '_pprredirect_newwindow', '_blank' );
       	if( $nofollow == 1 )
       		add_post_meta( $post_id, '_pprredirect_relnofollow', 1 );
       }
       ```
   
 * Then to use it, you would put this where your posts are created:
    **Be sure to
   make sure the `$post->ID` is replaced with your variable for the post id if not
   using `$post` global AND you add the correct URL of the Redirect.**
 *     ```
       $atts = array(
       	'post_id' => $post->ID, // your post ID of the created post
       	'url' => 'add redurect URL here', // redirect URL
       	//you can add the other attributes here is you need them
       );
       // call our function
       create_redirect_for_webinar( $atts );
       ```
   
 * We will be adding something like this to the plugin soon, but with this function
   you will be safe when we do.
 * Let me know if you have questions or issues.
    Warm regards, Don

Viewing 1 replies (of 1 total)

The topic ‘Create new redirect through PHP’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/quick-pagepost-redirect-plugin_8d7b6f.
   svg)
 * [Quick Page/Post Redirect Plugin](https://wordpress.org/plugins/quick-pagepost-redirect-plugin/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/quick-pagepost-redirect-plugin/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/quick-pagepost-redirect-plugin/)
 * [Active Topics](https://wordpress.org/support/plugin/quick-pagepost-redirect-plugin/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/quick-pagepost-redirect-plugin/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/quick-pagepost-redirect-plugin/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Don Fischer](https://wordpress.org/support/users/prophecy2040/)
 * Last activity: [10 years, 10 months ago](https://wordpress.org/support/topic/create-new-redirect-through-php/#post-6426808)
 * Status: resolved