In this session, we create the code for the shortcode function in the custom_functions.php file. We create a function that will display the information we want on the post and then create the shortcode. We also add a error handling to the shortcode function.
[This post contains video, click to play]
Code used in this lesson
/******************************************************************************* * This Function adds Thesis meta to Live Answers custom post type *******************************************************************************/ function gti_court_case_full_reference($atts){ $code = ($atts['case']); if ($code == ''){ return '<span style="color:red;">Whoops you forgot the case code</span>'; } $initial_search_args = array( 'post_type' => 'court-cases', 'meta_query' => array( array( 'key' => 'code_name', 'value' => $code ) ) ); $my_query = new WP_Query($initial_search_args); if ($my_query->have_posts()){ while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID; $case_id = get_the_ID(); endwhile; }else{ return '<span style="color:red;">Whoops there is no case by that code</span>'; } $full_reference = get_post_meta($case_id, 'full_reference', true); $reference_citation = get_post_meta($case_id, 'reference_citation', true); $case_url = get_permalink($case_id); $content = '<a href="' . $case_url . '" title="' . $full_reference . ', ' . $reference_citation . '"><cite>' . $full_reference .'</cite></a>, ' . $reference_citation; return $content; } add_shortcode( 'gti-case-full', 'gti_court_case_full_reference' );
Video Transcript
Reese: Okay.
Rick: Because that’s where we’re going to go next. We’re going to create the code for this and I am going to first, open up your custom functions php file. And that is Gambling Tax Institute… okay and then custom functions php. You’ve got a corrupted php file here. Okay, let’s see how badly it’s corrupted. We’ll try and use it anyway.
Reese: So you didn’t add the Thesis post meta? You can blow it away, I don’t have the…
Rick: No, I think your skin is dependent upon… at the very least, this line of code.
Reese: I don’t have any skins anymore. I took them out.
Rick: Is that right?
Reese: Yes sir. I tried using a skin and it didn’t work so I took it out.
Rick: Okay well then we’re going to delete this. Let’s see… file, save as. I’m just going to replace your custom functions php file because it does have a corruption in it which you can tell by the double spacing in the lines. Okay, didn’t I say save? Yes, I want to overwrite it and then I’m going to delete all these things.
Okay so I guess the first thing we’re going to do is do what we did last week which is add the custom post meta to court cases. And court cases was the name of that post type so this going to automatically just work. Then the second thing we’re going to do is create our function for getting the information that we want from the posts. And so we’re going to say function gti…
Reese: Cases, I guess.
Rick: Yeah, court case reference and this is going to be court case long reference. And actually, we should just say full reference since that’s the terminology we use. And while we’re at it, we’re going to use the function that creates our shortcode and that is add shortcode. So it’s add shortcode. The shortcode is going to be gti case full and then the function that it calls is that one.
So this is the way this add to shortcode thing works. Add shortcode defines essentially the calling parameter of the shortcode and then defines the php function that it acts on. And so when we go to create our shortcode, we’ll be creating a shortcode that’s gti case full as the first parameter of that shortcode. Okay?
Reese: That would be the function… right.
Rick: Well, it’s the first parameter of the shortcode is what it is. And the function name is gti court case full reference. So the next thing is we want to get the attributes from that because we’re going to be passing attributes in the shortcode. And the attributes look like… well, for example, let’s just say here that we’re going to… let’s put one right here. And so we’re saying gti case full and then the attribute is going to be code or actually, I’m going to say case… case equals and then we put our code in. So let’s see, what was our code for our 9th? It was 1925 dash… what was it?
Reese: McKenna.
Rick: McKenna.
Reese: But I’ve just been doing all those in lowercase.
Rick: Okay, I’m not sure how we wrote it though. I think we wrote it with the uppercase. It, of course, matters. So if you want to do court cases with all lowercase, we need to make sure that we did it with all lowercase. Yeah see, we typed 1925 McKenna with a capital M and a capital K. So if we want it not to be capitalized, we have to not capitalize it there. And that means we’d have to go back and change Frey too because Frey was capitalize.
And it’s important to see this because people don’t remember that capitalization matters or the case of the thing matters. But the case does matter. So let’s go back to court cases and edit Frey. 1925 lowercase f, update. So this is going to take… lowercase m. When this is working properly, what it’s going to do is it’s going to take the full reference and place it in this location. And so that’s what we’ll work on. In fact, let’s just go view that court case because right now, it’s not doing anything. Well, let’s move it up actually. That’s kind of out of the way.
Reese: Right there. Right where you had it before, those are the judges. Go ahead and just put that right there.
Rick: Okay, I’ll put that right below that V… update, refresh. And so right now, obviously, it’s not a recognized shortcode so it doesn’t do anything. But this is what the shortcode syntax is going to be… gti case full case equals and then the code name.
And so the next thing to do then is to… and that code name or case equals is the attribute that is referenced here. And so what we’re going to do… the very first thing we want to do is make sure that there actually is one that’s put with a shortcode and that’s it’s the case attribute. And so the very first thing we do is put the attribute in the case in a variable called code. So what this does is this takes this attribute which is an array of attributes and it takes the case element of that array and places it in the code. So in our case, in this specific example that we’re looking at, the case is 1925-mckenna and it’s going to put that in a variable that I called code.
And then it’s going to ask, “Is code blank?” And if code is blank then it’s going to return this error. It says, “Oops, you forgot the case code.” So if we save this right now and upload it to the site, come back over and refresh it, it’s actually going to go away. You’re not going to see it anymore because it doesn’t know what to do with itself. It’s recognized as a shortcode now and the only thing it does at the moment test to see whether or not it’s blank. And so for example, if we cut this out and hit update and refresh it, it’s going to return an error. The error it’s going to return is the condition that we just provided for which is if there is no case number there then return this error. And the error is, “Oops, you forgot the case code.” Okay?
Reese: Got it.
Rick: So let’s go ahead and put that back in here though. Case = 1925mckenna. Update. Okay so in the case of the blank code then return the error. So the next thing we’re going to do though is we’re going to search our database for all post types of the post type court case for that meta key which we called code name and the value that we put in that variable. So come down here, paste that code. We start off with a basic loop argument. We create a variable called initial search args and this is essentially the arguments for a new wp query. That’s what we’re doing here. We’re constructing those arguments and the 2 arguments are the post type and then a meta query.
Reese: I’ve got a question.
Rick: Yup?
Reese: A quick question. On your error checking with the blank, if it’s blank, that does then exit the function we’re building?
Rick: It does. This return right here, automatically ends… it terminates the processing entirely and returns this value. So the way short…
Reese: It exits that then goes… then it keeps doing its thing.
Rick: Yeah it entirely exits and the process of this function is completed. So now we’ve created this search query. We are looking through all of the post types of court cases and we are looking for the meta key of codename and we are looking for the value of code. And code is this code up here, right? It’s the attribute that we placed in here. So it’s going to search all of our court cases for this code name and that’s actually what this query does right here. My query = new wp query initial search args. You place these arguments, this argument here in here.
Now you will routinely have seen something that looks like this, args = array, right? Well, I don’t like to do that. When there is a possibility that exists that I’m going to have multiple sets of arguments, I would prefer to have a descriptive name. So that’s why I said initial search args = array because this is our first search.
Reese: Is that a global name?
Rick: It is not. No, it’s only going to last inside the scope of this function.
Reese: Okay so I can copy this code and use it for the US Constitution or citations or…
Rick: Absolutely.
Reese: It won’t trip on itself.
Rick: You will not trip on itself, no. Okay so then we have our query and then we end up on this very regular-looking loop.
Reese: It also… I’m looking at that and it looks like there’s an array of arrays?
Rick: Meta query is an array of arrays. So meta query… the initial array actually only has one element and it’s the one key value pair. But you could actually have another key value pair, right? You could say, “Get me all of the posts…” well, if this was a more complex arrangement of meta values, you could say, “Give me all of the Supreme Court cases with this code name.” except that our codenames…
Reese: Because I only have… each of these codes should be unique.
Rick: Right. And so since each of these are unique, you’re only using one query element in this meta query array. But the meta query actually allows you to have very complex queries. So you can search for a variety of different conditions of meta values which is why it’s an array. But it has to be an array even if you only have a single element. It still has to be an array. You couldn’t get rid of this array because that’s not how it works. Does that make sense?
Reese: It does.
Rick: Okay. So then we come down to our… so we’ve got our query and now we have the if statement. If my query has posts… now, very often, this if statement doesn’t exist. So you just see wwhile my query has posts… my query doesn’t have posts… do not duplicate. And then you put what you wanted to do inside the loop because that’s this loop is while and in while. The loop that’s created by this query exists between these two terms.
However, I understand that there’s a high likelihood that there’s an error in the attribute that’s entered. And so it may result in no posts being returned. And in the case that no post is returned, what we want to do is return an error message. And we want it to return the error message, “Oops, there’s no case by that code.” So there is the case first when code is blank. And here’s the case second when code doesn’t return an actual case.
Reese: Okay.
Rick: And then the only thing we’re doing in this loop is we are getting the id of the court case that has this meta value. That’s the only thing this loop is doing is just getting that id and putting it in a variable. Once we have it then we’re going to use this to get our post meta and that kind of stuff. But for the time being, the only thing we’re doing in this loop is getting the id of the court case that has this code and that’s it. Okay so we get the id and we assign it to this variable.
Now one thing to keep in mind is that there are a lot of different… and this actually hung me up for a while here today because I’d forgotten this. But there are a lot of different ways in which to reference a post id. Some of them print the post id. Some of them refer to the post id. This, get the id, refers to the post id. And so it’s going to get the value of the post id and then it will place it in this variable. One that I tried to use is this one, the ID. Well unfortunately, what the ID does is it actually prints the id. It doesn’t give it to me for use. It just prints it which is just fine when you’re printing a loop but we aren’t actually printing anything in a loop. We are simply grabbing that idea and placing it in a variable.
Okay once we’ve done that, now we need to get all of the information that we want. And in this case, the information is primarily post meta.
Reese: Right, right.
Rick: Okay, this post meta doesn’t exist so I’m going to delete that one. So full reference = get post meta case id full reference. Okay so the get post meta is a command… or a function that uses a post id and then uses the field name and then determines whether or not the information is returned as a string. We want it to return as a string, not as an array so we use the term true here. So we have get post meta case id full reference and true. Full reference is the name of that post meta field, right?
Reese: Correct.
Rick: I need to show you that. That is this right here.
Reese: That’s what you get with the Verve meta box.
Rick: Verve meta box, exactly. We created… let’s just look at it again anyway so you understand the relationship.
Reese: Now for my…
Rick: Right here is this full reference is what we are referencing then with this piece of code. Get the post meta that is in the field before reference.
Reese: Yeah. As I understand they way Verve meta box works, all it does is it groups together for display custom fields.
Rick: Well no, it also… custom fields are the dumb version of custom post meta. And custom post meta can be lots of different things. But what Verve meta box does is provides you with an interface for entering custom post meta.
Reese: Right. Because when I… after I created my… I was working on some of the other parts and I created the boxes for it. I looked down and I noticed that the custom fields part was populated and it was populated with the name and the information. It didn’t do any of the text that are in the checkboxes. It didn’t do any of that kind of stuff. It just had the information contained in it.
Rick: Well… and that’s sort of a… I mean, there’s technical rationale for that that has to do with the… you can tell your post meta whether or not you want it to show up in the custom field section or not and we didn’t bother with that. So it will show up.
Reese: Okay.
Rick: So we want to get our full reference and we want to get our short reference. So our short reference and we’re going to call this, I guess, our short name which is get the title. Actually, I’m not going to call it short name. I’m going to call it case title. Now the case title is the title of the post based on the post id, right? So we did this loop to get this case id and now we’re using that case id to get the full reference, the short reference, the title of the post and the permalink for the post. So this is why we weren’t entering any link information as post meta because what we’re going to do is we’re going to get that link information from the WordPress database. And so that’s what’s happening here is it’s getting the permalink for the case id and it’s storing it in this variable case URL.
Okay so then the last thing to do is to make a decision about how to treat this. Actually, we don’t really need the short reference in this do we? Yeah actually, we made this all an awful lot simpler.
Reese: Not the same as post title because I really don’t use the post title.
Rick: Yeah, I got you. We’re not going to use the short reference in this because this is the full reference thing. And so we actually don’t have this question either going on. What we’re going to do is simply… yeah actually, we’re just going to leave it there for reference for a moment. And so what we’re going to do is we’re going to create a variable called content equals and now we’re going to put our reference information in. Now the reference information is going to be… is going to look just like this. Okay so it’s going to have a href and then the case URL and then it’s going to have that full reference which is going to include the site and all the rest of that stuff in there. And then the one thing that we don’t have here is our title. So this is where we’re going to get our title equals and then we’re going to break the HTML, go back in the php, grab the case title variable. And the concactenation symbol, go back in the HTML and close our title tag. So now it’s a href equals and there’s the URL. And then title equals the title of the post and then the full reference. Okay and then the one thing that’s missing from this is that we have to actually return the content. You might be sort of tempted to just say echo here but you can’t say echo. You have to return content in a shortcode.
So now we say return $content; And what’s going to happen now is we’re going to get what we want. So if we hit save and upload, come over here and refresh. Here is the Appeal of McKenna and you can see it says Appeal of McKenna when you do the tool tip hovering over it. And that’s the full reference then, right? And when you click on it, it goes over to McKenna. So this shortcode does exactly what we want it to do.
Now here’s what happens if we put the wrong… let’s say we put 1924 in here. So we screw that up. We don’t have the right number in there. Oops, there’s no case by that code. Okay so what we’ve done is we have replaced… we have 3 conditions. We have the condition where we never put an attribute in our shortcode. We have the condition where we put the wrong attribute in for our shortcode and we have the condition where we put the right attribute in for our shortcode.
Reese: Correct.
Rick: Okay and those are the 3 possible results of this shortcode.