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 } }
5 thoughts on “Opening an external url in a pop-up window for a specific page”
Hi,
just wanted to ask, will it work only in paid theme or free theme as well?
thanks
sunil kumar
Hi, it works on both version.
Cheers
Hi. how can I add more page ID and external URLs to functions.php? Example: 2 more links to add in my website. Thanks!
I tried inserting this code in my functions.php of my child theme, but, i am not sure where to put the page ID and how the redirect link should be formatted. Can you give an example what the completed code should look like.
Hi Richard,
Here’s an example where the page id is 2 and external url is wordpress.org.
Hope this helps!