$wf - the point of entry into the entire API
The MasterPress API is quite vast but it offers a very simple entry point to access all of the functionality within – a single global PHP variable $wf, which is an instance of the MEOW class (an extension of the WOOF class).
“wf” stands for WordPress Framework – intentionally very short so that it is quick and easy to type.
The general pattern of code using the MasterPress API will be a method call or property access on the $wf object to obtain either a single object instance of another class, or a WOOF_Collection of such objects. You’ll often then chain additional property methods and calls on those objects, many of which can be done on a single line of code.
The examples below show some typical uses of the API. Note that in each example, we first start off with a method call on the $wf object, and then proceed from there:
Example 1: Working with a single taxonomy term
Example 2: MasterPress API - Looping over a post collection using $wf
Here’s how you might achieve something similar to Example 2 with standard WordPress template tags:
Example 3: WordPress API - Looping over a post collection with the template tags
You’ll note that Example 2 and 3 are quite different – where MasterPress favours drilldown property access on objects, most actions in WordPress revolve around calling functions declared in global scope – the so called template tags in WordPress. Furthermore, most of these calls either require some type of ID to be passed in to provide context, or additional function calls which change the global state of the current environment – this requires more code and can be rather counterintuitive at times (try explaining a function like setup_postdata or wp_reset_query to a novice developer).
The Importance of the global keyword
The $wf variable is exists in PHP’s global scope, which means that you will need to globalise the $wf when using it inside custom functions, including WordPress hooks, filters and handlers, as in the example below:
Example 4: Using global inside functions
Failing to add the global declaration on line 10 above would cause the function to fail with a fatal error when trying to render the example.
An important note on partial templates
Partial templates in WordPress, including files such as header.php, footer.php, and sidebar.php also require the $wf variable to be declared global, since these are included (required) in the context of WordPress’ load_template function, which is shown in the source-code excerpt below:
Example 5: load_template
Partial templates are brought into your templates as if they are inside a function body, so the you should add the global declaration at an early point in your template, as in the header.php example below:
Example 6: using global inside header.php
The most reliable way to work with $wf is to use the global declaration everywhere you intend to use it.