0

//zodiac_name
function zodiac_name_attachment_field_c2($form_fields, $post) {
    $zodiac_names = array(
        'Aries',
        'Taurus',
        'Gemini',
        'Cancer',
        'Leo',
        'Virgo',
        'Libra',
        'Scorpio',
        'Sagittarius',
        'Capricorn',
        'Aquarius',
        'Pisces'
    );
     $current_zodiac = get_post_meta($post->ID, 'zodiac_name', true);
     $form_fields['zodiac_name'] = array(
        'label' => __('Zodiac'),
        'input' => 'html',
        'html' => '<select name="attachments[' . $post->ID . '][zodiac_name]" id="attachments[' . $post->ID . '][zodiac_name]"><option value="-1">Select</option>',
    );
     foreach ($zodiac_names as $zodiac) {
        $selected = ($current_zodiac == $zodiac) ? 'selected' : '';
        $form_fields['zodiac_name']['html'] .= '<option value="' . esc_attr($zodiac) . '" ' . $selected . '>' . esc_html($zodiac) . '</option>';
    }
     $form_fields['zodiac_name']['html'] .= '</select>';
     return $form_fields;
}
add_filter('attachment_fields_to_edit', 'zodiac_name_attachment_field_c2', 10, 2);
// Save custom fields
function save_zodiac_name_attachment_field_c2($attachment_id) {
    if (isset($_REQUEST['attachments'][$attachment_id]['zodiac_name'])) {
        update_post_meta($attachment_id, 'zodiac_name', sanitize_text_field($_REQUEST['attachments'][$attachment_id]['zodiac_name']));
    }
}
add_action('edit_attachment', 'save_zodiac_name_attachment_field_c2'); //zodiac_name

Jagdish Sarma Asked question May 6, 2024
Add a Comment