Handy redirect for WordPress

This is a handy little wrapper around wp_redirect I use in functions.php. Most of the time when using wp_redirct I want PHP to exit, so thats what the following function does, and while we’re at it why not throw in the ability to accept a post ID or a uri for the location.

 * A handy wrapper to wp_redirect which exits after calling wp_redirect.
 * $location can be a url or a post id.
 * @param string|integer
 * @return bool False if $location is not set
function wptf_redirect( $location = '', $status = 302 ) {
	$permalink = false;

	if( is_int( $location ) ) {
		$permalink = true;
		$location = get_permalink( $location );

	if ( ! $location ) {
		return false;

	if( ! $permalink ) {
		$location = ltrim( $location, '/' );
		$location = '/'.trailingslashit( $location );
		$location = get_bloginfo( 'url' ).$location;

	wp_redirect( $location, $status );

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>