Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Adding a jQuery function to show or hide the welcome message

From: WordPress 3: Building Child Themes

Video: Adding a jQuery function to show or hide the welcome message

When you add nonessential content like a welcome message to a page on your site, it's always a good idea to give people an easy way of hiding that content. This is the type of behavior that JavaScript is great for. JavaScript allows you to add the user interaction on top of existing content so that your user can interact with the content and change it even after the page is being loaded in the browser. Now though we've added welcome message and an image here at the top of the page and it looks the way we want it to, I want to add an extra feature.

Adding a jQuery function to show or hide the welcome message

When you add nonessential content like a welcome message to a page on your site, it's always a good idea to give people an easy way of hiding that content. This is the type of behavior that JavaScript is great for. JavaScript allows you to add the user interaction on top of existing content so that your user can interact with the content and change it even after the page is being loaded in the browser. Now though we've added welcome message and an image here at the top of the page and it looks the way we want it to, I want to add an extra feature.

A little button down here somewhere, you can click on the button and this content is hidden and then you click on the button again and it shows the content again. To make that work I am going to use some jQuery Scripts, but before I can add the script I need to add the button. In my child theme I'll open welcome.php, scroll all the way to the bottom, and here I'm simply going to add a regular button. I'll wrap it in a div with a class, call the class hide-button, and inside this div I'm going to put a regular link.

The link doesn't have to point anywhere, because this link will trigger the JavaScript effect, but I'm still going to give it a title and I need some link text. So I'll just say Hide. I'll save this and test it in my browser to make sure it works here we have the hide button I can click on it and it jumps this up to the top of the page that's exactly what I am expecting. Now I need to add the jQuery script to make the hide and show function work. First, I'll go to my child theme folder, add a new folder call it JS.

And this is the folder where I am going to place all my JavaScript. Then I'll go to my code editor, create a new page, save it, and put it inside the JS folder calling it hide.js and I'll just make sure that this is a JavaScript file, Save it, and this is where I'll place my jQuery code. To make it easier for you, I have created a codesnippet, so you can simply go into the codesnippets file, copy out the codesnippet, paste it in, and then I'll show you how this works.

What we're doing here is adding a jQuery function. The function starts out by looking for a link that's wrapped in the class hide-button that's a link we just created down here, link wrapped in the hide-button class. It says when that link is clicked, run the following function. And the function does the following. It's finds the ID front page intro, which is the ID that wraps all the content in the welcome page. And then it says, slide toggle that front page intro slowly.

Slide toggle is a function that slides things up and hides them and then slides them down again to show them. The next function below here is just one that switches the hide text to show when it's hidden and then back to hide when it's shown. I'll save this JavaScript file, go back to my browser and reload it, and when I click on the link nothing happens. That's because although we created the JavaScript we haven't told the site to use it yet. To get the site to use the JavaScript we need to enqueue the JavaScript into the site.

Previously in the course we looked at enqueuing and dequeuing scripts and styles, and now we're going to use that same technique, to add a new script into our sites via our child theme. Enqueuing happens in functions.php, so I'll open functions.php in my child theme, scroll all the way to the bottom, create some more space and then I'll go back to my codesnippets file and find the function I need. It's right down here. I'll copy out that function, paste it in, and now we'll take a quick look at what it's happing here.

I create a new function called ChildOfTwentyTwelve_hideMeta. This function only works if this is not an admin page, because we don't need it for the admin page. What the function does is it first registers a script we give the script the name hide. We find the script in the stylesheet directory that is the directory for the child theme under the JS folder and the file is called hide.js. That's a file we created just a bit earlier. Then we associate hide.js with the jQuery scripts that's packaged inside WordPress and finally we enqueue this hide scripts.

The last part is an add_action call that on an initiation of the theme calls ChildOfTwentyTwelve_hideMeta which is this function. When I save this and reload my page again, you'll see that now the Hide/Show function works. I can hide the content, and when I hide it the text changes to Show and then back to Hide again. The last step in this process is purely cosmetic. It looks kind of weird right now because the hide is associated with the post directly underneath it.

What I want to do is add some styling to move Hide over here and make it visually associate with the box with the welcome content. So once again I'll go back to my codesnippets scroll down and here I'll find a little bit of style code that I can copy and paste into style.css in my child theme, so I'll open style.css, scroll down to the end of where I have the style for the front page and just paste that style in. If you look closely you'll see all this is a styling that affects the button, so that the button looks different, it has some borders on it, and it has a different color, and it also floats to the right.

When I save my stylesheet and reload my page, you'll see the button jumps over here to the rights it also looks like it belongs to this welcome box and it still works the exact same way as it did before. JavaScripts can make your site more interesting and easier to use for your visitors. Adding JavaScript correctly through enqueuing allows creative flexibility and less clutter when you work with your theme. The key when using JavaScripts is to subtle about it.

Adding too much fancy JS often leads to a clunky site that takes away from the content. What you have seen here in the few last movies is how we can add advanced features into our site in a very easy way and add extra functionality for our visitors. And that's kind of whole point of using a child theme, you make the experience better for your visitors.

Show transcript

This video is part of

Image for WordPress 3: Building Child Themes
WordPress 3: Building Child Themes

45 video lessons · 33008 viewers

Morten Rand-Hendriksen
Author

 
Expand all | Collapse all
  1. 6m 59s
    1. Welcome
      1m 6s
    2. Using the exercise files
      1m 34s
    3. What you need to know before watching this course
      1m 29s
    4. Getting the right tools for theme creation
      2m 50s
  2. 17m 2s
    1. What is a WordPress child theme and when should you use it?
      2m 38s
    2. Picking a parent theme
      3m 55s
    3. Making sure you have the Twenty Twelve parent theme
      1m 50s
    4. Creating and activating a basic child theme
      4m 20s
    5. Importing parent theme styles
      4m 19s
  3. 16m 2s
    1. Using the developer tools
      3m 53s
    2. Modifying existing styles
      4m 24s
    3. Adding space between paragraphs
      4m 7s
    4. Changing font family styles
      3m 38s
  4. 31m 47s
    1. Understanding the WordPress template hierarchy
      3m 12s
    2. Modifying existing templates
      2m 33s
    3. Moving the header image
      4m 29s
    4. Adding Related Posts feature to posts
      6m 26s
    5. Creating custom page templates
      5m 43s
    6. Using conditional statements for customized effects
      5m 41s
    7. Creating custom header, footer, and sidebar templates
      3m 43s
  5. 17m 5s
    1. Understanding the different index pages and what they do
      4m 6s
    2. Adding author, date, and time information to the index loop
      7m 15s
    3. Changing the appearance of category index pages
      5m 44s
  6. 43m 5s
    1. Introducing functions.php
      3m 24s
    2. Overriding existing functions
      3m 23s
    3. Adding pagination to index pages
      5m 49s
    4. Adding to existing functions
      3m 21s
    5. Adding a new footer menu to Twenty Twelve
      6m 24s
    6. Adding a new widgetized area to pages
      4m 9s
    7. Adding static content to the sidebar
      7m 44s
    8. Replacing existing functions
      2m 36s
    9. Adding a Google font through a function
      6m 15s
  7. 10m 24s
    1. Adding new featured image sizes
      5m 41s
    2. Adding featured images to posts and pages
      4m 43s
  8. 31m 1s
    1. Adding a welcome message to the front page
      1m 22s
    2. Displaying page content in an index page
      7m 42s
    3. Hooking in a featured image
      4m 34s
    4. Making the welcome message responsive
      6m 27s
    5. Restricting content to the first page of the blog
      4m 22s
    6. Adding a jQuery function to show or hide the welcome message
      6m 34s
  9. 10m 23s
    1. Adding a custom favicon
      3m 58s
    2. Adding a custom screenshot
      2m 29s
    3. Adding footer information
      3m 56s
  10. 7m 14s
    1. What to do when a child theme crashes your website
      4m 38s
    2. Updating parent and child themes
      2m 36s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed WordPress 3: Building Child Themes.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.