Get/Show Custom Field Value with Shortcode Without Plugin

Introduction to Custom Fields

Custom fields in WordPress are a feature that allows additional information to be added to a post or page. They are also known as post meta or metadata.

A key-value pair is utilized in custom field building, where the key serves as a unique identifier for the custom field and the information wanted to be stored is represented by the value. These fields can be displayed on your WordPress front-end using shortcodes.

WordPress, Give post authors the ability to add or assign custom fields to the posts or pages. These custom fields, also known as metadata, can store various types of information, such as:

  • Location: Dubai
  • Book Name: Refactoring
  • Original language:¬†English

Usage and Displaying Custom Fields

For example, if you need to get and display the value of a custom field called “location” using shortcode check the following steps:

  1. Add the following code to the functions.php file of your active theme:
add_shortcode('custom_field', 'display_custom_field_value');

function display_custom_field_value($atts) {
    $atts = shortcode_atts(array(
        'field' => '',
        'post_id' => get_the_ID()
    ), $atts);
    $field = esc_attr($atts['field']);
    $post_id = intval($atts['post_id']);
    if(empty($field)) return;
    return get_post_meta($post_id, $field, true);
  1. In your post or page content, use the shortcode like this: [custom_field field="location"]

Here is an example of how to use the “custom_field” shortcode in a post or page:

[custom_field field="key_you_need_to_show"]

This will display the value of the custom field with the key “key_you_need_to_show” for the current post or page.

If you want to display the value of the custom field for a specific post or page, you can use the “post_id” attribute:

[custom_field field="key_you_need_to_show" post_id="1"]

This will display the value of the custom field with the key “key_you_need_to_show” for the post or page with the ID of 1.

Please note that for the shortcode to work you need to have a custom field with the key ‘key_you_need_to_show‘ in the post/page you are trying to fetch the value from.

Code Explanation:

  • The code creates a shortcode called “custom_field” that can be used to display the value of a custom field.
  • “field” is utilized to specify the name of the custom field to be displayed, and “post_id” is utilized to specify the ID of the post or page that the custom field is to be displayed for.
  • The shortcode_atts() function is used to merge the default values with the shortcode attributes, so if the user doesn’t provide any attribute, the function will use the default values.
  • The $field variable stores the field name passed in shortcode after escaping it using esc_attr(), making it safe to use in get_post_meta() function.
  • The $post_id variable stores the post_id passed in shortcode after converting it to an integer using intval().
  • The if statement checks if the field name is passed or not, if not the function returns nothing.
  • Finally, the get_post_meta() function is used to retrieve the value of the custom field and return it.

Note: This example assumes that you have already created the custom field and added a value to it for the current post or page.

Leave a Comment