From the course: WordPress Development: Coding Practice
Add a sitewide banner to the top of the page - WordPress Tutorial
From the course: WordPress Development: Coding Practice
Add a sitewide banner to the top of the page
- [Instructor] Let's say we have a short promotion we want to run, and we want to advertise that on every page on our site. We are going to add a site-wide banner to the top of every page. So here are the requirements. The CSS and HTML are provided. You'll need to determine the proper actions for inserting both into the site. But there is a caveat. Right now, the CSS is completely fixed, but this will cause an issue if a user is logged in and displaying the WordPress admin bar on the front end. So be sure to create a CSS exception to account for that. As a reminder, the admin bar is 32 pixels tall. Let's take a look at our starter code. So again, we have our output functions and you can see that the site-wide banner is actually echoing the banner with the class banner. And our banner styles function is just printing the styles in line. And again, as I said, this is fixed to both the top left and right, so we want to make some adjustment here. Our goal is to find the right actions to add this banner to our website properly. So pause the video here and unpause when you're ready to see the solution. (upbeat music) (tones echoing) Welcome back. First, let's add the action. So we'll say add_action. And we want the banner styles to go in the actual header. So we'll say wp_head and then we'll call our function lil_banner_styles. This will print the CSS in the head of our page. We also want to add the HTML but there's no discreet action to place it above the header inside the template, as we don't really know, based on the theme, what that would look like. So instead, we're going to add this to the footer. This is why we have our position fixed here, because no matter what our theme looks like, our banner will always display at the top of the page. So we'll say wp_footer, and then lil_sitewide_banner. So let's save this here and see what our site looks like. But before we do that, let's actually add the s. We got to make sure that these functions match. Now let's save our file and see what the site looks like. So you can see when we're logged out, this looks great. We see our banner. It says, "Join Us for Our Free Webinar!" But when we're logged in, we only see half of the banner. So how do we fix that? Let's go ahead and make an exception for our top position. So let's create a variable called top and we'll say top gets, and then we'll do a conditional assignment. So we'll say if is_user_logged_in, and if they are, we'll make top 32 pixels the height of the banner. And if they're not, we'll make it zero. So now we will replace the zero in top with our top variable. We will save this. We'll refresh. And now we see our full banner.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
Dynamic copyright year2m 50s
-
(Locked)
Randomize posts5m 37s
-
(Locked)
Get post meta3m 19s
-
(Locked)
Change admin colors based on the server5m 18s
-
(Locked)
Create your own block pattern5m 58s
-
(Locked)
Append a message to RSS content5m 31s
-
(Locked)
Sanitize input4m 39s
-
(Locked)
Sanitize output4m 4s
-
Add a sitewide banner to the top of the page3m 32s
-
(Locked)
Register a custom post type5m 37s
-
(Locked)
Store user meta5m 29s
-
(Locked)
Send an email to the admin when a plugin is activated6m 42s
-
(Locked)
When a user logs in, greet them by their name4m 44s
-
(Locked)
Create a transient5m 30s
-
(Locked)
Hide content based on a query string5m 49s
-
-