Opening an external url in a pop-up window for a specific page

⚠️ Code snippets might not be compatible with the latest version of your theme. Always use a child theme to customize your theme, and test your code on a staging website before migrating to production.

To do that, you need :
1) The linked page ID (easy to find in the url when editing the page in admin)
2) the external page URL

Copy and paste the code below in your functions.php

add_action( 'wp_head', 'my_redirection' );
function my_redirection(){
	global $wp_query;
	//we get the current object id
    $id = $wp_query->get_queried_object_id();

    //we check if we display a page AND if the page has the wanted id
    if (is_page() && YOURPAGEID == $id) {
    	?>
    		<script type='text/javascript'>
			function open_win()
			{
				window.open("EXTERNALURL", '_blank');
			}
			open_win();
		</script>
    	<?php
    }
}

 

You can add a timer if you want the pop up to open after a delay :

add_action( 'wp_head', 'my_redirection' );
function my_redirection(){
	global $wp_query;
	//we get the current object id
    $id = $wp_query->get_queried_object_id();

    //we check if we display a page AND if the page has the wanted id
    if (is_page() && YOURPAGEID == $id) {
    	?>
    		<script type='text/javascript'>
			function open_win()
			{
				window.open("EXTERNALURL", '_blank');
			}
			timer=setTimeout('open_win()',5000);//change the delay in milliseconds
		</script>
    	<?php
    }
}

 

Leave a comment

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

5 thoughts on “Opening an external url in a pop-up window for a specific page”