tags - How to output get_tags array list to select box

admin2025-06-06  3

I have a custom meta box. In it there are input fields, as well as a select box. What I want, is to have the get_tags array list to be outputted as the values in the select box.

I currently have the select box option values hard coded. I want this to be dynamic depending on the tags on the website.

Here is the code;

    // Add the Meta Box
function spk_project_info_meta_box() {
    add_meta_box(
        'spk_project_info_meta_box', // $id
        'Project Information', // $title 
        'show_spk_project_info_meta_box', // $callback
        'post', // $page
        'normal', // $context
        'default'); // $priority
}
add_action('add_meta_boxes', 'spk_project_info_meta_box');


// Field Array
$prefix = 'spk_project_info_';
$tags_array = get_tags();
$custom_meta_fields = array(
    array(
        'label'=> 'Client',
        'desc'  => 'A description for the field.',
        'id'    => $prefix.'client',
        'type'  => 'text'
    ),
    array(
        'label'=> 'Our Role',
        'desc'  => 'A description for the field.',
        'id'    => $prefix.'role',
        'type'  => 'text'
    ),
    array(
        'label'=> 'Year',
        'desc'  => 'A description for the field.',
        'id'    => $prefix.'year',
        'type'  => 'text'
    ),

    array(
        'label'=> 'Select Box',
        'desc'  => 'A description for the field.',
        'id'    => $prefix.'select',
        'type'  => 'select',
        'options' => array (  
        'one' => array (
            'label' => 'Option One',
            'value' => 'one'
        ),
        'two' => array (
            'label' => 'Option Two',
            'value' => 'two'
        ),
        'three' => array (
            'label' => 'Option Three',
            'value' => 'three'
        )
      )
    )
);

// The Callback
function show_spk_project_info_meta_box() {
global $custom_meta_fields, $post;
echo '<input type="hidden" name="spk_project_info_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
    echo '<table class="form-table">';
    foreach ($custom_meta_fields as $field) {
        $meta = get_post_meta($post->ID, $field['id'], true);
        echo '<tr>
                <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
                <td>';
                switch($field['type']) {
                    case 'text':
                        echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
                            <br /><span class="description">'.$field['desc'].'</span>';
                    break;

                    case 'select':
                        echo '<select name="'.$field['id'].'" id="'.$field['id'].'">';
                        foreach ($field['options'] as $option) {
                            echo '<option', $meta == $option['value'] ? ' selected="selected"' : '', ' value="'.$option['value'].'">'.$option['label'].'</option>';
                        }
                        echo '</select><br /><span class="description">'.$field['desc'].'</span>';
                    break;
                }
        echo '</td></tr>';
    } // end foreach
    echo '</table>'; // end table
}

function save_spk_project_info_meta_data($post_id) {
    global $custom_meta_fields;
    if ( !wp_verify_nonce($_POST['spk_project_info_meta_box_nonce'], basename(__FILE__) ) ) 
        return $post_id;
    if ( defined('DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return $post_id;
    if ('page' == $_POST['post_type']) {
        if ( !current_user_can( 'edit_page', $post_id ) )
            return $post_id;
        } elseif ( !current_user_can( 'edit_post', $post_id ) ) {
            return $post_id;
    }
    foreach ( $custom_meta_fields as $field ) {
        $old = get_post_meta( $post_id, $field['id'], true );
        $new = $_POST[$field['id']];
        if ($new && $new != $old) {
            update_post_meta( $post_id, $field['id'], $new );
        } elseif ('' == $new && $old) {
            delete_post_meta( $post_id, $field['id'], $old );
        }
    } // end foreach
}
add_action('save_post', 'save_spk_project_info_meta_data');

I have a custom meta box. In it there are input fields, as well as a select box. What I want, is to have the get_tags array list to be outputted as the values in the select box.

I currently have the select box option values hard coded. I want this to be dynamic depending on the tags on the website.

Here is the code;

    // Add the Meta Box
function spk_project_info_meta_box() {
    add_meta_box(
        'spk_project_info_meta_box', // $id
        'Project Information', // $title 
        'show_spk_project_info_meta_box', // $callback
        'post', // $page
        'normal', // $context
        'default'); // $priority
}
add_action('add_meta_boxes', 'spk_project_info_meta_box');


// Field Array
$prefix = 'spk_project_info_';
$tags_array = get_tags();
$custom_meta_fields = array(
    array(
        'label'=> 'Client',
        'desc'  => 'A description for the field.',
        'id'    => $prefix.'client',
        'type'  => 'text'
    ),
    array(
        'label'=> 'Our Role',
        'desc'  => 'A description for the field.',
        'id'    => $prefix.'role',
        'type'  => 'text'
    ),
    array(
        'label'=> 'Year',
        'desc'  => 'A description for the field.',
        'id'    => $prefix.'year',
        'type'  => 'text'
    ),

    array(
        'label'=> 'Select Box',
        'desc'  => 'A description for the field.',
        'id'    => $prefix.'select',
        'type'  => 'select',
        'options' => array (  
        'one' => array (
            'label' => 'Option One',
            'value' => 'one'
        ),
        'two' => array (
            'label' => 'Option Two',
            'value' => 'two'
        ),
        'three' => array (
            'label' => 'Option Three',
            'value' => 'three'
        )
      )
    )
);

// The Callback
function show_spk_project_info_meta_box() {
global $custom_meta_fields, $post;
echo '<input type="hidden" name="spk_project_info_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
    echo '<table class="form-table">';
    foreach ($custom_meta_fields as $field) {
        $meta = get_post_meta($post->ID, $field['id'], true);
        echo '<tr>
                <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
                <td>';
                switch($field['type']) {
                    case 'text':
                        echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
                            <br /><span class="description">'.$field['desc'].'</span>';
                    break;

                    case 'select':
                        echo '<select name="'.$field['id'].'" id="'.$field['id'].'">';
                        foreach ($field['options'] as $option) {
                            echo '<option', $meta == $option['value'] ? ' selected="selected"' : '', ' value="'.$option['value'].'">'.$option['label'].'</option>';
                        }
                        echo '</select><br /><span class="description">'.$field['desc'].'</span>';
                    break;
                }
        echo '</td></tr>';
    } // end foreach
    echo '</table>'; // end table
}

function save_spk_project_info_meta_data($post_id) {
    global $custom_meta_fields;
    if ( !wp_verify_nonce($_POST['spk_project_info_meta_box_nonce'], basename(__FILE__) ) ) 
        return $post_id;
    if ( defined('DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return $post_id;
    if ('page' == $_POST['post_type']) {
        if ( !current_user_can( 'edit_page', $post_id ) )
            return $post_id;
        } elseif ( !current_user_can( 'edit_post', $post_id ) ) {
            return $post_id;
    }
    foreach ( $custom_meta_fields as $field ) {
        $old = get_post_meta( $post_id, $field['id'], true );
        $new = $_POST[$field['id']];
        if ($new && $new != $old) {
            update_post_meta( $post_id, $field['id'], $new );
        } elseif ('' == $new && $old) {
            delete_post_meta( $post_id, $field['id'], $old );
        }
    } // end foreach
}
add_action('save_post', 'save_spk_project_info_meta_data');
Share Improve this question asked Nov 7, 2018 at 12:51 LeroyLeroy 134 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can do it like so — replace the $tags_array = get_tags(); with this:

$tags_opt = array();
foreach ( get_tags() as $term ) {
    $tags_opt[] = array(
        'label' => $term->name,
        'value' => $term->term_id,
    );
}

And then, for the "Select Box" field, set the options to $tags_opt like this:

array(
    'label' => 'Select Box',
    ...
    'options' => $tags_opt,
)

If you want to save the term/tag slug instead of its ID (term_id), change the 'value' => $term->term_id to 'value' => $term->slug.

And if you want to include tags without any posts, use get_tags( array( 'hide_empty' => 0 ) ).

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749199733a317199.html

最新回复(0)