-
Notifications
You must be signed in to change notification settings - Fork 8
Using field values
As well as providing a well-structured API for creating containers and fields, Ultimate Fields provides clean and simple to use functions for getting and updating values. All of those functions are stored in core/api.php
and are thoroughly documented.
All API functions will be explored below, but let's take a quick look an example first.
<p><?php the_value( 'subtitle', 'post_5' ) ?></p>
This example will display the value of the field with name subtitle
, associated with a post that has the ID of 5
. This function is one of many value-related functions. Like most of them, it works with two main parameters:
-
$name
(required): The name of the field, whose value you are looking for. Please note that this is the field name, not label. -
$type
(optional): The item which the field is associated with
The type parameter works with various formats:
Format | Location | Example | Description |
---|---|---|---|
None | Post Type | - | The current post |
Number | Post Type | 5 |
A post with ID 5
|
{post_type_slug}_{id}" |
Post Type | "post_5" |
A post with ID 5
|
"term" |
Taxonomy | "term" |
The current term on category/tag/tax pages |
"term_{id}" |
Taxonomy | "term_10" |
A term with ID 10
|
{taxonomy_slug}" |
Taxonomy | "event" |
The current term on event taxonomy pages |
{taxonomy_slug}_{id} " |
Taxonomy | "event_15" |
A term with ID 15 from the event taxonomy |
"user" |
User | "user" |
The current user, if one is logged in |
"user_{id}" |
User | "user_1" |
The user with ID 1
|
"comment_{id}" |
Comment | "comment_167" |
A comment with ID 167
|
"option" |
Option | "option" |
An option |
"options" |
Option | "options" |
An option |
"network" |
Network Option | "network" |
A network (multisite) option |
"network_option" |
Network Option | "network_option" |
A network (multisite) option |
"widget" |
Widget | "widget" |
The widget that is currently being displayed, if there is one |
"shortcode" |
Shortcode | "shortcode" |
The shortcode that is currently being displayed, if there is one |
This function displays the value of a field, using the same arguments described in Parameters. Example:
<?php the_value( 'subtitle' ) ?>
The value, displayed by this function will be processed based on the settings of the field that it belongs to. If you have an image field and you have set its "Output Type" to "Image" (as it is by default), this function will display a complete <img />
tag.
Returns the processed value of a field. The returned value of this function will be the same value that would be displayed if you were to use the_value()
.
Example:
<?php
$image = get_the_value( 'header_background' );
$template = "The image is displayed next to this text: $image";
get_value
is the simplest of the API functions. It returns a basic value, just like it was saved in the database. If you were to have an image field, no matter what output settings you had selected for the field when creating it, get_value
will return the ID of that image.
$image_id = get_value( 'header_background' );
$attributes = wp_get_attachment_image_src( $image_id, 'full-hd' );
$src = $attributes[ 0 ];
update_value( $name, $value, $type = null )
Whenever you need to update a value, you can use the update_value
function. It takes the same arguments as the rest of the API functions, but with a $value
in the middle.
When you update a value, there will be no checks for its type: Instead it will be blindly saved in the database, so you must be careful.
Example:
<?php
update_value( 'post_subtitle', 'New Subtitle', 'post_5' );
Quick start
- Creating fields and using their values
- Installation
- Administration interface
- Using the PHP API
- Container Settings
Locations
- Overview & Usage
- Post Type
- Options Page
- Taxonomy
- Comment
- User
- Widget
- Shortcode
- Menu Item
- Attachment
- Customizer
Fields
- Fields
- Text
- Textarea
- WYSIWYG
- Password
- Checkbox
- Select
- Multiselect
- Image Select
- File
- Image
- Audio
- Video
- Gallery
- WP Object
- WP Objects
- Link
- Date
- DateTime
- Time
- Color
- Font
- Icon
- Map
- Embed
- Number
- Sidebar
- Complex
- Repeater
- Layout
- Section
- Tab
- Message
Features
- Adding fields to the Customizer
- Conditional Logic
- Front-End Forms
- Administration columns
- Import and Export
- REST API
- JSON Synchronization
- Yoast SEO
Ultimate Post Types
Functions and API
Tutorials