http://themethesis.com/wp-content/uploads/2010/07/jquery-alert-box.jpg

How to Make a jQuery-Powered Alert Box for the Thesis WordPress Theme

July 9, 2010

in Tutorials

jQuery Alert Box Thesis

The details of this tutorial only apply to people using the Thesis theme. The technique, however, can apply to anyone. So if you’re new to jQuery with any tip of website, read on.

As you may know, the wonderful Thesis theme comes with some default paragraph styles. This includes styling for notes (as you can see above) and alerts (as you can see above that). These are great, but wouldn’t it be cool if you could close the alert after you’ve been alerted? Potentially.

This tutorial has a bigger purpose than that, though. It should be a great introduction to jQuery for those who have never used it before. So let’s begin.

  1. Turn jQuery on

    On a normal site you would call jQuery from the head of your document. Thesis has support for this built in, so in your WordPress dashboard go to Thesis > Page Options > Javascript and select the jQuery library.

  2. Place the following in custom_functions.php:

    function custom_alert_box() {
    ?>
    
    <script type="text/javascript">// <![CDATA[
    $(document).ready(function() {
       $('#closealert').click(function(){
         $('p.alert').hide('2000');
       });
     });
    
    // ]]></script>
    
    add_action('wp_head', 'custom_alert_box');
    

    So what does this mean? It means that p.alert will be closed when you click something with the ID closealert and the animation will be finished in 2000ms. This allows for lots of customization, as #closealert could be an image or a span that says close me or a header, or whatever you think would be most useful.

  3. Add alerts with your close mechanism

    Now when you use the built-in alert style with <p class=”alert”>, place your #closealert button in there somewhere and you’re all set.

Tip: If you don’t want to add a closing mechanism, then you can change the applicable lines in the code to this:

$('p.alert).click(function(){
$('p.alert').hide('2000');

Now when you click anywhere in the alert box it will close.

Enjoy

This post was brought to you by the Thesis WordPress theme; the search-engine optimized powerhouse of a framework designed to help you get a website up and running faster and more successfully. Want more information? Get it here. Want a professional website built on the Thesis framework and customized just for you? Contact me.

Thesis Theme for WordPress:  Options Galore and a Helpful Support Community

You'll notice this site talks about the Thesis theme for Wordpress. Don't know what the heck that is? Find out.

{ 8 comments… read them below or add one }

Avi D July 9, 2010 at 5:13 pm

The sweetest bit of code this week! Thanks a ton Matt!

Need a bit of help in the actual execution…

Your actual code was #closealert or what? I’m not following that #closealert bit in the post…

Reply

Matt Dunn July 10, 2010 at 4:53 pm

#closealert can be anything. So for example, if you wanted to say “Click here to close” you would put “< span id="#closealert" > Click here to close < /span >” Or if you wanted to use an image, you would put “< img src = "image.jpg" id="closealert" / >“. Just whatever you put, give it an ID of closealert.

Reply

Richard Barratt July 9, 2010 at 7:50 pm

Hi, thanks for this – it’s about time I stared to try some jQuery, and this will be a great start!

Just one query (on jQuery).

How would you get the alert paragraph to reveal on the click as well? This technique could be used for all sorts of great mods.

Thanks again :)

Reply

Matt Dunn July 10, 2010 at 4:57 pm

The code’s a tiny bit different for that. Check out this article. That’s the tutorial I used when I learned how to do showing and hiding with jQuery and it has a lot of great examples, including toggling. “Option 1: Independent” should do the trick.

Reply

Richard Barratt July 13, 2010 at 9:27 am

Thanks Matt – will check it out :)

Reply

Avi D September 10, 2010 at 8:25 pm

Matt, a bit more help….finally got down to deploying this on my site but nothing happens.

This is what I have copy-pasted in…

function custom_alert_box() {
?>
//
add_action(‘wp_head’, ‘custom_alert_box’);

function welcome_box() {
Click here to close
}
add_action(‘thesis_before_content’, ‘welcome_box’);

Can you help some??

Reply

Matt Dunn September 19, 2010 at 2:32 pm

You don’t need a second function at all. The code is set up to work with the default alert style that comes with Thesis.

Reply

Tom Harrison September 25, 2010 at 8:27 pm

Hi Matt –

Thanks for the tutorial — I was looking for the simplest possible case, and this one looked good. I am still having problems wrapping my head around this and getting it to work.

I have jQuery turned on. I added the function declaration and add_action call to custom_functions.php (and can see it all saved and everything on the server). I added a paragraph with the class “alert”, and a span inside the paragraph with the id #closealert with some text. When I click the text, nothing happens.

So my question is a “teach me to fish” question — obviously I have been an idiot and failed to understand something (a common occurrence, sadly). My assumption is that at some point, when the page loads, the wp_head hook is called, thereby executing the function which adds the javascript … to where? Somewhere in the DOM, I would assume? Should it be visible as a simple script declaration in the head of the HTML through view source?

Using Firebug, I looked for any occurrence of the javascript in all the places I could thing of and couldn’t find it. I didn’t see any errors in the firebug console.

What am I missing? How can I debug my error?

Thanks!!!

Tom

Reply

Leave a Comment

Previous post:

Next post: