[do_action]
Executes a hook action.
This shortcode works the same as the WordPress do_action function.
Functions can be hooked on to an action with the add_action function.
The [do_action] shortcode allows you to call the hooked PHP functions from anywhere where you can use shortcodes.
So, you have the possibility to display dynamically generated content in posts and pages.
add_action ('welcome_text', 'display_welcome_text');
function display_welcome_text () {
echo ('Hi User!');
}
then anywhere where the [do_action action="welcome_text" /]
shortcode is used, a 'Hi User!' text will appear.
function display_welcome_text () {
echo ('<h1 style="color: red;">Hi User!</h1>');
}
If you want, you can pass arguments to the hooked function. If the hooked function seems like this:
add_action ('welcome_text', 'display_welcome_text');
function display_welcome_text ($args) {
$tag = isset ($args['tag']) ? $args['tag'] : 'h1';
$color = isset ($args['color']) ? $args['color'] : 'red';
echo ($tag .' style="color: '. $color .';">Hi User!'. $tag . '>');
}
then the
[do_action action="welcome_text" tag="h2" color="blue" /]
shortcode displays 'Hi User!' in blue and in a h2 heading element while, for example, the
[do_action action="welcome_text" tag="h1" color="green" /]
shortcode displays 'Hi User!' in green and in a h1 heading element.
[if]
Displays its content only if the given condition is true.
With the help of the meta_key and meta_value attributes, you can display some content depending on the value of a post meta (such as a custom field).
If you need a more complex condition, use the 'function' attribute of the shortcode.
Attributes: | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
meta_key | Specifies the key of the post meta whose value must be compared with the value given by meta_value. The [if] shortcode displays its content only if the two values are the same. If the meta_value attribute is not set, the content will be displayed only if the value of the post meta is true (casted to boolean). | ||||||||||
meta_value | Specifies a value that should be compared with the value of the meta key identified by the meta_key attribute. | ||||||||||
condition |
Specifies the condition that must be satisfied.
Available conditions are (nocase means not case sensitive):
|
||||||||||
function | Specifies the comparison function. The content of the [if] shortcode will be displayed only if the comparison function returns true. Extra paramaters specified for the [if] shortcode will be passed as an array (key-value pairs) to the comparison function as a first parameter. |
This [if] shortcode only displays its content if the value of the 'my_field' custom field is 'show':
[if meta_key="my_field" meta_value="show"]Hello[/if]
This [if] shortcode only displays its content if the post format is 'link':
[if function="show_on_link_format"]Visible in link post format[/if]
functions.php of your child theme:
function show_on_link_format () {
if ( has_post_format( 'link' ) ) {
return true;
}
return false;
}
The first [if] shortcode only displays its content if the post format is 'link', the second one only if the post format is 'image'. The 'format' custom attribute will be passed to the filter_by_post_format method.
[if function="filter_by_post_format" format="link"]Visible in link post format[/if]
[if function="filter_by_post_format" format="image"]Visible in image post format[/if]
functions.php of your child theme:
function filter_by_post_format ($args) {
if ( has_post_format( $args['format'] ) ) {
return true;
}
return false;
}