Yes No Meta Field Creation
December 23, 2024 ยท AI Conversations
### 1. Register the Meta Field
Hook into WordPress using `register_meta` to register a custom meta field for your custom post type.
### Example Code:
Add this to your theme's `functions.php` file or a custom plugin:
function register_yes_no_meta_field() {
register_post_meta('your_custom_post_type', '_yes_no_field', [
'type' => 'string',
'description' => 'Yes/No Field',
'single' => true,
'sanitize_callback' => 'sanitize_text_field',
'auth_callback' => function() {
return current_user_can('edit_posts');
},
]);
}
add_action('init', 'register_yes_no_meta_field');
### 2. Add the Meta Box in Admin
Add a meta box to your custom post type's editor to allow the user to select Yes or No.
function add_yes_no_meta_box() {
add_meta_box(
'yes_no_meta_box',
'Yes/No Option',
'render_yes_no_meta_box',
'your_custom_post_type',
'side',
'default'
);
}
add_action('add_meta_boxes', 'add_yes_no_meta_box');
function render_yes_no_meta_box($post) {
// Retrieve current value
$value = get_post_meta($post->ID, '_yes_no_field', true);
wp_nonce_field('save_yes_no_meta_box', 'yes_no_meta_box_nonce');
?>
<p>
<label for="yes_no_field">Select Yes or No:</label>
<select name="yes_no_field" id="yes_no_field">
<option value="" <?php selected($value, ''); ?>>None</option>
<option value="yes" <?php selected($value, 'yes'); ?>>Yes</option>
<option value="no" <?php selected($value, 'no'); ?>>No</option>
</select>
</p>
<?php
}
### 3. Save the Meta Field
Ensure the value is saved when the post is updated.
function save_yes_no_meta_box($post_id) {
if (!isset($_POST['yes_no_meta_box_nonce']) ||
!wp_verify_nonce($_POST['yes_no_meta_box_nonce'], 'save_yes_no_meta_box')) {
return;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (!current_user_can('edit_post', $post_id)) {
return;
}
if (isset($_POST['yes_no_field'])) {
$value = sanitize_text_field($_POST['yes_no_field']);
update_post_meta($post_id, '_yes_no_field', $value);
}
}
add_action('save_post', 'save_yes_no_meta_box');
### 4. Display the Meta Field
To display the Yes/No field in the front-end, you can retrieve its value using `get_post_meta`.
$value = get_post_meta(get_the_ID(), '_yes_no_field', true);
echo $value === 'yes' ? 'Yes' : 'No';
### Notes:
– Replace `your_custom_post_type` with your actual custom post type slug.
– The `_yes_no_field` is a meta key; you can change it as needed.
– The `sanitize_text_field` ensures the input is cleaned before saving to the database.
This setup will provide a Yes/No dropdown for your custom post type in the admin and save the value to the post meta.
### 1. Register Multiple Meta Fields
Define and register multiple fields in the `register_post_meta` function.
function register_custom_meta_fields() {
$fields = [
'_yes_no_field' => 'Yes/No Field',
'_custom_text_field' => 'Custom Text Field',
'_custom_number_field' => 'Custom Number Field',
];
foreach ($fields as $key => $description) {
register_post_meta('your_custom_post_type', $key, [
'type' => 'string',
'description' => $description,
'single' => true,
'sanitize_callback' => 'sanitize_text_field',
'auth_callback' => function() {
return current_user_can('edit_posts');
},
]);
}
}
add_action('init', 'register_custom_meta_fields');
### 2. Add the Meta Box in Admin
Modify the `add_meta_box` and `render_meta_box` functions to handle multiple fields.
function add_custom_meta_box() {
add_meta_box(
'custom_meta_box',
'Custom Meta Fields',
'render_custom_meta_box',
'your_custom_post_type',
'side',
'default'
);
}
add_action('add_meta_boxes', 'add_custom_meta_box');
function render_custom_meta_box($post) {
$fields = [
'_yes_no_field' => 'Select Yes or No:',
'_custom_text_field' => 'Enter custom text:',
'_custom_number_field' => 'Enter a number:',
];
wp_nonce_field('save_custom_meta_box', 'custom_meta_box_nonce');
foreach ($fields as $key => $label) {
$value = get_post_meta($post->ID, $key, true);
?>
<p>
<label for="<?php echo esc_attr($key); ?>"><?php echo esc_html($label); ?></label><br>
<?php if ($key === '_yes_no_field'): ?>
<select name="<?php echo esc_attr($key); ?>" id="<?php echo esc_attr($key); ?>">
<option value="" <?php selected($value, ''); ?>>None</option>
<option value="yes" <?php selected($value, 'yes'); ?>>Yes</option>
<option value="no" <?php selected($value, 'no'); ?>>No</option>
</select>
<?php elseif ($key === '_custom_number_field'): ?>
<input type="number" name="<?php echo esc_attr($key); ?>" id="<?php echo esc_attr($key); ?>" value="<?php echo esc_attr($value); ?>">
<?php else: ?>
<input type="text" name="<?php echo esc_attr($key); ?>" id="<?php echo esc_attr($key); ?>" value="<?php echo esc_attr($value); ?>">
<?php endif; ?>
</p>
<?php
}
}
### 3. Save Multiple Meta Fields
Loop through the fields to save their values.
function save_custom_meta_box($post_id) {
if (!isset($_POST['custom_meta_box_nonce']) ||
!wp_verify_nonce($_POST['custom_meta_box_nonce'], 'save_custom_meta_box')) {
return;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (!current_user_can('edit_post', $post_id)) {
return;
}
$fields = ['_yes_no_field', '_custom_text_field', '_custom_number_field'];
foreach ($fields as $key) {
if (isset($_POST[$key])) {
$value = sanitize_text_field($_POST[$key]);
update_post_meta($post_id, $key, $value);
}
}
}
add_action('save_post', 'save_custom_meta_box');
### 4. Display the Meta Fields
Retrieve and display the values in the front-end.
$yes_no = get_post_meta(get_the_ID(), '_yes_no_field', true);
$text = get_post_meta(get_the_ID(), '_custom_text_field', true);
$number = get_post_meta(get_the_ID(), '_custom_number_field', true);
echo '<p>Yes/No: ' . esc_html($yes_no) . '</p>';
echo '<p>Text: ' . esc_html($text) . '</p>';
echo '<p>Number: ' . esc_html($number) . '</p>';
### Notes:
– Replace `your_custom_post_type` with the slug of your custom post type.
– Add or remove fields in the `$fields` array as needed.
– Use the appropriate input types (e.g., text, number, select) for your meta fields in the meta box rendering function.
This approach is flexible and allows you to manage multiple meta fields dynamically.