Tips on how to to debug your WordPress code

## [00:00:00] Introduction

[00:00:00] **Aurooba Ahmed:** You are listening to viewSource, a conversation around tech, web development, and WordPress with hosts Aurooba Ahmed, that's me and Brian Coords.

[00:00:11] Hello, we are recording on a morning today. How are you doing, Brian?

[00:00:16] **Brian Coords:** I'm doing good. I can hear my children melting down in the background, some nice gloomy clouds outside. It's a, it's a beautiful day.

[00:00:27] **Aurooba Ahmed:** It's a beautiful day. It's very beautiful here today actually. But today we are going to take a little break from our React series and dive into another topic that I think is a big part of our lives every single day at work. Um, and to start that off, I'm gonna ask you, what is your favorite PHP function?

[00:00:53] **Brian Coords:** Oh man. Coming from like, of React and, uh, blocks. I'm like, I'm like, all right, PHP wake, wake back up. Bring back the, the PHP thing. I mean, I, I don't know if it's because it's the topic of our episode, but definitely the one that came to mind as the one I find myself typing the most is probably like, like var_dump().

[00:01:14] You know, like it's, it's the one I can think of consistently as like, y ou help, like you helped me in a way that, uh, you know, a for loop doesn't like emotionally impact me. You know what I mean?

[00:01:28] **Aurooba Ahmed:** That's true. That's true. I think that we'll get into this a little bit later, but I have a little snippet that I've been using for like nearly a decade, that is another version of var_dump(), and the keyboard like snippet that I haven't attached to is called DPD. So in my head, I don't even call it var_dump(). I say, let me DPD this.

[00:01:52] **Brian Coords:** Oh yeah.

[00:01:53] **Aurooba Ahmed:** Uh, yeah. And then it will like, do it for me.

[00:01:56] **Brian Coords:** That's the, um, is it laravel that has the, like DD like the die and dump function, which is like, what I, what I'll often do is in WordPress, I'll do like a var_dump() and then I'll do a wp_die() right after it. Um, and that's generally what I go and I'm sad to say like I don't have a snippet for it, but I also have recently noticed that Copilot does it for me, like very often when I'm like starting it and it knows it's better cuz it knows what I, it always knows like, what do you wanna dump? And so that actually is like, I'm like, ah, I don't need to snippet now, so.

[00:02:34] **Aurooba Ahmed:** no, totally. I agree. Um, well, you know, I, I would agree. I think var_dump() and its associated cousins are my favorite functions for sure. I use them way too much or no, no. I use them as much as I need and that is okay. And.

[00:02:51] **Brian Coords:** Yeah, don't var_dump() shame people.. There's, there's no shame in the var_dump().

## [00:02:55] How do you debug?

[00:02:55] **Aurooba Ahmed:** There really isn't. Um, and sometimes I have multiple mul, multiple of them, like sitting on a page for a long time while I'm like figuring stuff out. So I totally get that. But that goes into, you know, this conversation really well because we are talking about how we debug, especially inside WordPress. But the concept of debugging is not special to WordPress.

[00:03:19] There are some things in WordPress that can help us. But first and foremost, how you debug is kind of a mindset. So walk me through, you know, a developer comes to you with a problem. How, what is the first thing that you start thinking of, or how do you approach helping them debug the problem that they've presented?

[00:03:40] **Brian Coords:** Yeah, so I, I feel like I do this a lot, which is like, here's a problem: can you tell me what's wrong and how to fix it? And inevitably the first thing I do is like, I have to say like back up 10 steps because I often don't know what project you're working on, what you're trying to accomplish, what you know the code looks like at this point, what phase in the project you are.

[00:04:02] So like, usually the first step is to be like, what are you actually trying to accomplish? What is the, like intended result? And then what is the result you're getting? And then I need to see all the code, every part of the code. I don't need to see the line of code or a chunk of code. I need, like push that code to a branch and I need to see the code for myself cuz um, that I need all of the context first and then I need to absorb it and like let it soak in.

[00:04:29] **Aurooba Ahmed:** Yeah, I think one of the pitfalls that a lot of people fall into, and I've certainly been there myself, when you're deep in a project, you are trying to figure out this problem and you start zeroing in on just the area where you start to notice the problem, and so you just get into the space where that's the only area you're focusing on.

[00:04:49] But you know, I think you and I both know from experience, a lot of the times, that's not where the problem happened. This is a side effect that's happening now. So when a developer sends me a screenshot of like a one tiny little bit of code, it's like, this is where the problem is happening. And I'm just like, that's not helpful, because if this was the only area the problem was happening, you would've figured it out already, you know?

[00:05:10] **Brian Coords:** Yeah, I make the same mistake cuz you and I will send each other things too, where you just hit that mental block and you're just like, can you just look at this? Like, cuz you want that fresh perspective on it, which you can always get by um, I think you had tweeted earlier about just like, taking a nap is a, like taking a nap and giving it back to yourself is one way.

[00:05:29] But if we're sending stuff to each other, you do fall into that trap where you're like, oh, this person's really smart, so I'll just send them like the line and they're gonna know immediately. But it's like, no, everybody needs like the full context. The more information you can provide, the more clear you can be about what you're actually trying to do and what's not working, uh, the better it's gonna be for that person.

[00:05:49] **Aurooba Ahmed:** Agreed. Exactly. And I think even like, just the way you said, sometimes you and I do that to each other and it, it, it's okay when we're working in the same code base, so the other person is like fairly familiar with it. But if you're not working in the same code base, then it's even more important that one either you provide a far larger screenshot with far more context and more stuff that you write down for the person or you provide the full code base so that the person can start playing with it, or the, the very third option is, you know, you get up on get, get on a Zoom, you live share, you screen share, and then you play with the code together where the person who you're trying to help is the one doing the actual work, but you're like telling them or working with them to figure out what to do.

[00:06:36] Right. I do that a lot, like constantly.

[00:06:39] **Brian Coords:** Yeah. One trick is like when you're going to make that screenshot, and if you're on a Mac, you do like the command shift four, right? And you're like gonna draw your little box. Like if you hit that space bar, it turns into like, take a picture of my entire VS Code window with my integrated terminal, but also maybe the files, which might give me a little clue or, you know, all of it.

[00:07:01] I wanna see all of it. Gimme that full screenshot of the whole app, not just like the little, the little piece, you know?

[00:07:08] **Aurooba Ahmed:** A hundred percent. I remember, I think this was just last week, someone actually gave me that full screenshot and the error was a tiny little bug that I could see in their integrated terminal. And I was like, there's your bug. There's your bug right there. You know? And they hadn't been looking at that.

[00:07:22] **Brian Coords:** You put npm run tart or something, you know, dumb like that. Yeah. It's

[00:07:27] **Aurooba Ahmed:** Yeah, exactly. Uh, and then the code, the source code did not get compiled correctly, and therefore nothing was working.

[00:07:35] **Brian Coords:** On a similar, I've been having a similar thing where sometimes I'll hide my terminal and then I'll be working on something and it's not working and it's not working and it's not working and it's not working. And then I realize I opened the terminal and it stopped compiling my code like 15 saves ago cuz of an error or it, I made a new file and it's not tracking that file so it hasn't been compiling anything.

[00:07:55] So like that big picture view, you're like, oh yeah, that, that often saves you.

[00:08:01] **Aurooba Ahmed:** A hundred percent. And. Sometimes when that kind of thing happens to me, I feel really dumb. But it's also like, just a reminder, it might be time to take a small break because you're too close to it and you're not thinking straight, you know?

[00:08:14] **Brian Coords:** Yeah. Oh, for sure.

## [00:08:16] Live debugging a little problem

[00:08:16] **Aurooba Ahmed:** So today I created a little bug for us to live debug, and what I wanna do is, so, okay, stepping back for one moment.

[00:08:27] I think that a lot of the times people don't understand or know what debugging looks like. And I know I used to have imposter syndrome about how I used to debug, and I think it would be really cool if we just debug in live, like literally, I have a bug. I don't know what's going on. And I'm coming to you for help and you and I are gonna screen share and figure this out together because I think it's important to remember that no matter how much you know, there's always more to know.

[00:08:55] **Brian Coords:** Yeah. Now I'm a little nervous cuz I haven't, I haven't actually looked at the code, so I.

[00:09:00] **Aurooba Ahmed:** Well, that's good. It'll be like a proper, real experience.

[00:09:03] **Brian Coords:** All right.

[00:09:04] **Aurooba Ahmed:** Okay, so I'm gonna share my screen and give you a little context for what's going on.

## [00:09:10] Giving context for the bug

[00:09:10] **Brian Coords:** Yeah, tell me like what you're working on. Let me know, like what you're trying to get done here. Where things are going wrong. Um, I think for context, this is all gonna be, we're, we're really focused on PHP, right? A little like PHP WordPress. So this is not. I think we could do a whole separate episode about debugging when you're doing blocks or working in the block editor, but this is more like classic PHP front end WordPress stuff.

[00:09:37] **Aurooba Ahmed:** Yeah. Okay, so I'm sharing my screen and in front of me you can see a little blog post that I have running, it's called little title, "I wrote a little PHP inside a block theme", and what I am trying to achieve here is spit out a formatted custom date section below the title. And I wanna be able to choose, like if it was Monday, maybe choose, show the date like this, or maybe if it was Tuesday, show me something else instead.

[00:10:08] So in order to do that, the very first thing I did was I created some ACF options and there's an options page called Custom Date. In here I have a repeater, and in each repeater I have the field day and there's Monday, Tuesday, you know, all the days of the week. And then I can choose a date output that is the second field, which has the options of published date, modified date, or custom.

[00:10:33] And here what I've set up is just three of these repeated rows, one for Monday. So on Monday I wanted to show the published date. On Wednesday, I wanted to show custom and it should say "it's viewSource recording day". And then Friday I want to show the modified date. So this is what I've set up. The problem I'm having is when I go to the post, this doesn't show up.

[00:10:56] Now in order to do this, I added a filter to the_content. That's where I am sort of spitting out this logic, but even though I'm doing that, when I go to the front end, there's nothing there. Help me.

## [00:11:13] Using Query Monitor

[00:11:13] **Brian Coords:** Help you. So the first thing I would say is have you installed Query Monitor the WordPress plugin on your WordPress site? That's my first question.

[00:11:24] **Aurooba Ahmed:** Yes.

[00:11:25] **Brian Coords:** Awesome. For context, Query Monitor is, is a plugin I always have on, always all the time when I'm working on a WordPress site, like it's the first thing I install, like it, it is the first place to step. So on the top of the screen you'll, when you have query monitor enabled, you'll see like in the admin bar, an area that says, like, it has some like statistics on it, like how long the page loaded, like the total load size, um, stuff like that. How many queries are running. But if you hover over it, You can see basically like a list of all these different things you can dig into.

[00:12:00] You can see what are the database queries, what are, what templates are being pulled, what scripts are being enqueued, what API calls are being run in the backend, like what's the request? What, you know, what like is getting served over WordPress? Like what conditionals are true and false, like you can see everything. And so there you go. Like you've clicked it open and now on the front end and back in the WordPress, you can dig through all of these different sections and just, you're basically seeing like what is WordPress thinking about? Literally everything in this moment right now.

[00:12:31] **Aurooba Ahmed:** Mm-hmm.

[00:12:31] **Brian Coords:** but it also shows you if you have PHP errors, which I can see because it's bright red that you have one PHP error. Okay.

[00:12:40] **Aurooba Ahmed:** Yeah, so I've clicked it open to see what it's showing, and it's showing me basically a table, but the main message it's saying is the foreach argument must be of type array or object, null was given, and then it's giving me the location of where I can find this bug.

[00:12:59] **Brian Coords:** Yeah, and I actually, what I love is I hit that plus icon on the location and it gives you like that full kind of stack trace. So it's like, it's telling you that it's in your theme, in your functions.php file on line 69, but it also tells you it's this function, the vs_custom_date_info(). And that function is called because of the apply_filters() running on the_content.

[00:13:20] So you've clearly, there's a function you added as a filter on the_content, and then you can see all the way down. Um, the rest of it is all just like WordPress generating the content of this, uh, this page.

[00:13:33] **Aurooba Ahmed:** totally. Okay, so now that we've seen this error, what would you like me to do?

[00:13:38] **Brian Coords:** Well, I guess we should open up your functions.php file. We should go to line 69. We should look at your function and see what did you put in this foreach loop or this for loop that it's just so mad about?

[00:13:49] I mean, cuz it was a red Fi. It wasn't even like an orangey like warning that, I mean, that was a red PHP notice.

[00:13:56] **Aurooba Ahmed:** Yeah, it hated me. Um, my code hates me right now. Okay, so I have the code editor open and I've opened up the functions file and gone to the uh vs_custom_date_info() function and line 69. I see that, uh, well it says foreach and then there's a variable called $custom_date, and I wanna use it as key to value, key value pair inside the foreach.

[00:14:24] But since it said null, I'm guessing that the custom date is like not actually what I think it needs to be.

[00:14:31] **Brian Coords:** Okay. Okay, so I can actually see what's going on here. Do you want me to just, I can just tell you, I can tell you right now.

[00:14:38] **Aurooba Ahmed:** Oh, I know exactly what's going on here too,

[00:14:40] **Brian Coords:** Well it's your bug

[00:14:42] **Aurooba Ahmed:** but it is my bug. Yeah, like walk me through what you saw, like how you're thinking about it. What's going on in your brain?

## [00:14:48] Talking through the problem

[00:14:48] **Brian Coords:** Okay, so we know that the error is on line 69, right? So we know that for each custom date it says it wants an array cuz you're looping through something or something Iterable and it's not, it's, it's getting a, like a null value or a false or something like that.

[00:15:02] So at this point I would probably go backwards and I would look at where does custom date come from? So line 65, we see that custom date you're pulling in get_field() from ACF, like get_field() is an ACF function that's basically a way to pull, um, values of your custom field. And so there's a few things that I would think about here.

[00:15:24] So first off, when you gave us the walkthrough of where your custom fields were defined, like these date labels, um, that was an ACF Options page. And so when you use get_field() with an options page, you do have to pass at that second parameter that says options rather than just. like Because like right now it's looking for this custom field on the actual post that it's rendering right now.

[00:15:49] But the field wasn't a post meta field, it was on the options page. So that's the first thing I would do.

[00:15:56] **Aurooba Ahmed:** Okay, so what I'm missing here is that we need to call this from the options page, right?

[00:16:02] **Brian Coords:** Yeah. We need that second parameter that tells ACF. Um, this is an options field.

[00:16:07] **Aurooba Ahmed:** Mm-hmm.

[00:16:08] **Brian Coords:** Do you do option or do you do options? Which one do you like?

[00:16:12] **Aurooba Ahmed:** I like option.

[00:16:13] **Brian Coords:** You don't like that extra s just,

[00:16:16] **Aurooba Ahmed:** no? Do you, do you use options?

[00:16:19] **Brian Coords:** I think, um, yeah, I think I do. Yeah. I

[00:16:22] **Aurooba Ahmed:** I think, I think of it as this is a field which is an option that I want, so that's why I always use option.

[00:16:30] **Brian Coords:** so for context you can pass it either one, and it'll work either way. But I think like, oh, this comes from my options. So I'll pass it options and type that extra

[00:16:40] **Aurooba Ahmed:** That's fair

[00:16:40] **Brian Coords:** s every time. I don't know that, it just looked weird to me when I saw it there as option, but yeah, you can pass either one.

[00:16:46] **Aurooba Ahmed:** that's that's funny

[00:16:47] **Brian Coords:** Um,

[00:16:48] **Aurooba Ahmed:** Okay, then what do I do? I've, I've changed this.

## [00:16:51] Adding conditionals appropriately

[00:16:51] **Brian Coords:** so, uh, two things. One is that I would also, if it were me, because I love conditionals, um, as you called out on me earlier this week, I really would wrap the four each in a conditional just to make sure, like, because what if it does return null? Like what if you actually don't have anything said in that options field?

[00:17:10] It feels like it should be a conditional around it. Right.

[00:17:14] **Aurooba Ahmed:** I think in this case I've made it. I'm set up the options fields themselves so that they can never be empty. But yes, you're right. If you don't have that set up in your, in your actual ACF options, then it would make sense to say, you know, yeah, that, make sure that it's not empty. I'm just letting, uh, Copilot do the work for me of telling me that, Hey, if custom date is not empty, then only run this for each, but then that also means no.

[00:17:44] Okay. The rest of my function is totally fine.

[00:17:47] **Brian Coords:** Okay. Should we go back to the front end and see like

[00:17:52] **Aurooba Ahmed:** Yeah.

[00:17:53] **Brian Coords:** you know, now our, like, so now we found like, okay, it came from the wrong, it was pulling the wrong value, it was trying to pull it from the post. Like that's more of an ACF error. Um, you just kind of forget to like pass it that second parameter sometimes.

[00:18:05] Um, and then your four each was trying to loop over a null value. So it is yelling at you. It's not, it's not gonna break a site. It's not a fatal error, but, you know, um, I don't ever.

[00:18:15] **Aurooba Ahmed:** It's not doing what I wanted it to do.

[00:18:16] **Brian Coords:** Yeah, I don't ever like to turn over a site with query monitor showing anything. Anything. Not even a notice. I don't want anything in there.

[00:18:23] I want it clean.

[00:18:25] **Aurooba Ahmed:** That's fair. Okay, so I'm sharing my screen again. I'm gonna refresh the page, and now I see a date and it's telling Go ahead.

[00:18:36] **Brian Coords:** I was gonna say, can we open query monitor just to make sure? I wanna make sure we didn't add, cuz sometimes, you know there's stuff in there. All right. No errors. Okay. No errors.

[00:18:42] **Aurooba Ahmed:** Yeah, there's no errors. We're all good, so that's good. But we're seeing the date, but this is the wrong date. This is not what I actually wanted to show because today is a Wednesday and what I had set up in my custom date was that on Wednesday, I wanted to say it's viewSource recording day, instead. So we don't have an error.

[00:19:06] Something is showing up, but it's the wrong stuff.

## [00:19:09] Debugging a functional logic error

[00:19:09] **Brian Coords:** Okay. Okay, so now, so now we have functional logic error. This is gonna be a lot harder. This is gonna be, this is really gonna push me cuz uh, it's pretty easy to see like a nice little PHP notice, but this isn't something that Query Monitor particularly is gonna help you with. Right.

[00:19:27] **Aurooba Ahmed:** Yeah. This is something that you now had to go into and run with your favorite function

[00:19:33] **Brian Coords:** Okay.

[00:19:33] **Aurooba Ahmed:** a little bit, right?

[00:19:35] **Brian Coords:** So let's go through, let's go through the logic a little bit because, uh,

[00:19:39] **Aurooba Ahmed:** Yes.

[00:19:40] **Brian Coords:** there's a few ways you can go about this, right? Um,

[00:19:43] **Aurooba Ahmed:** Mm-hmm.

## [00:19:44] Let's var_dump()!

[00:19:44] **Brian Coords:** you know, you can var_dump() basically everything sometimes, and then just like look at it and kind of do that math or we can kind of go through it. Um,

[00:19:52] **Aurooba Ahmed:** Yeah, like step it through logically, right? Like what is actually happening in this function, because we haven't actually looked at the whole thing yet.

[00:19:59] **Brian Coords:** yeah. So you're getting the date, you're, so let's go through like starting at line 59, you're getting the Post ID of the post we're looking at

[00:20:07] **Aurooba Ahmed:** Mm-hmm.

[00:20:08] **Brian Coords:** line 61, you're getting the date and line 63, you're getting the modified date

[00:20:14] **Aurooba Ahmed:** Mm-hmm.

[00:20:14] **Brian Coords:** that seems good. And line 65, you're getting the field options. And then on line 67, you're starting an empty string because, you know, the way the content works is you're just, basically, you, it's a string and you can kind of add stuff to it.

[00:20:30] So we're gonna make our own string to kind of combine at the end.

[00:20:34] **Aurooba Ahmed:** Mm-hmm.

[00:20:35] **Brian Coords:** Um, okay, so let's go into your, foreach. So we're gonna, you're looping through that custom date that we looked at before, which was like an ACF repeater field, and it had like some options, right?

[00:20:48] **Aurooba Ahmed:** Yeah, so it has day and you know, date, like the kind of output you wanna do, and then there's a conditional output like field. So if you choose custom, you have another field where you can type in what that custom output you want it to be.

[00:21:01] **Brian Coords:** And theoretically that's what should come out today, right? Like we should be, we should be getting the custom field. Okay? So for each, we're looping through for each day. That's the value of day. So that should line 70 The day should say Wednesday, cuz today,

[00:21:18] **Aurooba Ahmed:** Yes.

[00:21:19] **Brian Coords:** like it's gonna loop through, but we're looking for the instance where it's Wednesday.

[00:21:23] **Aurooba Ahmed:** Mm-hmm.

[00:21:25] **Brian Coords:** Then date output, published equals, oh, those equal. How do you, how do you do those equal signs like that? Come on. Doesn't that, doesn't that hurt your face to look at that?

[00:21:35] **Aurooba Ahmed:** I love it. I like having it all connected.

[00:21:38] **Brian Coords:** How many equal signs is that?

[00:21:40] **Aurooba Ahmed:** it's three equal signs.

[00:21:41] **Brian Coords:** It's like 7 equal signs combined into one. What?

[00:21:44] **Aurooba Ahmed:** two would look like that with only two lines, and three will have three lines. Yeah. This is like coding. Coding fonts have this as like a ligature that you can use

[00:21:53] **Brian Coords:** Who does this to themselves?

[00:21:54] **Aurooba Ahmed:** And the number of lines everyone, everyone.

[00:21:57] it, but a ternary and that's kind of an annoying thing sometimes, especially when you are debugging. Sometimes that can make it harder to debug, which is why they're not super encouraged in WordPress standards. Right.

[00:22:11] **Brian Coords:** Not super encouraged? I mean, do you have your coding standards turned on? Because this would be all red squiggly lines if I pulled this up in my code editor.

[00:22:19] **Aurooba Ahmed:** I do have my coding standards turned on.

[00:22:21] **Brian Coords:** Okay. It's not yelling at you for this one?

[00:22:24] **Aurooba Ahmed:** Nope, it's not yelling.

[00:22:26] **Brian Coords:** Okay, so walk me through what is, what is date output that you're, you're deciding this variable equals date output equals.

[00:22:33] So explain to me what, um, what do you think.

[00:22:36] **Aurooba Ahmed:** so I have this field called date output, and that can be either set to published or modified or custom. So here what I was doing was saying, Hey, if the the field itself is set to published, then this date output here should be set to the current post date because it's just the published post date.

[00:22:58] Otherwise, give it the modified date because what I decided to do instead was have a custom variable underneath and say, Hey, if the date output set in the repeater was custom, then you know you have a custom value and you can set that in custom, and then I can check that later. And instead of like checking what the date output was, was, and then doing it separately, like I'm just doing it together and without like having to do a bunch of if conditionals,

[00:23:26] **Brian Coords:** Okay. I guess in my mind, what I would think is the first thing I'd want to know is if I'm even on the right day, you know what I mean?

[00:23:34] **Aurooba Ahmed:** right.

[00:23:35] **Brian Coords:** Like before I did any logic, I would want to know like, is the day of the post, the actual day that I'm looking at? Does that make sense? So when you're, when you're doing your, your filter and you're saying on, you're saying if the published date of the post is, is a Wednesday, not if, like today, right now, the current date I'm looking at is Wednesday.

[00:23:58] **Aurooba Ahmed:** Yeah, yeah. If the, we are, we're checking the published date of the post and we're saying, Hey, if, if that matches, you know, and we have a setting for it in the repeater, then, uh, do the output that we've set for that date. So, like, if today, if this published date is Wednesday, show me it's viewSource recording day.

[00:24:18] **Brian Coords:** Okay. Okay, so then I'm probably correct that like lines 71 to 72, like we don't need to get to just yet, right, because

[00:24:27] **Aurooba Ahmed:** Yeah.

[00:24:27] **Brian Coords:** we like we're doing extra logic that might not even matter because we only wanna like deal with this. If this, if this is later. Okay. So I think we should just start variable dumping some things, cuz I think like, that's gonna make me super happy.

[00:24:43] Okay. All right.

[00:24:44] **Aurooba Ahmed:** What do you want a variable dump?

[00:24:46] **Brian Coords:** okay. I want a variable dump day, which is the value of the day. So on line 70 or, yeah, that's fine. You can do it right there. I wanna know what.

[00:25:01] **Aurooba Ahmed:** Yeah. Do you want me to do them separately? Do you want them like in each line?

[00:25:08] **Brian Coords:** Yeah, do them on a separate line cuz it'll tell us what line, when you variable dump, it tells you like what line you dumped it on,

[00:25:13] **Aurooba Ahmed:** Mm-hmm.

[00:25:14] **Brian Coords:** and then I want to know what day the post was. Right. So what I want is that's where you're using get_the_date() with the uh, uppercase N. Right?

[00:25:26] **Aurooba Ahmed:** Yeah,

[00:25:27] **Brian Coords:** I want that one too

[00:25:28] **Aurooba Ahmed:** we can do that. Okay. Let's do that too. All right. Those are the two you want right now.

[00:25:35] **Brian Coords:** Should I get anything else while we're here?

[00:25:37] **Aurooba Ahmed:** Mm. You tell me you're, you're the one helping me debug right now.

[00:25:42] **Brian Coords:** Okay. Then I think the next one I would want, since we're here, I also wanna know what that date output is.

[00:25:49] **Aurooba Ahmed:** Date output. Okay.

## [00:25:50] Thinking about what should happen

[00:25:50] **Brian Coords:** Yeah. So this is what's gonna happen.

[00:25:52] **Aurooba Ahmed:** Mm-hmm.

[00:25:53] **Brian Coords:** It's gonna loop through and we're gonna get a few different values cuz custom date, we're gonna loop through. We're gonna see this three times. And what I think I'm gonna see, this is what I think is gonna happen is I'm gonna see the day.

[00:26:05] So I'm gonna see like Monday, Wednesday, Friday, cuz you had done Monday, Wednesday, Friday in your repeater field. I'm gonna see the day of the actual post that we're looking at, which should stay the same for all three instances.

[00:26:18] **Aurooba Ahmed:** Mm-hmm.

[00:26:18] **Brian Coords:** And one of those instances should match, cuz it should say Wednesday. So it should be like Monday, Wednesday, Friday.

[00:26:24] Then it should say Wednesday, Wednesday, Wednesday. And then I think for that date output, I should see the values for that.

[00:26:30] **Aurooba Ahmed:** Yeah, so one thing I'm gonna say is that we're not gonna see the words, we're gonna see the number for each of day of the week. That's what N does. It's like Monday is one, Tuesday is two, et cetera.

[00:26:41] **Brian Coords:** okay.

[00:26:41] **Aurooba Ahmed:** Okay, so I'm gonna share my screen. I'll go to the front end again. Okay. So I'm gonna hit refresh. And now at the very, very top of my screen, we're seeing a bunch of really nice var_dump()'s.

[00:26:57] **Brian Coords:** Yeah, and I would say there's a few things to think about. One is that, um, essentially When you're running a filter on the_content, like that's happening way before like the actual content is being rendered. It's like happening way, like while WordPress is still thinking before it's rendering. And so, um, sometimes when you do like a variable dump.

[00:27:18] You might not see it right away, cuz it might be somewhere in your hidden header or, you know what I mean? Like, sometimes it's somewhere where it doesn't show up on your page. Yeah. So always like, scroll up or even check your source code and like, look for it, because sometimes you, you just don't, you don't find it.

[00:27:34] **Aurooba Ahmed:** yeah. I do this a lot. And so here I'm actually gonna show open up the inspector tools and make that a little bit bigger. Um, Because I'm using Local, I turn on the Xdebug option in there. And what that does is regular var_dump() actually sometimes puts it in like a weird table sometimes or does other things.

[00:27:54] But if you have Xdebug turned on for Local, it'll for, it'll put it inside this class called xdebug-var-dump. So if you just want, you can like search for that with like command F or something if you can't find it, and it'll show you where it is. So that's something that's really handy.

[00:28:13] **Brian Coords:** Okay, so, so I am correct that the line 73, we have 1, 3, 5. That's Monday, Wednesday, Friday.

[00:28:21] **Aurooba Ahmed:** Mm-hmm. Mm-hmm.

[00:28:21] **Brian Coords:** Line 74. We have 3, 3, 3. Wednesday, Wednesday, Wednesday, and my line 75, the custom date is April 12th. April 12th. April 12th. And that to me, doesn't seem, that to me, tells me that all the date stuff is working out pretty well.

[00:28:42] That stuff is good. There was something about your conditional that I would wanna return to when we get to it.

[00:28:48] **Aurooba Ahmed:** Mm-hmm.

[00:28:50] **Brian Coords:** So far, that's all pretty good, but I'm kind of curious why line 75 is the same for all three because it's supposed to be returning different values. So what that means is either it's wrong or I didn't understand what that line was supposed to do.

[00:29:02] Let's be honest. It could be either one of those two things. So let's go back into the code and let's do some more variable dumping,

[00:29:09] **Aurooba Ahmed:** Okay.

[00:29:10] **Brian Coords:** I think we can agree that the matching of the post date and the date in the loop works pretty well.

[00:29:18] **Aurooba Ahmed:** Yes. Okay. So yeah, so the, the thing that we're seeing that is always the same is the, the date output, um, here. So that's always April 12. Um, Which would make sense because I published it and I modified it today.

[00:29:37] **Brian Coords:** Okay.

[00:29:38] **Aurooba Ahmed:** Right? So,

[00:29:39] **Brian Coords:** Yeah, cuz that's pulling from the actual post. Yeah. Okay. That makes sense. This shouldn't change. There's no change in that value.

[00:29:46] **Aurooba Ahmed:** that's right. Yeah. So then what am I missing?

[00:29:51] **Brian Coords:** Okay. So the next thing I would wanna do is let's keep 73 and 74 because I do want to know when, like I want to know

[00:30:00] **Aurooba Ahmed:** Okay.

[00:30:00] **Brian Coords:** which iteration of the loop I'm in. You know, I want to know that I'm in that like Wednesday loop. All right. But now in line 75. Let's do this first. Let's variable dump custom.

[00:30:11] **Aurooba Ahmed:** Okay.

[00:30:12] **Brian Coords:** to be clear, I'm not going into your if statement yet because I feel pretty confident that it's not pa like you're, I feel like you're conditional on line 76 is not hitting true at all.

[00:30:25] Like so I don't wanna do anything in there cuz I feel like nothing will come out. I mean, that I could do, I could put a little like var_dump() inside

[00:30:31] **Aurooba Ahmed:** But we are seeing an output, so it is getting into the if conditional.

[00:30:35] **Brian Coords:** Oh yeah, that's true because you're not changing the date, you're actually just adding it. Okay. Okay. I take it back. Okay, so let's see what the, let's see what custom comes up with.

[00:30:50] **Aurooba Ahmed:** Okay.

[00:30:50] Okay, so custom is coming up as false every single time.

[00:30:55] **Brian Coords:** Okay. That's not good cuz we wanted a custom date. We wanted it to say it's viewSource recording time. Right. Did I get that right?

[00:31:02] **Aurooba Ahmed:** That's right. Yeah.

[00:31:04] **Brian Coords:** I almost I was gonna do it like in the Saturday night Live voice, you know, like the old guy. He does like his, the the intro voice.

[00:31:11] **Aurooba Ahmed:** that's how I think about it when I was writing it.

[00:31:14] **Brian Coords:** Yeah. All right, so then let's go back. So it should definitely not be false every time.

[00:31:18] It should be true in that third statement.

[00:31:21] **Aurooba Ahmed:** Yes, exactly. So custom right now is saying, Hey, if $date_output is custom, then grab the custom value. Otherwise it's false.

[00:31:35] **Brian Coords:** Yeah, but it shouldn't be date output cuz

[00:31:38] **Aurooba Ahmed:** That's right, yeah

[00:31:38] **Brian Coords:** you set date output on line 71. So like,

[00:31:41] **Aurooba Ahmed:** Yes.

[00:31:44] **Brian Coords:** like you overrode. I don't want date output your variable. I want date output from your repeater field. I want like value, date, output. I want date output that was coming outta there. Yeah, yeah, yeah.

[00:31:54] **Aurooba Ahmed:** Yeah, exactly. And this is what happens when you have not good variable names. You confuse yourself.

[00:32:04] **Brian Coords:** yeah. I don't know what's worse, your variable names or your equal sign, like voodoo magic. Both of those trick me out. All right. Save this. Run this again I think what's happening is, We wanted to know if the value was gonna be custom or not.

[00:32:19] **Aurooba Ahmed:** Yes. Yes, right. exactly

[00:32:21] **Brian Coords:** All right.

[00:32:22] **Aurooba Ahmed:** Okay, so now in one of the iterations for Wednesday, it is showing me it's viewSource recording day, and if I go down into the actual post, I can see that it's showing me the actual custom value now, so you solved it.

## [00:32:37] Providing code feedback

[00:32:37] **Brian Coords:** That was pretty good. I got a little, uh, like halfway through. I thought, man, I don't know if I'm gonna solve this. Sometimes you're looking at a bug and you're just like, I didn't eat enough breakfast for this. I think, I think you, I think if I were giving feedback, I would say a few things. I would say the first thing I would do is on each of those lines, I would add a comment above it, explaining what you think it needs to do. And it's more for yourself than for anything.

[00:33:05] Like, it's more just like, think through what you're doing, um, think through all the steps. And then I would think also about what, basically what you want, what you want these variables to, to exist. Because I did feel like it seemed a little.

[00:33:24] **Aurooba Ahmed:** like there's a lot of work happening before it needs to happen.

[00:33:28] **Brian Coords:** Yeah.

[00:33:29] **Aurooba Ahmed:** Yeah, In a lot of situations, I would say even like the dates that I'm grabbing, like I don't need to grab these dates this early on. You know, I can just grab them when I need to grab them.

[00:33:40] **Brian Coords:** yeah, and I would say WordPress has gotten pretty good at like caching a lot of that stuff. So it's not like you're like hitting the database for it, you know? You're just pulling it out of the like cache. But like again, yeah, I would. I'd be like, I think you could rewrite this to be a lot more performant.

## [00:33:54] Being more verbose in your code

[00:33:54] **Brian Coords:** And I also think this is where I fall on the side of really liking that WordPress makes you be more verbose and not use so many like inline conditionals like that because I do think it takes the amount of time you saved typing you wasted in my, my low amount of brain cells having to process what was happening in those.

[00:34:16] So I, I'm a fan of being a little bit more descriptive with what you're doing to save time in the long run.

## [00:34:24] Naming variables better

[00:34:24] **Aurooba Ahmed:** Yeah, I mean, I think that I do enjoy ternaries, uh, you know, doing, sort of evaluating things in the actual setting or declaring of a variable, but I think that there's a lot of situations where that does make it more complicated and it's not suited for every situation. So in this particular case, I think the biggest problem that I see when I look at this code is that the variable names for day, date output, they're just a little bit confusing and they're not quite as accurate as they should be. Like a lot of the times when you're looking at your code, if you haven't named your variables correctly, one, you definitely then need a lot more in in inline documentation because your code is not self-explanatory. And two, you, if you, if you're not clear, then it's harder to read it yourself and find the problem, which is what happened here, because I have this date output, but it probably shouldn't have been called date output. Or maybe the original repeater field should not have been called date output.

[00:35:21] Like this is, this is fairly confusing to look at that. I have a variable called date output, but then I also have a repeater field that does something different that has date output. I mean, that's, that's not helpful, especially if you come back to it, you know?

[00:35:34] **Brian Coords:** Yeah.

[00:35:34] **Aurooba Ahmed:** I agree with like what, what you're seeing.

[00:35:36] Plus I just think that it's important to be a little bit more intentional with how you name things.

## [00:35:43] Choosing the right kind of conditionals

[00:35:43] **Brian Coords:** Yeah. The other piece, I think too is like, There was three different types of date outputs, right? You could have the publish date, the modified date, and the custom date, but by using two separate conditions, like a conditional or a ternary is like this or that, but it's really not great when there's three use cases.

[00:35:59] So like now that I've looked at it, I'm like, I understand the logic of what you're doing, but like I would've probably used like a switch case kind of a thing because there's three different options and then I could very easily see because like if it's custom, then you don't actually need line 71 at all.

[00:36:17] But like you're doing the logic ahead of time. Yeah. There I think.

[00:36:22] **Aurooba Ahmed:** exactly, and I think that this is something that I see all the time. Switch. Switch case is so good, but so few people use it and this is the perfect scenario for using Switch Case in instead of foreach where, and it's a little bit more performant too whenever you have like those different options because it's just like switching and quickly evaluating before running through through the whole thing, which is really, really nice.

[00:36:46] **Brian Coords:** Yeah. And putting all of that in the conditional,

[00:36:49] **Aurooba Ahmed:** Yeah, that was a pretty successful debugging session though, I think.

[00:36:54] **Brian Coords:** Yeah, that's, I mean, I, I'll, I'm gonna check my Apple watch for what my heart rate went up to in the middle. Cuz there was like a moment where I was like, cuz I think the hardest part of helping another person is. Is getting that, like we talked at the beginning, it's like getting the full context, getting the information.

[00:37:10] It's like, it's not always finding the bug, it's finding like, what am I, you know, understanding the code and like understanding the code, knowing the whole picture. That's way more important than like the tools of debugging.

[00:37:21] **Aurooba Ahmed:** I think you and I could probably talk about this a lot more, and there's more to cover with debugging, so we might do another episode on another angle of debugging as it goes with everything we talk about. But I think that was a, that was pretty good.

## [00:37:34] Conclusion

[00:37:34] **Brian Coords:** Yeah, I, I'm looking forward to the barrage of tweets about why we didn't use xdebug for this, uh, case, um, which we could do, which is a different, a different mindset and a different approach, and I think we could maybe do a, a similar version with, with that approach. But, um, but I, I, you know, nothing wrong with little variable dump when you're just trying, just trying to get through

[00:37:56] **Aurooba Ahmed:** Solve a little problem. Yeah. All right. Well thank you for your help on my imaginary, uh, bug there on our little theme and I'll see you in the next episode.

[00:38:06] **Brian Coords:** All right. See you then.

[00:38:08] Visit viewsource.fm for the latest updates and links to the show notes. Review and subscribe to viewSource in iTunes, YouTube, or wherever you get your podcasts.

Creators and Guests

Aurooba Ahmed
Host
Aurooba Ahmed
(she/her) Developer building bespoke #WordPress solutions, tools, and blocks. My name is pronounced "oo-ROO-ba" — Default to kindness, folks.
Brian Coords
Host
Brian Coords
WordPress developer and writer blah blah
Tips on how to to debug your WordPress code
Broadcast by