WordPress - Functions tags and filters


Functions

You can invoke functions using the WordPress library: $this->zajlib->wordpress->on();

register_widget($settings)

This registers a widget for use in an Outlast Framework – Custom Widgets section. The $settings are passed as an array and specify the name, description, and the path to the controller method which handles the widget features. Here’s a full, working example:

$this->zajlib->wordpress->register_widget(array(
		'name' => 'Name of the widget',
		'id'   => 'a_unique_id_for_the_widget',
		'reroute' => '/path/to/controller/'
	));

After this, you can create a controller for this – for the above example path.ctl.php / function to_controller() – and place whatever code you wish to run for this widget in this controller. Here’s a possible example:

function to_controller(){
// fetch some data here, then display the template
$this->zajlib->template->show('/widgets/the_template.html');
}

register_template($settings)

This registers a custom template for use in an Outlast Framework – Custom Templates plugin (which is automatically enabled in WordPress). The $settings are passed as an array and specify the name, id, and the path to the controller method which handles the template request. Here’s a full, working example:

$this->zajlib->wordpress->register_template(array(
  'id'=>'example_template',
  'name'=>'Example Template',
  'reroute'=>'path/to/controller/'
));

After this, you can create a controller for this – for the above example path.ctl.php / function to_controller() – and place whatever code you wish to run for this widget in this controller. Here’s a possible example:

function to_controller(){
// fetch some data here, then display the template
$this->zajlib->template->show('/widgets/the_template.html');
}

You can also skip the controller declaration if it is not needed and reroute directly to a template file. Here’s a working example of this:

$this->zajlib->wordpress->register_template(array(
  'id'=>'example_template',
  'name'=>'Example Template',
  'template'=>'example-template.html'
));

In this case no controller needs to be created, so it is simpler and perfectly enough when all your controller would do is display the template.

Be careful: you must set either the template or the reroute parameter – you cannot set both (well you can, but the template would be ignored and only the reroute parameter would be used).

on()

This turns on special WordPress features and turns off the Mozajik autoloading features. You only really need to use this when writing plugins, tags, filters, etc.

off()

This turns off special WordPress features and turns on the Mozajik autoloading features. You only really need to use this when writing plugins, tags, filters, etc.

is_on()

Checks to see if WordPress special features are on or off and returns true if so.


Tags

{% wp_head %}

Creates the WordPress-compatible header. No parameters, {% wp_header %} can also be used.

{% wp_footer %}

Creates the WordPress-compatible footer. No parameters.

{% wp_e 'Text.' 'template-id' %}

Not yet supported fully.

{% wp_sidebar 'sidebar_id' %}

Add a widget area (sidebar) by id. Use register_sidebar in functions.php to create widget areas, then use wp_sidebar to add them to the template. In order to use this you must first register the sidebar in the __wpload() magic method. See above…

{% wp_get_post '7' %}

Get a specific post by ID and set it up for use with a Mozajik {{wp_post}} variable.

{% wp_do_action 'name_of_action' 'arg1' 'arg2' 'etc' %}

Executes a custom hook. WPML uses this for example to display the language selector. You can read more on how this works on the WordPress site.

{% wp_get_search_form %}

Displays the search form.

{% wp_nav_menu 'primary' 'nav' '2' %}

Creates the navigation menu. You must first register the menu in your __wpload() magic method.

The parameters are as follows:
1. theme name – The name of the menu within the theme.
2. menu class – (optional) The class for the wrapper <ul>, defaults to menu.
3. menu depth – (optional) The depth of the menu. If ‘0’ is used (the default), the depth is infinite.

{% wp_simple_field 'simple_field_key' %}

This will display the value of a simple field. Must be used inside the loop.

You can also use this with a parameter as such: {% wp_simple_field 'simple_field_key' variable_name %}. In this case the value of the simple field will be set to this parameter variable, so you can then filter it or use it multiple times on the page.

See simple fields documentation for more information.

{% wp_simple_field_repeatable 'simple_field_key' variable_name %}

Same as wp_simple_field, but the parameter variable name is required, since the result will be an array or a list. Must be used inside the loop.

See simple fields documentation for more information.


Filters

|in_category

Tests if the current post (or any specified post) is assigned to the specified category. Category can be id, name, or slug.

{{ wp_post|in_category:'4' }} Will return true if it is in category 4, false otherwise.

You can also use it in if statements like so:

{% if wp_post|in_category:'3' %}This is in Category 3!{% endif %}

|post_page

If a post is paginated (using the “nextpage” icon), you can use this filter to display only a single page within a post.

{{wp_post.post_content|post_page:'2'}} This will return the second ‘section’ or ‘page’ within a specific post.

A useful example is when you have several different content sections on a page, but you don’t want separate posts for each. This may include boxes, tabs, etcetc. where the content is separate semantically but still coming from a single post.

<div class="tab1">{{wp_post.post_content|post_page:'1'}}</div>
<div class="tab2">{{wp_post.post_content|post_page:'2'}}</div>

|wp_featured_image

This returns the featured image for a given wp_post object.

{{ wp_post|wp_featured_image:'large' }} Returns the large image for wp_post. By default, the thumbnails is returned when no parameter is specified.

Outlast Web & Mobile Development (c) 2019 | Privacy Policy |