Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m

Next lesson playing in 5 seconds

  • Overview
  • Transcript

4.6 Outputting Text With PHP

In this lesson, I'll show you some further options for outputting text from PHP. After all, code that doesn't output anything is not very useful for our WordPress themes or plugins!

4.6 Outputting Text With PHP

Hello, and welcome back to this tuts plus course on learning PHP for WordPress. In this part of the course, I'm gonna show you how to use the echo function to echo out text variables and data in WordPress and in PHP. Now, you'll have seen me using echo quite a few times in this course and I'll be referring back to some of the code that we've already seen. But let's take a look at the simplest way to echo something out. And that's just to echo out some text. Lets just correct that, now what we do is it will simply display hello world on the screen. Simple as that. You can also use echo to display what's fetched when you use a given function or template tag. So here let's use a WordPress template tag to show how you display the title of a post and this is in the loop. So what that does is it displays the post title. Now to do this without echo. You use a different tag, a different template tag called the title instead of get the title. So in WordPress, you'll often see functions that begin with get. They will fetch data from the database, but they won't echo it out or display it. The functions without get such as the title there tend to display it. So it's worth checking because occasionally you might need one or the other and you can check whether there's an equivalent with or without get. So now I'll show you how to echo out some HTML. And I'm gonna do this in a dummy of a template file just to show you how it might work in the loop. So let's imagine that we've got our header. And then the loop opens underneath. Now at the moment I'm working without echo, I'm opening and closing PHP on each line, because I'm working as if I'm in HTML. So you can see here, we've got an article let's close that later on. And then, I’ll put that in an h2. Right, so we're imagining we're getting the header, opening the loop, and then we're opening up our HTML. So we're not in PHP here. So we're going back into PHP to fetch the idea of the post for the idea of the article. We're also going back into PHP for the title and for the content. So you can see I'm repeating the PHP tags again and again. So let me show you an alternative using echo. I'm gonna copy this and paste it and then edit it so you can see how it works. So I'm gonna just have one opening and closing PHP tag here. Get rid of all these tags. I also get rid of those semi colons. Because I'm using those with an echo, which you'll see shortly. And I move the PHP tags to different lines so we can see. Now at the moment that's a real mess. My syntax highlighter is showing that everything's wrong. So let's take a look in here. This is HTML, so I need to echo it out. But that doesn't work, because in here, I've got PHP. So what I do is I close the quotation marks for what I'm echoing. I put space, and then a period, and then a space. Now I need to use get the ID not the ID because I'm fetching it, so that I can then echo it out with echo here, and then a space period space, and then open up quotation marks again. So what I'm doing here is opening up my article using get the ID instead of the ID here. Because echo get the ID is the same as the ID. Let's do that for the second line. So I've got my h2 with the title in it, then I don't need PHP tag before the content, I need to echo my HTML again, much more straight forward this time. And then get sidebar is fine. So you can see from the colors and the highlighting that everything looks fine. So this code here, and this code here do exactly the same thing. But it shows you how you've got a choice of doing it more than one way. And it's up to you which one you prefer. I find that in some cases working like this is helpful. If I've got a lot of HTML I want to work with. It's easier to work in HTML, and not be worrying about all these periods and everything. But if I've just got one line I want to echo out, this can be easier. Another thing that you need to bear in mind, hang on a moment let me just, You can see that there was an error there because I closed PHP and opened it again. So I'm actually gonna not close it there. Another thing that you need to bear in mind is internationalization. If you want to echo out some text like hello world up here, you should actually echo it so that it can be translated by WordPress. And to do that you use one of a range of functions but the one I use most commonly is A and that has two parameters. The first one is the text I really need to learn how to spell, hello. And the second one is what's called your text domain. And you define that when opening up your plugin, or your theme in the comments at the beginning of the plugin or style sheet files. And that then references a folder and a file within your theme or plugin where translation files are stored. I'm not going into a lot of detail on internationalization here, because it is a huge topic, but I will put a link to some resources on it in the notes for this course. So I call it "rmcc" my text name. So now let's have a look at another example where we're using HTML, variables and text. And here I'm gonna go back to some code that I used earlier on in the course. So let's take a moment to look at what I've typed here. Because if you're not familiar with the way I've used echo here and gone in and out of quotation marks, you might feel a bit confused. So if I had typed in echo link, I'd lighten a HREF that would theoretically be valid, although my HTML wouldn't be very good because I wouldn't be closing things off. But what I'm doing, let me just type things back there. So I'm echoing this HTML here and then I'm closing my single quotation marks and I use a period followed by some PHP code. So I'm outside echoing here, I'm just fetching something. So what I'm echoing here, is something I'm fetching using PHP. So for example, if I was to remove that, I could use echo get page link. That would be a valid line of PHP. So I'm echoing this HTML here, then I'm coming out of that going back into my PHP using a period with a space either side, then using my PHP function which is get page link. And that gets you the permalink for the page, the current page using its ID, because that variable here is my page. And then again, I'm using this period with spaces I'm going back into HTML. I'm closing the double quotation marks for my href and then closing my A element here and then putting another single quotation mark because I'm going back into PHP from HTML. I've got a period with a space either side. Then I've got another PHP value that I'm fetching from the database, the post title of my page using that my page variable. Then I've got a period with a space on the side, going back into HTML inside these single quotations, closing off my link and closing off my list item. So I could have typed that a completely different way. But let me just show you. So I've copied this into my demo file for this part of the course. I'll show you how you do it. So there's one version of it, and then, Another, so this one is not using echo. So here I'm gonna close PHP, I'm gonna do my unordered list. I'm gonna open PHP again. Close it again. Now I've got a list item. I'm gonna indent now so we can see exactly what's going on. So let's just get all those lines correctly writes so. So we've got a HREF here so I need to open up PHP again. So here I'm adding echo, because I want to echo out, get page link for my page ID. If I was in the loop, I'd simply use the permalink instead. Now here I need to get rid off, the period and the, quotation mark. I need to close off, my PHP and add my column. So that's my A, and then I need to open up PHP again, And semicolon and close PHP and get rid of a few other extraneous bits down here. I need to open PHP again for my closing brace because this is PHP and then I'm coming back into HTML and then back into PHP again. I find one of the benefits of working with echo here is not constantly opening and closing PHP because if you miss one of these opening or closing PHP tags you'll get white screen of doom, you'll get an error and nothing will be displayed. So that's how you use echo to echo out text functions and variables in WordPress. In the next part of the course, we're going to look at a practical project that puts together some of the skills that you've learned on this course. So we'll create a theme template file for a website. Then incorporates a custom loop, a number of variables and some conditional statements. See you next time and thanks for watching.

Back to the top