Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

Luismin on "WordPress do not create more custom fields"

$
0
0

Hi guys!

My problem is that I can not create more custom fields inside my custom meta boxes.

I have this:

function add_custom_meta_box_productos() {
    add_meta_box(
        'custom_meta_box_productos', // $id
        backend_product_metabox_title, // $title
        'show_custom_meta_box_productos', // $callback
        'products', // $page
        'normal', // $context
        'high'); // $priority
}
add_action('add_meta_boxes', 'add_custom_meta_box_productos');

    // Field Array
    $prefix_productos = 'custom_';
    $custom_meta_fields_productos = array(  

array(
'label'=> 'Label 1',
'desc'  => 'Title',
'id'    => 'label1',
'type'  => 'text'
),
array(
'label'=> 'Label 2',
'desc'  => 'Title',
'id'    => 'label2',
'type'  => 'text'
),
array(
'label'=> 'Label 3',
'desc'  => 'Title',
'id'    => 'label3',
'type'  => 'text'
),
array(
'label'=> 'Label 4',
'desc'  => 'Title',
'id'    => 'label4',
'type'  => 'text'
),
);

// The Callback
function show_custom_meta_box_productos() {
global $custom_meta_fields_productos, $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_productos as $field_producto) {
        // get value of this field if it exists for this post
        $meta_producto = get_post_meta($post->ID, $field_producto['id'], true);
        // begin a table row with
        echo '<tr>
                <th><label for="'.$field_producto['id'].'">'.$field_producto['label'].'</label></th>
                <td>';
switch($field_producto['type']) {

// text
					case 'text':
						echo '<input type="text" name="'.$field_producto['id'].'" id="'.$field_producto['id'].'" value="'.$meta_producto.'" size="30" />
							<br /><span class="description">'.$field_producto['desc'].'</span>';
					break;
                } //end switch
        echo '</td></tr>';
    } // end foreach
    echo '</table>'; // end table
}

// Save the Data
function save_custom_meta_producto($post_id) {
    global $custom_meta_fields_productos;  

    // 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_producto as $field_producto) {
        $old = get_post_meta($post_id, $field_producto['id'], true);
        $new = $_POST[$field_producto['id']];
        if ($new && $new != $old) {
            update_post_meta($post_id, $field_producto['id'], $new);
        } elseif ('' == $new && $old) {
            delete_post_meta($post_id, $field_producto['id'], $old);
        }
    } // end foreach
}
add_action('save_post', 'save_custom_meta_producto');

First, I create the metabox and create an array with 4 different input text. But when I introduce information and clic in publish, they do not save nothing. The problem is that WordPress does not create the id custom field for each input text. This is a piece of code but I have more fields in this metabox and save the information properly. But, If I create new fields, these fields do not save the information. Any idea?

Thanks!


Viewing all articles
Browse latest Browse all 8245

Trending Articles