Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Greg Schudel
Greg Schudel
4,090 Points

WordPress : Filter vs Action vs Loop

I am trying to better understand the concepts of the Wordpress core functionality.

What are the real distinctive differences between a Filter, an Action and a Loop. I asked this before in another post. Please be concise, if possible., I don't want to confuse myself or overthink this :)

Action - So I understand that action is about adding a behavior or content at specific parts of the theme

Filter - The ability to grab content that is already being displayed and customize how it is shown/displayed.

Loop - This is where I am fuzzy. Does it just mean the ability to take content that has been displayed in other parts of the site and display them in a custom way. If so, how is this different than a filter?

Finally, aren't filter and actions both basically loops since they edit/add information/content at specific intervals of the WP theme?

1 Answer

You're close but there are a couple of things that need to cleared up.

Actions and Filters are Hooks, not loops.

Action hook adds something while WordPress is loading, you can see this when you enqueue your CSS or JavaScript files. Here is an example:

<?php 
function my_scripts_method() {
    wp_enqueue_script( 'scriptaculous' );
}
add_action( 'wp_enqueue_scripts', 'my_scripts_method' );

Filter hook is when you want to modify data of a function. here is an example:

<?php
function wporg_filter_title($title)
{
    return 'The ' . $title . ' was filtered';
}
add_filter('the_title', 'wporg_filter_title');

The difference between the two by looking at the examples are Actions are adding to WordPress core, and filter is modifying a WordPress function.

Loops are just that a loop, nothing more. Just like in JavaScript using the while loop in PHP/WordPress is the same. we create a condition and check that condition and if true or not, or while something is true or not we tell it to do something.

Here is a common loop in WordPress:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <h2><?php the_title(); ?></h2>
    <?php the_post_thumbnail(); ?>
    <?php the_excerpt(); ?>
<?php endwhile; else: ?>
    <?php _e( 'Sorry, no posts matched your criteria.', 'textdomain' ); ?>
<?php endif; ?>

Here is The Loop in action. We are saying, if there are posts, and while there are posts get the post and do this. We state where we end the loop with the endwhile statement then end the if statement after.

Hooks do not loop they are modifiers or hooks that you "hook" into WordPress while loading. A loop is just that looping through some conditional statement and performing a process that you state.

Greg Schudel
Greg Schudel
4,090 Points

Great! So clear!

However, are filters for native functions to WP or custom ones, or both? I'm pretty sure both right?

Generally Filters are used with WP functions/code. You can create a custom filters for a plugin. Giving the user options. So yes, if you want to modify something that already exists and change that behavior, that is where a filter comes into play.