Last active
March 27, 2020 20:42
-
-
Save weismannweb/f1febe71e9f638d7d31464247d357961 to your computer and use it in GitHub Desktop.
I am using this to load JS like Mautic, Google Analytics, Facebook Pixel before redirecting to the target URL. This has only been tested a little but. Use at your own risk. It should still log the redirect but I have not checked that yet. In order for this to work you should create a page with the slug 'interstitial'. https://wordpress.org/plugi…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'redirection_url_target', 'wp_redirection_target'); | |
function wp_redirection_target($url){ | |
if(!defined('THE_REDIRECTION_TARGET')){ | |
define('THE_REDIRECTION_TARGET',$url); | |
return true; | |
} | |
} | |
add_action( 'wp_redirect', 'func_interstitial_redirect_get_url',1,1); | |
function func_interstitial_redirect_get_url($url) { | |
if(!defined('THE_REDIRECTION_TARGET')){ | |
return $url; | |
} | |
$add_js_content = ' | |
<script type="text/javascript"> | |
window.setTimeout(function () { | |
window.location = "'.THE_REDIRECTION_TARGET.'"; | |
}, 4000); | |
</script>'; | |
add_filter( 'the_content', function( $content ) use ( $add_js_content ) { | |
return $content.$add_js_content; | |
}, 12 | |
); | |
return false; | |
} | |
function func_interstitial_redirect() { | |
if(!defined('THE_REDIRECTION_TARGET')){ | |
return; | |
} | |
status_header( 200 ); | |
$the_slug = 'interstitial'; | |
$post = get_page_by_path($the_slug); | |
if(!empty($post->ID)){ | |
global $wp_query; | |
$wp_query->queried_object = $post; | |
$wp_query->is_single = true; | |
$wp_query->is_404 = false; | |
$wp_query->queried_object_id = $post->ID; | |
$wp_query->post_count = 1; | |
$wp_query->current_post=-1; | |
$wp_query->posts = array($post); | |
} | |
} | |
add_action( 'template_redirect', 'func_interstitial_redirect'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI You can make that page look however you want in the wp admin and in the pages template file.