The meta_form WordPress PHP function prints the form in the Custom Fields meta box.
Usage
meta_form($post);
Parameters
- $post (WP_Post | Optional) – The post being edited. Default: null.
More information
See WordPress Developer Resources: meta_form
Examples
Display the meta form for a specific post
This example displays the meta form for a specific post with the ID 123.
// Fetch the post object $post = get_post(123); // Display the meta form for the post meta_form($post);
Display the meta form within a custom meta box
This example displays the meta form within a custom meta box on the post edit screen.
// Register the custom meta box function my_custom_meta_box() { add_meta_box('my_custom_meta', 'My Custom Meta', 'my_custom_meta_callback', 'post'); } add_action('add_meta_boxes', 'my_custom_meta_box'); // Callback function to display the meta form function my_custom_meta_callback($post) { meta_form($post); }
Display the meta form on a custom post type edit screen
This example displays the meta form on a custom post type edit screen.
// Register the custom post type function my_custom_post_type() { register_post_type('my_cpt', array( 'label' => 'My Custom Post Type', 'public' => true, 'supports' => array('title', 'editor', 'custom-fields') )); } add_action('init', 'my_custom_post_type');
Customize the meta form using CSS
This example demonstrates how to customize the appearance of the meta form using CSS.
// Add CSS to style the meta form function my_meta_form_styles() { echo '<style> .postbox-container .inside .form-table { background-color: #f9f9f9; padding: 10px; border-radius: 5px; } </style>'; } add_action('admin_head', 'my_meta_form_styles');
Save the custom fields data from the meta form
This example saves the custom fields data from the meta form when the post is updated.
// Save custom fields data function my_save_post_custom_fields($post_id) { // Loop through $_POST data foreach ($_POST as $key => $value) { // Check if the key starts with 'meta-' if (strpos($key, 'meta-') === 0) { // Update the post meta update_post_meta($post_id, $key, $value); } } } add_action('save_post', 'my_save_post_custom_fields');