New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Start learning with our library of video tutorials taught by experts. Get started

WordPress: Creating Custom Widgets and Plugins with PHP
Illustration by

Introducing the Loop


From:

WordPress: Creating Custom Widgets and Plugins with PHP

with Drew Falkman

Video: Introducing the Loop

At some point when you are working with WordPress, you're going to come to a point where you want to work with the posts. This is, after all, blog software, and post of blogs are all about. WordPress has a special construct to hold this data. It's called the loop. It's used to hold page data as well. In the video, we are going to discuss this key piece of the WordPress architecture. I've created a simple widget here that's going to be used on the front-end. Note, I've use the old widget methodology. This way has been deprecated, and we looked at the new widget API, but it's kind of good to see how it works, in case you see this in some other plugin.
Expand all | Collapse all
  1. 1m 49s
    1. Welcome
      1m 16s
    2. Using the exercise files
      33s
  2. 23m 29s
    1. WordPress overview
      2m 32s
    2. Installing WPI for Windows
      3m 42s
    3. Installing MAMP for the Mac
      3m 25s
    4. Installing and configuring WordPress
      5m 51s
    5. Comparing WordPress 3.0 with previous versions
      2m 57s
    6. Setting up a PHP/WordPress development environment
      5m 2s
  3. 14m 47s
    1. Exploring WordPress plugins
      3m 42s
    2. Administering plugins from the WordPress admin
      5m 23s
    3. Exploring where plugins reside
      2m 51s
    4. Introduction to hooks
      2m 51s
  4. 39m 28s
    1. Creating the plugin PHP file(s)
      3m 12s
    2. More on hooks: Actions and filters
      3m 15s
    3. Installation and activation
      4m 6s
    4. Writing activation code
      3m 45s
    5. Writing an action
      5m 12s
    6. Writing a filter
      4m 15s
    7. About pluggable functions
      2m 1s
    8. Writing a pluggable function
      5m 30s
    9. Using template tags
      2m 46s
    10. Introducing shortcode
      5m 26s
  5. 26m 2s
    1. Widgets and the WordPress Widgets SubPanel
      2m 54s
    2. Comparing widgets and plugins
      1m 8s
    3. Using and customizing built-in widgets
      3m 18s
    4. Creating a new widget
      7m 21s
    5. Writing the constructor and registering widgets
      5m 20s
    6. Enabling configuration of widgets
      6m 1s
  6. 44m 59s
    1. Creating an admin interface
      5m 25s
    2. Saving data to the database
      5m 39s
    3. Securing form submission with nonces
      2m 25s
    4. Options editing post-WordPress 2.7
      4m 8s
    5. Integrating with the WordPress admin menus
      3m 34s
    6. WordPress admin dashboard API
      4m 5s
    7. Using existing options and option editing pages in WordPress
      5m 19s
    8. Using jQuery and AJAX for administration
      14m 24s
  7. 27m 13s
    1. Accessing the WordPress database
      5m 45s
    2. Using the built-in schema
      2m 21s
    3. Accessing data using $wpdb
      5m 15s
    4. Creating new tables
      7m 18s
    5. Inserting data
      6m 34s
  8. 26m 27s
    1. Introducing the Loop
      6m 22s
    2. Using WP_Query()
      3m 11s
    3. Custom filtering and sticky posts
      4m 58s
    4. Using jQuery and AJAX for posts and pages
      11m 56s
  9. 12m 9s
    1. Registering and promoting plugins
      2m 28s
    2. Creating an uninstall function
      5m 53s
    3. Backward compatibility issues
      3m 48s
  10. 15m 3s
    1. Understanding security issues
      11m 20s
    2. Internationalizing your plugin
      3m 43s
  11. 18s
    1. Goodbye
      18s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
WordPress: Creating Custom Widgets and Plugins with PHP
3h 51m Intermediate Nov 04, 2010

Viewers: in countries Watching now:

In WordPress: Creating Custom Widgets and Plugins with PHP, Drew Falkman teaches PHP developers how to create custom functionality for WordPress 2.0 through 3.0 using widgets and plugins. This course starts by installing and setting up WordPress 3.0 on both Mac and Windows, then provides an in-depth look at tasks related to these WordPress add-ons: installing and administering, building and customizing, creating editable options and database tables, working with posts and pages, and utilizing jQuery and AJAX. There are also tutorials dedicated to promoting a widget or plugin, adding security, and localizing the interface. Exercise files are included with the course.

Topics include:
  • Installing WPI and MAMP server solutions
  • Administering WordPress plugins
  • Introducing hooks
  • Writing the PHP for a plugin
  • Using template tags and shortcode
  • Building a new widget
  • Creating an admin interface
  • Accessing the WordPress database
  • Using jQuery and AJAX for posts and pages
  • Registering and promoting plugins
Subjects:
Developer Web CMS
Software:
WordPress
Author:
Drew Falkman

Introducing the Loop

At some point when you are working with WordPress, you're going to come to a point where you want to work with the posts. This is, after all, blog software, and post of blogs are all about. WordPress has a special construct to hold this data. It's called the loop. It's used to hold page data as well. In the video, we are going to discuss this key piece of the WordPress architecture. I've created a simple widget here that's going to be used on the front-end. Note, I've use the old widget methodology. This way has been deprecated, and we looked at the new widget API, but it's kind of good to see how it works, in case you see this in some other plugin.

What I am going to do is I am going to loop through the loop, and I am going to output information. So, by default, the loop is going to hold whatever data is being used on that page. On the front page, it's going to have however many post the user specified. The default is 10. It's going to start with sticky posts at the top, ones that have been marked as sticky, and then it's going to go through in descending order of when they were entered. On other pages, it's going to differ. If you go to a custom page, for example, the loop will just be populated with the data from that page.

If you go to a specific post page, it will be populated with just that post. So let's go ahead and output this widget. This widget is essentially going to show us what's in the loop for each particular page. The way you loop through it is you actually don't access anything called the loop or anything. There's a special function called have_ posts that we can use to loop through it. So the first time you use it; it's always going to go out and get the next available post. If there are no posts at all in the loop, then it will return nothing.

So what we can do is we can use this in an if statement, and say if have posts and then do some particular form of code execution. So if there are no posts, after this it will stop, and we'll be done. However, if there are any posts, we can use the while loop and basically say as long as there's another post in here, go ahead and loop through it. What we are going to do then is we are going to access the posts by calling the special function, the_post.

That will queue up the post and populate a number of template tags with post data. If you remember template tags, these are these functions that we can use to access data. They're built into WordPress. A lot of these work with data that's in the current value of the loop. For example, this comment data is going to come from comments that are in the loop, the_content is going to come from existing content for the current item in the loop, the_title--same thing.

So we can use these template tags to access data inside of the loop. So in our function here, we have our if statement and our while statement looping through the_post. So now we can output what's inside of the post. So we are going to create a div tag for each entry, and inside of this div, we are going to have a link--a href--and the link is going to refer to the permalink for the post, and the way we access that is using a template tag--echo the_permalink. There is our link.

We'll set the title property of our href to the title of the post, and again, we use a template tag. We'll set the same value in between the open and closing a. So, php echo the_title. The last thing we are going to do is we are going to output however many comments are in this current post. So we'll create our parentheses, and then we'll put it in here using another function call comments_number.

The last thing we need to do is close the loops that we have going on. So after this final div tag, we'll say endwhile, which will end our loop, and endif, which will end our if statement. So this is our basic loop that will go through the loop for whatever is on the current page. So, let's go ahead and go into the WordPress admin. We are going to look for our plugin called top posts. We are going to activate it.

This will register the widget. So now we need to go into the Widgets page, look for our Top Posts widget, here it is, and drag it into our web site. If we go down to the front of our web site, we should see our plugin in action. Indeed, here it is. You can see Posts on this page, our Hello world, and there are 3 comments. Note that comments number doesn't just output the number, but it actually outputs the word 'comments' as well. So it's meant for visual display.

So, let's go back in, and let's add another post. If we go to the dashboard, we have this QuickPress plugin. We can use this to quickly add a post. We'll just call this "Update for Today," "More sunny weather in Ventura," Publish. So, now it's been published. If we reload, you can see it's been updated. It's been moved to the top of the list, because by default it's in descending order, and you can see down in our output, it does the same.

If I were to go into one of these, I would also see only that item. Notice the other one disappeared, because on this particular page, the loop was only populated with that data. It's true for pages. The page information is stored just like a post is, inside of the loop, so you can see About gets listed for the loop on this page. If you ever want to start over in your loop, if for some reason you need to do multiple loops, you can use a function called rewind posts to go back to the beginning.

So the WordPress loop offers information for WordPress developers to access the posts and the pages inside of WordPress. We can use this have_posts method to iterate through them, the_post to get it, and then any number of template tags to access all the data inside of it.

Find answers to the most frequently asked questions about WordPress: Creating Custom Widgets and Plugins with PHP.


Expand all | Collapse all
please wait ...
Q: Do I need a web hosting service for this course?
A: You don't need a hosting site to do any testing or development work that’s covered in this course. However, if you want to have your WordPress site available to the public, you will most definitely need a WordPress site. If you are hosting with an independent company, they will need to have PHP and MySQL installed, and there will be some configuration differences, but basically, you can upload anything on your local version to the web site. If you are hosting with Wordpress.com, you will need to add your plugins by uploading them manually through the WP Admin Plugin screen.
 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed WordPress: Creating Custom Widgets and Plugins with PHP.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.