How to use WordPress Core as a Reference

wordpress core as a reference
Share this:

Using the Core as a Reference

To use the WordPress Core as a reference you wish to know what to expect within the Core files. In this post we will discuss all about what is within WordPress core files.

Most WordPress Core files contain documentation within the type of code comments. Usually this is often displayed within the header of the file and offers an overall outline of the Core file you’re viewing.

All Core files, apart from pictures, will be viewed employing a text editor program. Depending on your default program settings, you’ll have to be compelled to open up your text editor initial and so open the file instead of simply gap up the file directly.

It’s conjointly useful to use a text editor that has syntax highlight, which means PHP syntax would be highlighted to assist you browse the code easier.
There is a full list of compatible text editors on the Codex at http://codex.wordpress.org/Glossary
#Text_editor.

Inline Documentation

Most WordPress Core files contain inline documentation. This implies every function is explained well directly before the function during a comment block. The subsequent is that the outlined guide for documenting a WordPress function:

This is surprisingly useful in understanding how functions work. The comment includes a brief and long description. It conjointly includes the version of WordPress it had been added in. This helps distinguish new functions added to WordPress once a new version is free.

Available parameters are listed along side the parameter knowledge sort. {a data|a knowledge|an information} sort is that the sort of data that’s needed for the parameter. {for example|for instance|as an example} an ID parameter would doubtless use the int (integer) knowledge sort.

The ultimate piece of data is that the return price. The return price knowledge sort is additionally listed.
All new functions added to WordPress are documented using the preceding template.

Finding Functions

Looking up a function within the Core is that the fastest way to find out how a particular WordPress function works. You’ll be able to see specifically what attributes are allowed to be sent to the function, likewise as what the function truly will and therefore the return values.

To start, check that you’ve got downloaded the most recent version of WordPress domestically to your laptop. You’ll search these files as a reference for WordPress. Open up any text editor you’ve got that may search files (I suggest TextPad for Windows and Textmate for the Mac).

When finding out a function you wish to eliminate calls to it function from your search. Do this by including the word function at the beginning of your search, as in ‘‘function wp_head.’’

Not everything in WordPress could be a function, however this is often a decent place to start out. If you don’t realize any matches, take away function from the start of your search. Conjointly bear in mind to line your text editor to go looking all files (*.*), not just .txt files.

As an example, cross-check the add_post_meta operate. This function is employed to feature metadata information, or custom field knowledge, to a post in WordPress. for instance, we tend to may save the present we tend to ather on each post we produce in WordPress as a post information price.

You wish to understand specifically what values the function expects before you’ll be able to use it. Open up your text editor and search all files in WordPress for ‘‘function add_post_meta.’’ The search ought to produce one result in wp-includes/post.php:

Right away you notice four variables that may be sent to the current function: $post_id, $meta_key, $meta_value, and $unique. Notice the inline documentation listed directly on top of the function. During this case the add_post_meta documentation feels like this:

This is an especially valuable block of content. The comment includes a short description regarding what the function does, during this case ‘‘Add meta data field to a post.’’ The comment conjointly notes once the function was added (since version one.5.0), what global variables are used ($wpdb), and a link on to the Codex article describing this function.

Global variables are variables that are used throughout WordPress to store key items of data that may be accessed anyplace, in this case the add_post_meta function.

For example, the $wpdb global variable is an instance of the WordPress database category containing all of the affiliation and context required to speak to your info. There are a couple of global variables that contain the default or current state info for WordPress, however there are times once you’ll wish to make your own instances of those objects for additional fine-grain or custom management of execution.

There is conjointly info regarding the four variables, together with the variable sort and what every variable is to blame for, likewise as what the expected come values are going to be. During this case the function can come True if successful, and False if not.

This alone is enough info to know however this function works, however let’s poke into the code for a stronger understanding. The primary 2 lines seem like this:

This means if the $meta_key price is empty, come false and exit the function. If the meta key value is empty, then you’ve got no name for the meta data and so can’t add it to the info. Subsequent line within the function is:

This references the worldwide info affiliation object. Functions and data during this category are used for all info manipulation handled in WordPress. up to now this is often pretty customary stuff. Subsequent line truly calls another function:

The first factor you wish to try and do is confirm what specifically wp_is_post_revision does. To try and do this search your WordPress Core files for ‘‘function wp_is_post_revision.’’ Notice the comment header on top of the function:

So currently you recognize this function is employed to see whether or not a nominative post could be a revision. If the post isn’t a revision the function returns False; but, if it’s a revision it returns the ID of the revision’s parent.

Now back to the first code. You’ll be able to confirm that the post ID you sent to the function is verified to not be a post revision, however rather the particular revealed post. If the post ID could be a revision the function assigns the parent ID to the $post_id variable and continues on. Next, the meta key value is passed through:

is a conditional statement that checks 2 values. The primary is $unique, that by default is false. If you pass the $unique value as True once job this operate, the meta key name should be distinctive. If this value is false, you’ll be able to add multiple values to an equivalent meta key, basically making an array of values.

If $unique is set to True, WordPress can execute a info question to see if the meta key already exists. If the meta key exists, the function returns false and exits. Next up in our function the meta value is serialized and slashes are stripped if needed:
$meta_value = maybe_serialize( stripslashes_deep($meta_value) );

Serialized knowledge could be a data coding format in PHP accustomed generate a storable illustration of a price. for instance, serializing an Array can basically flatten the Array values into a string format creating it appropriate to store within the info. Finally the information is prepared to be inserted into the WordPress database:

This line inserts the new post information into the info. Subsequent line deletes any cached post_meta data:

And here’s the ultimate line of the function:

If the function makes it to the top you’ll be able to assume it absolutely was roaring and true would be returned. When viewing this instance it ought to be additional apparent however helpful the WordPress Core code will be.

You learned specifically however this function works by exploring the ASCII text file. All the answers to your queries exist inside the Core therefore it’s essential to own a decent understanding of the way to utilize the Core to your advantage.

What are your thoughts about this post? Let me know in the comments!

Did you like this guide of WordPress Core Files? Share it with your friends on Facebook, Twitter, and Google Plus!

Don’t forget to Subscribe to our Newsletter for more insightful post updates, tips and guides!

See you in the next post.

Stay awesome!

Authored By Aakanksha

Hi, I’m Aakanksha, a computer science engineer and the founder of TheTechPower. I help people make there online presence process easier by empowering them with clean, beautiful and easy to use wordpress themes.

Share this:

Leave a Reply