One simple way of enabling pop-up windows right from your WordPress posts, is by using the Thickbox javascript library that comes with WordPress.
You can link the pop-up window to a HTML input element such as a button, or you can simply link the pop-up window to a HTML hyperlink. Below we show examples of both –
How to Add Pop-up Windows to Your WordPress Posts
Step 1
You need to add thickbox to your WordPress post. You can do this by adding the add_thickbox function to the ‘init’ function of your theme. In particular, add the code below to your theme functions.php file.
<?php add_action('init', 'init_theme_method'); function init_theme_method() { add_thickbox(); } ?>
Step 2
Now we can just add the button right in our post and link it to thickbox by specifying class=thickbox
. Click on the HTML tab in your post editor and paste the HTML code below into your post.
<div style="text-align:center;padding:20px 0;"> <input alt="#TB_inline?height=300&width=400&inlineId=examplePopup1" title="add a caption to title attribute / or leave blank" class="thickbox" type="button" value="Show Thickbox Example Pop-up 1" /> </div>
Step 3
Finally, we specify the content that goes into our pop-up window by creating a div with id=examplePopup1
.
<div id="examplePopup1" style="display:none"> <h2>Example Pop-up Window 1</h2> <div style="float:left;padding:10px;"> <img src="https://shibashake.com/wordpress-theme/wp-content/uploads/2010/03/bio1.jpg" width="150" height = "168"/> </div> I was born at DAZ Studio. They created me with utmost care and that is why I am the hottie that you see today. My interests include posing, trying out cute outfits, and more posing. <select name=""><option>test</option></select> <strong>Just click outside the pop-up to close it.</strong> </div>
And just like that, we have our pop-up window.
There are a variety of other thickbox options including using iframes, displaying image galleries, and using AJAX content.
Only Include Thickbox as Necessary
While the example above works, it is not the most efficient because you are including the thickbox library into all of your WordPress posts, whether you are using it or not.
Ideally, you only want to include thickbox in those posts that actually require it.
One way to achieve this is by using the is_single or is_page WordPress functions to include thickbox on a post by post basis.
add_action('template_redirect', 'add_scripts'); function add_scripts() { if (is_single('2794')) { add_thickbox(); } }
Line 1 – Instead of tying the add_thickbox function to the init action hook, we tie it to the template_redirect action hook instead. This will allow us to properly use the is_single and is_page commands in our hook function.
Lines 4-6 – Only add thickbox to this post which has an id of 2794.
Include Thickbox Based on Post Tags
The solution above works well if you want to include pop-up windows to a small set of posts and pages. However, if you later decide to use pop-ups on a large set of posts, using is_single may quickly become unwieldy.
While you can pass in an array of posts to the is_single function, you will still need to list out the title or id of each and every post that uses thickbox.
A more elegent solution, is to include the thickbox libraries based on post tags.
<?php function add_scripts() { global $wp_query; if (!is_singular() || !$wp_query->post) return; // Get post tags $tags = wp_get_object_terms($wp_query->post->ID, 'post_tag'); foreach ($tags as $tag) { if ($tag->name == "thickbox") { add_thickbox(); break; } } } ?>
Line 4 – We only add thickbox for single posts, pages, or attachments.
Line 6 – Get the post tags for the current post.
*Note*, we cannot used WordPress Loop functions at this point because the template_redirect action hook is called before the Loop is fully instantiated. However, query_vars has already been called, so the $wp_query global variable contains the values that we need.
Lines 7-12 – Check for the thickbox tag. If we find it, we add the thickbox library.
Now all we need to do is add the thickbox tag to posts that use the thickbox library and we are done.
hiral says
how to use this code in image element instead of button/input element??
Bliz says
Thanks! This helps a lot!
Roger says
Gracias Maestro, me ayudo un monton el codigo y la explicación!
Stephen says
Thanks for the reply. I looked into JQuery Ready after your suggestion and glossed over a bit. Sounds like the onload event would be easier. Do you have a example I could follow that would work with your previous example? I have the page loading while clicking the button but just need it when the page loads… Thanks!
Stephen says
Thanks for the tip on popups. I got our example to work without problems, but I wonder how I could invoke this popup when the page loads rather than clicking on a button?
ShibaShake says
You can call the tb_show javascript function directly. Just link it to the javascript onload event, or use jQuery ready.
http://phpxref.ftwr.co.uk/wordpress/nav.html?wp-includes/js/thickbox/thickbox.js.html
Debby says
Hi Shibashake
I am trying to implement this on my site and it is not working. It’s on my localhost at present so i cant send link. I also noticed that the button and link on this page don’t work. is it just me?
ShibaShake says
Thanks for pointing out that the button and link does not work. It is fixed now. The problem was due to a plugin conflict.
Andrea Bersi says
Thank you for another great tutorial!!!!
Your site is a resource of great tip for wordpress.
Ciao!
Dion GeBorde says
Hey ShibaShake,
I found your site a little while back and I’ve got to say, you must be one of the most talented folks I’ve come across when it comes to being both a designer and a tutor. Thanks for this site.
I have a question about using thickbox and using init to call it. I’ve seen a lot in the WP codex about using wp_enqueue_script to avoid duplicate calls of jquery or whatever might be needed to run a function or plugin. Is there a reason that you do it this way? I’m still very much a newbie, so trying to get all this info sorted out in my head.
Thanks.
ShibaShake says
Hello Dion,
Thanks for your very kind works.
You are definitely right about wp_enqueue_script, it is a good function to use to avoid duplicate loads of the same js or css files. In fact the add_thickbox function uses both wp_enqueue_script and wp_enqueue_style to add the relevant thickbox script and style.
add_thickbox is a shorthand way to add the relevant scripts and styles. It also runs any extra WP hooks that are deemed necessary. If there are plugins that hook into those actions, then using add_thickbox will automatically include those plugin calls as well.
ShibaShake says
I would just set up a timer using the javascript setTimeout command and then use that to call the thickbox Javascript function tb_show directly. Something like this –
John says
That’s a great tutorial! I have a question: IS there a way to make it so that the popup comes up without the user doing anything, on a timer? Essentially, my client needs a landing page that will display a popup if the user stays on the page for 20 seconds or so. So I would need this to come up automatically and on a delay. Any advice you could give me?