添加带有上传代码的元数据箱

时间:2017-05-24 13:35:17

标签: wordpress meta-boxes uploader

不久前,我发现了自定义元框的代码。它运作良好,但我想添加一个图像上传器。由于我不记得我在哪里找到这段代码 - 因此联系作者 - 是否有人可以帮助我解决这个问题?

    $meta_boxes = array();


// Description
$meta_boxes[] = array(
'id' => 'descriptions',
'title' => 'Description',
'pages' => array('fleet', 'link'), // multiple post types
'context' => 'normal',
'priority' => 'high',
'fields' => array(
    array(
        'name' => 'Title',
        'desc' => 'Insert a title here',
        'id' => 'desc-title',
        'type' => 'text',
        'std' => 'Some problem'
    ),
 )
);

foreach ($meta_boxes as $meta_box) {
$my_box = new My_meta_box($meta_box);
}

class My_meta_box {

protected $_meta_box;

// create meta box based on given data
function __construct($meta_box) {
    $this->_meta_box = $meta_box;
    add_action('admin_menu', array(&$this, 'add'));

    add_action('save_post', array(&$this, 'save'));
}

/// Add meta box for multiple post types
function add() {
    foreach ($this->_meta_box['pages'] as $page) {
        add_meta_box($this->_meta_box['id'], $this->_meta_box['title'], 
array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']);
    }
}

// Callback function to show fields in meta box
function show() {
    global $post;

    // Use nonce for verification
    echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';

    echo '<div>';

    foreach ($this->_meta_box['fields'] as $field) {
        // get current post meta data
        $meta = get_post_meta($post->ID, $field['id'], true);

        echo '<label for="', $field['id'], '">', $field['name'], '</label><br>';
        switch ($field['type']) {
            case 'text':
                echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
                    '<br />', $field['desc'], '<br />';
                break;
            case 'textarea-side':
                echo '<textarea name="', $field['id'], '" id="', $field['id'], '" value="'. $field['std']  .'" cols="60" rows="4" style="width:100%; height:25px; border:1px solid black;box-sizing:border-box;">'. get_post_meta($post->ID, $field['id'], true) .'</textarea>',
                    '<br />', $field['desc'], '<br />';
                break;
            case 'textarea-normal':
                echo '<textarea name="', $field['id'], '" id="', $field['id'], '" value="'. $field['std']  .'" cols="60" rows="4" style="width:100%; height:300px; border:1px solid black;box-sizing:border-box;">'. get_post_meta($post->ID, $field['id'], true) .'</textarea>',
                    '<br />', $field['desc'], '<br />';
                break;
            case 'textarea-half':
                echo '<textarea name="', $field['id'], '" id="', $field['id'], '" value="'. $field['std']  .'" cols="60" rows="4" style="width:50%; height:250px; border:1px solid black;box-sizing:border-box;">'. get_post_meta($post->ID, $field['id'], true) .'</textarea>';
                break;
            case 'select':
                echo '<select name="', $field['id'], '" id="', $field['id'], '">';
                foreach ($field['options'] as $option) {
                    echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
                }
                echo '</select>';
                break;
            case 'radio':
                foreach ($field['options'] as $option) {
                    echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
                }
                break;
            case 'checkbox':
                echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' /><br />';
                break;
        }

    }

    echo '</div>';
}

// Save data from meta box
function save($post_id) {
    // verify nonce
    if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
        return $post_id;
    }

    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }

    // check permissions
    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 ($this->_meta_box['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);
        }
    }
}
}

非常感谢。

0 个答案:

没有答案