• Resolved snyzio

    (@snyzio)


    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://ww.wp.xz.cn/plugins/quick-pagepost-redirect-plugin/

Viewing 1 replies (of 1 total)
  • 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.