The Stack Overflow podcast is back! Listen to an interview with our new CEO.
    Bumped by Community user
    Bumped by Community user
3 Add code directly to question
source | link

Ive uploadedHere is my code so far at pastebin: http://pastebin.com/6PiadUgW

//Add custom metadata
function add_custom_metadata_boxes() {
        add_meta_box(
        'custom_meta_box', // $id
        'Values', // $title
        'show_custom_boxes', // $callback
        'foo1', //Custom post types
        'normal', // $context
        'high' // $priority
    );
}

$prefix = 'custom_';
$custom_meta_fields = array(
    array(
            'name' => 'number',
            'label' => 'Number',
            'id' => $prefix . 'number',
            'type' => 'number',
            'std' => ''
    )
);

// The Callback for the first one
function show_custom_boxes() {
global $custom_meta_fields, $post;
// Use nonce for verification
echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';

    // Begin the field table and loop
    echo '<table class="form-table">';
    foreach ($custom_meta_fields as $field) {
        // get value of this field if it exists for this post
        $meta = get_post_meta($post->ID, $field['id'], true);
        // begin a table row with
        echo '<tr>
               <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
               <td>';
                switch($field['type']) {
                    // checkbox
                                        case 'number':
                                echo '<input type="number" min="0" step="1" pattern="\d+" placeholder="Fill in a number" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
                        break;
                } //end switch
        echo '</td></tr>';
    } // end foreach
    echo '</table>'; // end table
}

add_action('add_meta_boxes', 'add_custom_metadata_boxes');

function save_custom_meta($post_id) {
    global $custom_meta_fields;

    // verify nonce
    if (!wp_verify_nonce($_POST['custom_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;
    }

    // loop through fields and save the data
    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_custom_meta');

AnyoneAny suggestions?

Ive uploaded my code so far at pastebin: http://pastebin.com/6PiadUgW

Anyone?

Here is my code:

//Add custom metadata
function add_custom_metadata_boxes() {
        add_meta_box(
        'custom_meta_box', // $id
        'Values', // $title
        'show_custom_boxes', // $callback
        'foo1', //Custom post types
        'normal', // $context
        'high' // $priority
    );
}

$prefix = 'custom_';
$custom_meta_fields = array(
    array(
            'name' => 'number',
            'label' => 'Number',
            'id' => $prefix . 'number',
            'type' => 'number',
            'std' => ''
    )
);

// The Callback for the first one
function show_custom_boxes() {
global $custom_meta_fields, $post;
// Use nonce for verification
echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';

    // Begin the field table and loop
    echo '<table class="form-table">';
    foreach ($custom_meta_fields as $field) {
        // get value of this field if it exists for this post
        $meta = get_post_meta($post->ID, $field['id'], true);
        // begin a table row with
        echo '<tr>
               <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
               <td>';
                switch($field['type']) {
                    // checkbox
                                        case 'number':
                                echo '<input type="number" min="0" step="1" pattern="\d+" placeholder="Fill in a number" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
                        break;
                } //end switch
        echo '</td></tr>';
    } // end foreach
    echo '</table>'; // end table
}

add_action('add_meta_boxes', 'add_custom_metadata_boxes');

function save_custom_meta($post_id) {
    global $custom_meta_fields;

    // verify nonce
    if (!wp_verify_nonce($_POST['custom_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;
    }

    // loop through fields and save the data
    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_custom_meta');

Any suggestions?

2 added 13 characters in body
source | link

I want to add one custom meta box to a specific custom meta type "chifres" and two custom meta boxes to a specific custom meta type "presse". This is how far I've gotten:

Ive uploaded my code so far at pastebin: http://pastebin.com/6PiadUgW

The "chifres" box works perfectly, I just can't figure out how I can add moreother custom meta boxes to another custom post type.

Anyone?

I want to add one custom meta box to a specific custom meta type "chifres" and two custom meta boxes to a specific custom meta type "presse". This is how far I've gotten:

Ive uploaded my code so far at pastebin: http://pastebin.com/6PiadUgW

The "chifres" box works perfectly, I just can't figure out how I can add more boxes to another custom post type.

Anyone?

I want to add one custom meta box to a specific custom meta type "chifres" and two custom meta boxes to a specific custom meta type "presse". This is how far I've gotten:

Ive uploaded my code so far at pastebin: http://pastebin.com/6PiadUgW

The "chifres" box works perfectly, I just can't figure out how I can add other custom meta boxes to another custom post type.

Anyone?

1
source | link

Add multiple custom meta boxes to different custom post types

I want to add one custom meta box to a specific custom meta type "chifres" and two custom meta boxes to a specific custom meta type "presse". This is how far I've gotten:

Ive uploaded my code so far at pastebin: http://pastebin.com/6PiadUgW

The "chifres" box works perfectly, I just can't figure out how I can add more boxes to another custom post type.

Anyone?