Shortcodes
4 min read
Two shortcodes print values anywhere shortcodes run: post content, widgets, page builders and do_shortcode().
[wpmcf_field]
Prints one field value, escaped, through the same code as wpmcf_the_field() (see Output formats and escaping).
[wpmcf_field group="listing" field="price"]
[wpmcf_field group="listing" field="price" id="42" wrap="false"]
[wpmcf_field group="profile" field="bio" context="user" id="3"]
[wpmcf_field group="site_settings" field="company_name" context="options"]Attributes
Attribute | Default | Description |
|---|---|---|
| required | Field group key. Without it (or without |
| required | Top-level field key. |
|
| Object ID. |
|
|
|
|
|
|
| none | Dot path inside a Group or Repeater value: |
| none | One-segment shortcut for |
| none | Printed (escaped) when the field has no value (never saved or cleared). Only applies when you write the attribute: |
|
|
|
| none | Name the widget when |
Group, field, context, format and sub_key values go through sanitize_key(), so group="LISTING" works. Unknown attributes are ignored.
Because the shortcode takes an id, it accepts IDs and contexts only, not reference strings like user_3: use context="user" id="3".
Examples (verified)
Shortcode | Output |
|---|---|
|
|
|
|
| the |
| the second row's |
|
|
|
|
|
|
{{ }} expressions
Expressions inside a field value are resolved under the same rule as wpmcf_get_field(): only when the value's author may use unfiltered HTML. See Reading values.
Who can see what
Shortcodes (like blocks) can be typed by contributors, so an explicit id may only point at things the current visitor could see anyway. Anything else prints an empty string. The PHP functions are not restricted, because theme code is trusted.
| Printed when |
|---|---|
| the visitor has |
| it is the current user, or the author of the post being viewed (author boxes), or the visitor has |
| the term's taxonomy is publicly viewable |
| the visitor can |
| see below |
no | not checked: it is the page being rendered |
Options values (context="options") inside post content print only when the post's author has the capability of an options page that shows the group: manage_options by default, or the capabilities returned by the wpmcf/options/capabilities filter. A contributor therefore cannot publish your options-page values by writing a shortcode. Outside post content (a template, a widget) the value prints; during REST and Ajax requests (editor previews) the current user needs the capability.
The filter wpmcf/shortcode/can_view (arguments: visible, context, object ID) overrides every decision above:
add_filter( 'wpmcf/shortcode/can_view', function ( bool $visible, string $context, int $id ): bool {
return $context === 'user' ? true : $visible; // show every user's fields
}, 10, 3 );Verified matrix (field title set on every object):
Object | Admin | Subscriber | Logged out |
|---|---|---|---|
published post | yes | yes | yes |
private post | yes | no | no |
draft | yes | no | no |
password-protected post | yes | no | no |
the subscriber's own user | yes | yes | no |
another user | yes | no | no |
term | yes | yes | yes |
approved comment | yes | yes | yes |
pending comment | yes | no | no |
Password fields
A Password field never prints through the shortcode, for any format, unless the wpmcf/field/the_password filter allows it. See Password helpers and security.
[wpmcf_global]
Prints a Global Data variable, escaped, like wpmcf_the_global().
[wpmcf_global key="phone"]
[wpmcf_global key="phone" wrap="false"]
[wpmcf_global key="site_url" format="url" wrap="false"]Attribute | Default | Description |
|---|---|---|
| required | Variable key. Case-insensitive ( |
|
|
|
|
| Same rules as above; the wrapper class is |
| none | Printed (escaped) when the variable is missing or empty. |
There are no visibility rules for [wpmcf_global]: Global Data is site-wide and public by design. Password variables never print.
Using shortcodes in PHP
echo do_shortcode( '[wpmcf_field group="listing" field="price" id="' . (int) $post_id . '"]' );Prefer wpmcf_the_field() in templates; it avoids shortcode parsing and accepts reference strings.
Troubleshooting
Symptom | Cause |
|---|---|
Nothing prints | The value is empty; a Password field; the visibility rules above (private, draft, password-protected, another user); a typo in |
Raw | The place does not run shortcodes. Wrap in |
An | Use a field type whose |
Wrapper span is unwanted inside an attribute | Add |
Still stuck? Contact support