Add custom field and meta box on pages

Last edited:
October 31, 2023
Reading time:
5 mins

/

Blog

/

Development

/

Add custom field and meta...

đź’ˇWe may earn a commision if you subscribe to a service from a link on this page.

Custom fields and meta boxes are powerful features in WordPress that allow you to extend the functionality of your pages and posts. By adding custom fields, you can store and display additional information or data associated with your content. Meta boxes, on the other hand, provide a user-friendly interface for inputting and managing these custom fields. In this article, we will guide you through the process of adding custom fields and a meta box to your WordPress pages, enhancing your website’s capabilities and flexibility.

Why Use Custom Fields and Meta Boxes?

Before we dive into the implementation details, it’s essential to understand why custom fields and meta boxes are valuable:

  1. Customized Content: Custom fields enable you to tailor your page content with specific information. For example, you can add a custom field to store and display a product’s price, author bio, or event date.
  2. Structured Data: Custom fields help structure data on your website. This organized information can be used for various purposes, such as filtering and sorting content, improving search functionality, or creating templates.
  3. User-Friendly Input: Meta boxes provide a user-friendly interface for adding and editing custom field data. This makes it easy for content creators and editors to manage additional content details without delving into the code.
  4. Enhanced Functionality: By adding custom fields and meta boxes, you can extend your WordPress theme or plugins to incorporate specific features or functionalities.

Adding a Meta Box for Custom Fields

While using custom fields directly in the page editor is useful, creating a meta box for them provides a more user-friendly experience. Let’s start by creating a meta box for our “Subtitle” custom field. In this example, we’ll create a custom field to display a page subtitle:

1. Functions.php Modification

Open your theme’s functions.php file and add the following code to create a meta box:

function add_subtitle_meta_box() { add_meta_box( 'subtitle-meta-box', 'Page Subtitle', 'render_subtitle_meta_box', 'page', 'normal', 'high' ); } function render_subtitle_meta_box($post) { $subtitle = get_post_meta($post->ID, 'Subtitle', true); ?> <label for="subtitle">Subtitle:</label> <input type="text" id="subtitle" name="subtitle" value="<?php echo esc_attr($subtitle); ?>" /> <?php } function save_subtitle_meta_box($post_id) { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; if ($parent_id = wp_is_post_revision($post_id)) $post_id = $parent_id; $subtitle = sanitize_text_field($_POST['subtitle']); update_post_meta($post_id, 'Subtitle', $subtitle); } add_action('add_meta_boxes', 'add_subtitle_meta_box'); add_action('save_post', 'save_subtitle_meta_box');

This code does the following:

  • Registers a meta box named “Page Subtitle” for pages.
  • Defines the content of the meta box, including an input field for the “Subtitle” custom field.
  • Saves the custom field data when the page is updated.

2. Update Page with Meta Box

Now, when you edit a page, you will find a “Page Subtitle” meta box in the right sidebar. You can use this meta box to input and update the page’s subtitle.

Adding Custom Fields to Pages

Now we are ready to add a custom field to your WordPress pages’ front-end

1. Edit your theme files for the page you want to edit

Log in to your WordPress (or to your hosting panel) and navigate to the theme files and specifically the page you want to edit.

2. Display the Custom Field

To display the custom field’s value on your page, you can use WordPress template tags. In your page template (usually page.php), add the following code where you want the subtitle to appear:

<?php $subtitle = get_post_meta(get_the_ID(), 'Subtitle', true); if ($subtitle) { echo '<p class="subtitle">' . esc_html($subtitle) . '</p>'; } ?>

This code fetches the value of the “Subtitle” custom field and displays it if it exists.

Enhance your pages with meta fields!

Adding custom fields and meta boxes to your WordPress pages empowers you to customize and structure your content effectively. Whether you’re enhancing the presentation of your pages or creating custom functionalities, custom fields and meta boxes are versatile tools that can elevate your WordPress website’s capabilities. By following the steps outlined in this article, you can unlock the potential of custom fields and meta boxes, making your WordPress pages more versatile and tailored to your specific needs.

🚀 Suggested Reading: Now you know how add meta boxes for meta fields in WordPress pages. Do you want to know how to identify the right hosting company for your WordPress amongst the wide list of available providers?

Leave a Reply

Your email address will not be published. Required fields are marked *