Skip to content

Instantly share code, notes, and snippets.

@jartes
Created March 18, 2013 10:48
Show Gist options
  • Save jartes/5186365 to your computer and use it in GitHub Desktop.
Save jartes/5186365 to your computer and use it in GitHub Desktop.
Redirect WordPress page to URL specified by a custom field with key "url_to_go"
function lets_redirect( $query ) {
if ( !is_admin() ) {
if ( $query->is_page() && $query->is_main_query() ) {
$redirection = get_post_meta( $query->queried_object->ID, 'url_to_go', true );
if ( $redirection != '' ) {
?>
<script language="javascript" type="text/javascript">
window.location.href="<?php echo $redirection ?>";
</script>
<?php
}
}
}
}
add_action( 'pre_get_posts', 'lets_redirect' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment