To continue using what we’ve learned in this lesson on Using Custom Post Meta in the Thesis theme, we’re going to add sidebar configuration options. What we’re going to do is create a post meta box that will control whether or not these attention boxes show up or this alternate feature box shows up. If you look at the way this works, right now we make these show up by hard coding it so if it’s single 118 then show this, if it’s page carousel then show this.
Add Sidebars to Specific Pages in the Thesis Theme
The only way for a user to automatically add these sidebars to specific pages is to come in here and edit this. What we want them to be able to do is check off a checkbox inside the page itself to display that rather than having to come in and edit the code and that’s what we’re going to do now.
We’ll go ahead and start by defining this, actually let’s just define it here. This is going to be not $byob_page_specific_sidebar_content but it’s going to be $byob_custom_sidebar_selector. Again, we’ll capture that ID then Sidebar Configuration Selector will be the tile of this. In this case, the context is not going to be normal it’s going to be side and the priority will be low. Now, also in this case, we’re going to specify types.
Right here, this applied to both posts and pages but in this case, we’re going to apply it only to pages so we’ll specify types and this has to be in a form of an array even if it’s only one element. We still need to define it as an array so we’re going to call it page. The type that’s going to show up is going to be the type ‘page’ and that’s the only change we’re going to make here. We have to create our new template.
Our new template is going to be byob_sidebar_selector_meta. Now we’re going to create that file, we’re going to take this file that we just created, the sidebar_content_meta, copy it, paste it here and inside of content_meta, it’s going to be sidebar_selector_meta and we’ll come over here and open that. At the moment, it’s exactly the same. Here what we’ll say is Select a Custom Sidebar Configuration.
Although we’re not going to be using these things here at all so we’re going to take this part out. What we’re going to do is open up the checkbox_meta example which is actually pretty complicated. This system can do a lot of crazy things and we’re not going to bother looking at those crazy things so all what we’re really going to do here is come and take this one piece, copy it and we’re going to replace this section with that.
Place a Checkbox Beside Sidebar Configuration
What we’re going to do is now, we’re going to give people the opportunity to place a checkbox beside each sidebar configuration they want to use on a given page. The first sidebar configuration we’ll use is alternate meta box, byob_use_alternate_feature_box, that’s the field name. A checkbox input type takes a name, it can take an ID and it takes a value and this value is the value that it will have if it’s checked.
This value could be 1, it could be true, it could be abc, it could be a long sentence. I’m just going to say, ‘use’ so if it’s checked, we’re going to use the alternate feature box. Then we have this thing, the_checkbox_state. The checkbox state is if it’s checked, then the value is going to be use, if it’s not checked then there’s no value. That’s what this thing means.
What we’ll say here is, Use the Alternate Feature Box. We’re going to add one more of these to this so we’ll just copy that whole thing and we’ll say, use_attention_boxes and the state will be the same and the value will be the same but over here, we say, Add the Attention Boxes to the bottom of the page content, that is our form.
It’s going to have 2 checkboxes and this is the definition of that meta box so we’ll save that, upload our custom_functions, we’ll upload our sidebar_selector, we’ll come over to our site and edit the page. Now you can see we have this Sidebar Configuration Selector, which I said Give it a Title which of course is useless so let’s just say Select Sidebars. Save that, refresh it.
Add Attention Boxes to a Specific Page
We are on the carousel page so let’s go to a page that doesn’t have these actually because this page currently already has all these things. Let’s go to the Mommy Blogger page and let’s say, Add Attention Boxes, if we hit update and scroll down here to the custom fields, we can see the custom field that said byob_use_attention_boxes and it’s set to use.
Now what we need to do is write our function to use that. Let’s just copy this function, byob and this function name is add_attention_boxes. That’s the thing we’re going to hook to and the action is thesis_hook_after_post_box. Now we don’t want $title and what we’re going to do here is say, if(get_post_meta($post->ID, ‘byob_use_attention-boxes’. If byob use attention boxes exists, if get_post_meta, post ID, if it doesn’t exist, this get_post_meta returns false, so if it exists then what we want to do is do the attention box thing.
The attention box thing is byob_attention_boxes so if there’s any value, we can say if the value is equal to use but it doesn’t really matter here. If there’s any value, that means it’s being used, then we’re going to call this thing, attention_box_sidebars and add that action to thesis_hook_after_post_box, Add Attention Boxes.
If we save this, upload it and go look at this, the attention boxes should be added. They aren’t added, what did I do wrong there? Oh, there’s no hook, that’s not the name of the hook, thesis_hook_after_post_box, that’s the name of the hook. There we go, now our attention boxes show up here but if we come over to demonstration, they don’t show up there.
Now, we’re going to do exactly the same thing here for the alternate_feature_box. This is going to be thesis_hook_before_content, add_alternate_feature_box and our alternate_feature_box_sidebar is here. It’s not going to be use_attention_boxes, it is use_alternate_feature_box. In this case, if(get_post_meta($post->ID, ‘byob_use_alternate_feature_box’ then call that feature box and we’re hooking this function to here. Let’s edit this page, let’s use the alternate_feature_box, hit update and view the page.
That’s what I did, I failed to upload it. Now our Alternate Feature Box shows up in this section and I probably want it before_content_box so that it’s consistent with all the rest of these that we’ve done. Let’s refresh this again so it’s not there, it isn’t the carousel because we still have all that old stuff affecting it but it’s not on About but it is on Demonstration.
The Control Structure
The next thing to do is to incorporate this concept into our control structure because we come back up here to our control structure and we have these things put together for if(is_page). We could continue to use if(is_page) here but instead of using if(is_page), we could also use this. Let’s use this one first, copy and we could make this so that it affects all pages so if(s_page) and we’re not going to do it this way, we are to add the action alternate_feature_box.
We could do the same thing here for our other one. I’m going to comment out both of these functions so that they don’t work anymore. Then I’m going to come down here and add that to it, use_attention_boxes and what we’ll do is cut that one out, replace it with this.
Really, unless there’s something special we want to do with carousel, there’s no real reason to have carousel here, we’ll go ahead and leave it there anyway except that this will exclude these things here because if it’s page carousel, it’s just going to automatically return so this thing will only happen if it’s not page carousel.
In fact, we probably want to get the return statement out of this now and the reason we’re getting the return statement is because we have some things that we want to have happened on every page. We could do exactly the same thing with single, with category and things like that if we wanted to but right now, we’re just going to stick with this page.
Go ahead and hit save, upload our custom_functions.php file. Now what should happen here is carousel shouldn’t have either of those things anymore. It doesn’t have either of those because none of those things were checked.
We’ll see Demonstration, let’s double check this. Why doesn’t it work is because we don’t have global $post. What did I say the beginning of this thing? If you don’t have global $post, it may not work or if it doesn’t work, look for global $post. That’s because we don’t know what the page ID is without this global $post variable. Now, it’s going to work just fine.
Let’s view that page and now we have that widget area, carousel doesn’t have anything. If we want to add something to carousel, we can do that from here, Add the Attention Boxes, hit update, view the page and the attention boxes are now added.
Obviously, this is a very powerful way of adding custom things to specific pages in the Thesis theme that lets you get out of all of this if (is_single) such and such, if(is_category), if(is_page) such and such. It gets you out of needing to hardcode that question and all you have to do is get the post meta and ask if the post meta has been set, that’s really the whole point in my perspective. The main point of using custom post meta is to have this ability to be able to add things to a post or page without having to get in here and fix or adjust the code.
Some of you are developing websites professionally so you want your clients to be able to add these kind of things. Some of you are developing your own sites and if you’ve got to go back and open the code or remember what was that code again, what did I have to do, where did I store that thing, that can create a barrier to getting something like this done but with the custom post meta now, you can create a post meta box and set it up that way.