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.
Benny Neugebauer says
Thank you very much! This was helpful. 🙂
Bnpositive says
I’m working on a project where this is working. In the pop-up div I’ve embedded a Gravity Form. How can I make the form submission button close the pop-up?
Jeffrey says
You can try implementing an OnClick or OnSubmit (don’t recall which) to close after?
Safaa says
thanks a lot for the helpful information.
but I was wondering what if I want to add the button in multiple page hierarchy?
Jane Adams says
Hi, Unfortunately, ThickBox is beyond my skills et.
I dont understand steps 1-3. I thought I did, but all I end up with is a button but no pop up. Instructions for a total newbie would be great.
Jeffrey says
Can I just say…you are SAVING my life right now! Though I need help with a minor tweak. The link provided above is to the dev site where I am testing this. It resides within a feed of 28 posts. When I click it…it is only grabbing the information (Image and Title) of the first post and not that of the button under the specific post I clicked.
How do I change this?
Thanks you in advance!!
AGAIN…life saver
ShibaShake says
Very nice looking design. Also looks like you got things to work. Good job!
Anonymous says
Thanks!
Not entirely what I was looking for…but to where I’m ok with it. But I have a related question,
Using the thick box code in the link as I have to iFrame content….do you know of a way (possibly css) to hide the header of the page being iFramed?
I tried going the route of single templates based on the cat slug, but when the same post was accessed via other means (not Thickbox) it looked all wonky.
ShibaShake says
Hmmm, perhaps you could try limiting the scope of the CSS to just TB iframes, e.g.
#TB_window .post-title or #TB_iframeContent .post-title
reza says
hello, i have the same problem with Jeffrey, but can’t seem to find the answer here. is there a solution to this? much appreciated
reza says
oops, i was referring to the “it is only grabbing the information (Image and Title) of the first post and not that of the button under the specific post I clicked.” part. thanks
Jess says
How can I do this for a page via a menu link?
Deano says
Great example thanks, i’m all for using worpdress built in functions..
cheers
Mircea says
This works really well! Thank you for your share 🙂
Faisal Sarfraz says
Hi
I am trying this, entering the step 1 code in functions.php and then all other stuff but i am unable to see this working, 🙁
It is showing button but not displaying the content
andrakis says
Thanks for this great tutorial, shiba! Use the Thickbox at my travel diary of Ireland now. It works great. You rock! 🙂