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 How to Build a WordPress Plugin How WordPress Plugins Work Examples of WordPress Actions and Filters

David Wang
David Wang
1,407 Points

Passing objects into functions

In this lesson we see several examples of passing objects into functions, e.g.

function make_content_social( $content ) {}

How do we know what object to pass into the function? In other words, how would I know that the object should be $content and not $the_content or $my_content?

Sorry, noob question I know.. :D

1 Answer

Ryan Boone
Ryan Boone
26,518 Points

This is how the hooks work: when WordPress gets to a line where it executes code attached to a particular hook, WordPress itself passes in the correct object for that context. You would then need to look at the Codex or any other WordPress reference to know what that particular object contains.

In terms of what you would name the object in the argument of your function, it makes no difference; it merely represents or refers to the object being passed into the hook, it's not meant to be the actual name of the object.

I would suggest reviewing how PHP functions work, particularly with arguments. When a function is declared, the argument is a variable declaration that's initialized for the function scope (it more or less exists only in the function itself). It's a weird concept to wrap your head around (it took me a while, trust me), but once you form a correct mental concept, it becomes much clearer.

So, you could use the same variable every time, e.g. $my_wp_object, but it's usually better to stick to something that makes sense for that particular hook. The examples given in the Codex are a great starting place.

David Wang
David Wang
1,407 Points

In terms of what you would name the object in the argument of your function, it makes no difference; it merely represents or refers to the object being passed into the hook

Thanks! That clarifies it :)