How to Display Hidden Custom Field Values in WordPress

Home » Snippets » How to Display Hidden Custom Field Values in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

If you’re a WordPress user and you’ve ever needed to display hidden custom field values on your website, you may have found it to be a bit of a challenge. By default, WordPress hides custom field values from being displayed in the front-end, which can be frustrating if you’re trying to show this information to your website visitors. In this article, we’ll show you how to easily display hidden custom field values in WordPress, so you can showcase the data you need on your website.

					function wpturbo_display_hidden_custom_field_values($post_id) {
    $custom_fields = get_post_custom($post_id);
    foreach ( $custom_fields as $key => $values ) {
        if ( substr( $key, 0, 1 ) !== '_' ) {
            continue;
        }
        echo $key . ': ';
        foreach ( $values as $value ) {
            echo $value . ' ';
        }
        echo '<br>';
    }
}

add_action('wp_footer', 'wpturbo_display_hidden_custom_field_values');
				

The code snippet above demonstrates how to display hidden custom field values in WordPress. Hidden custom fields are typically prefixed with an underscore (_) and are not visible in the default WordPress editor or on the front end of a website.

To retrieve and display these hidden custom field values, we create a function called wpturbo_display_hidden_custom_field_values that takes in the $post_id parameter. This function will be hooked into the wp_footer action, so the hidden custom field values will be displayed in the footer of the website.

Inside the function, we use the get_post_custom function to retrieve all the custom fields associated with the given post ID. This function returns an array where the keys represent the custom field names and the values represent the corresponding field values.

We then iterate through each custom field using a foreach loop. The $key variable represents the current custom field name, and the $values variable holds an array of values associated with that field.

In order to display only the hidden fields (those prefixed with an underscore), we use an if condition to check if the first character of the $key is not an underscore. If so, we continue to the next iteration and skip the rest of the code block.

Next, we echo the custom field name followed by a colon, using the $key variable. Then, we iterate through each value in the $values array using another foreach loop, and we echo each value followed by a space.

Lastly, we append a line break (<br>) to separate each custom field value set.

By adding the wp_footer action hook and calling the wpturbo_display_hidden_custom_field_values function, we ensure that the hidden custom field values will be displayed in the footer of the website.

This code snippet is useful for developers who need to access and display hidden custom field values for debugging purposes or for creating custom functionality based on these values.

Register an account to save your snippets or go Pro to get more features.