Reading values
4 min read
wpmcf_get_field()
wpmcf_get_field( string $group_key, string $field_key, mixed $object = null, array $args = [] ): mixedReturns the raw, unescaped value of one field. Escape it for the place you print it (see Output formats and escaping).
Parameter | Type | Description |
|---|---|---|
| string | Key of the field group |
| string | Key of a top-level field of that group |
| mixed |
|
| array | See below |
Arguments
Key | Type | Default | Meaning |
|---|---|---|---|
| string | implied by |
|
| string or array | none | Reach inside a Group or Repeater value: |
| string | none | Shortcut for a one-segment |
| mixed |
| Returned when the value is |
| string, string, int | none | Name the widget when |
format and wrap are ignored by wpmcf_get_field(); they only affect printing functions.
What you get back
The value is the field type's PHP value, not the database text. Examples verified on a live site:
Field type | Returned value |
|---|---|
Text, textarea, email, URL, WYSIWYG |
|
Number, range |
|
True / false |
|
Select, radio, button group | the stored value ( |
Checkbox |
|
Image, file | attachment ID as |
Group | associative |
Repeater | list of row arrays |
Password | the stored, sealed string (a hash, |
Each type's exact shape is documented under Field types.
When you get the default
default is returned when the result is NULL:
- the field has never been saved, or was cleared;
- the group, the field or the object does not exist, or the object reference is invalid;
- the
pathdoes not exist inside the value; - there is no current post, or no ID for a
user/term/commentcontext.
It is not used for an empty string that was actually saved, and 0, '0' and false are real values.
$phone = wpmcf_get_field( 'shop', 'phone', 'option', [ 'default' => 'Not available' ] );Reaching inside Group and Repeater fields
// Group field "details" holds {"city": "Rome"}
wpmcf_get_field( 'listing', 'details', $id, [ 'path' => 'city' ] ); // 'Rome'
wpmcf_get_field( 'listing', 'details', $id, [ 'sub_key' => 'city' ] ); // 'Rome'
wpmcf_get_field( 'listing', 'details', $id, [ 'path' => [ 'city' ] ] ); // 'Rome'
// Repeater field "items": row index, then sub-field key
wpmcf_get_field( 'listing', 'items', $id, [ 'path' => '1.name' ] ); // second row's name
wpmcf_get_field( 'listing', 'items', $id, [ 'path' => '5.name', 'default' => 'none' ] ); // 'none'Row indexes start at 0. Password fields cannot be placed inside containers (the field builder refuses them), so a path never reaches a password.
{{ }} expressions in values
If a string value contains a {{ ... }} expression, wpmcf_get_field() resolves it, but only when the person who wrote the value may use unfiltered HTML: the post's author for post fields, the user themselves for user fields. Term, widget and options values always resolve; comment values never do. Values that fail the rule are returned with the braces untouched (verified: a contributor's post value stays V {{ wp.post_title }}). The wpmcf/field/resolve_variables filter changes the decision. Details: Variables and hooks.
// Stored: "Call {{ glb.phone }}!" -> returns "Call 555!"Filters
wpmcf/field/value filters the value after it is read (arguments: value, group key, field key, object ID, $args). See Variables and hooks.
Current object
With $object = null and the default post context the function uses the current post (get_the_ID(), or the queried object on a singular page). There is no "current user", "current term" or "current comment": for those contexts you must pass an ID or reference; without one the default is returned.
wpmcf_has_field()
wpmcf_has_field( string $group_key, string $field_key, mixed $object = null, array $args = [] ): boolTrue when the value is not empty. Empty means null, '' or []. 0, '0' and false count as values (verified: a True/false field saved as off and a number field saved as 0 both return true). Takes the same $args as wpmcf_get_field(); use it to skip markup when a field is unset.
if ( wpmcf_has_field( 'listing', 'price' ) ) {
echo '<p class="price">';
wpmcf_the_field( 'listing', 'price' );
echo '</p>';
}Options pages
Values of groups shown on an options page have one shared row per group (a group shown on several options pages has one value per field).
wpmcf_get_option_field( string $group_key, string $field_key, array $args = [] ): mixed
wpmcf_has_option_field( string $group_key, string $field_key, array $args = [] ): boolAccepted $args: path, sub_key, default. The context is always options.
$company = wpmcf_get_option_field( 'site_settings', 'company_name', [ 'default' => get_bloginfo( 'name' ) ] );These are equal to wpmcf_get_field( $group, $field, 'option' ).
Users, terms, comments
$bio = wpmcf_get_field( 'profile', 'bio', 'user_' . get_current_user_id() );
$color = wpmcf_get_field( 'brand', 'color', get_queried_object() ); // a WP_Term on a term archive
$rating = wpmcf_get_field( 'review', 'rating', get_comment() ); // a WP_CommentNo access checks on reads
wpmcf_get_field() is theme code and is not restricted: it returns values of private posts, drafts and other users' profiles if you ask for them. Do not pass user-supplied IDs to it without your own check. The shortcode and the block, which editors and contributors can write, do enforce visibility (see Shortcodes).
Examples
// Archive loop: price of each post.
while ( have_posts() ) {
the_post();
$price = wpmcf_get_field( 'listing', 'price' );
if ( $price !== null ) {
printf( '<span class="price">%s</span>', esc_html( number_format_i18n( (float) $price, 2 ) ) );
}
}
// An image field: attachment ID to HTML.
echo wp_get_attachment_image( (int) wpmcf_get_field( 'listing', 'photo' ), 'large' );
// A checkbox field.
foreach ( (array) wpmcf_get_field( 'listing', 'features' ) as $feature ) {
echo '<li>' . esc_html( $feature ) . '</li>';
}Still stuck? Contact support