WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] WordPress do not create more custom fields (7 posts)

  1. Luismin
    Member
    Posted 10 months ago #

    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!

  2. bcworkz
    Member
    Posted 10 months ago #

    Hello again!

    You have a typo in the save_custom_meta_producto() function. The foreach source is specified as $custom_meta_fields_producto but it must be $custom_meta_fields_productos (with an 's' on the end). As it is, nothing is being saved, perhaps what appeared to be saved is residual from a previous version?

    As long as the IDs are all unique, all the values should be saved correctly once the function is working properly.

  3. Luismin
    Member
    Posted 10 months ago #

    Hi bcworkz!

    Thanks for the correction, but I still with the problem. Finally, I will make it with a DEFINE.

    But here, I need it. As you can see, the method is the same, but It does not save the input text area.

    // Add the Meta Box
    function add_custom_meta_box_descriptiontexts() {
        add_meta_box(
            'custom_meta_box_descriptiontexts', // $id
            backend_descriptiontext_metabox_title, // $title
            'show_custom_meta_box_descriptiontexts', // $callback
            'descriptiontexts', // $page
            'normal', // $context
            'high'); // $priority
    }
    add_action('add_meta_boxes', 'add_custom_meta_box_descriptiontexts'); 
    
    	// The Callback
    function show_custom_meta_box_descriptiontexts() {
    global $post;  
    
    	$prefix = 'custom_';
        $custom_meta_fields = array(
    
    		array (
    			'label' => backend_descriptiontext_metabox_explanation,
    			'desc'  => backend_descriptiontext_metabox_dropdown,
    			'id'    => $prefix.'select',
    			'type'  => 'select',
    			'options' => array (
    					'one' => array (
    					'label' => backend_descriptiontext_metabox_categories,
    					'value' => 'Categories'
    					),
    					'two' => array (
    					'label' => backend_descriptiontext_metabox_step1,
    					'value' => 'Step 1'
    					),
    					'three' => array (
    					'label' => backend_descriptiontext_metabox_step2,
    					'value' => 'Step 2'
    					),
    					'four' => array (
    					'label' => backend_descriptiontext_metabox_step3,
    					'value' => 'Step 3'
    					),
    					'five' => array (
    					'label' => backend_descriptiontext_metabox_step4,
    					'value' => 'Step 4'
    					),
    					'six' => array (
    					'label' => backend_descriptiontext_metabox_responsible_purchasing,
    					'value' => 'Responsible Purchasing'
    					),
    					'seven' => array (
    					'label' => backend_descriptiontext_metabox_search,
    					'value' => 'Search'
    					)
    		)
    		),
    		array(
            'label'=> 'Enter the second title for the page',
            'desc'  => 'Type title',
            'id'    => $prefix.'text',
            'type'  => 'text'
        )
        );
    
    // 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']) {
    // case items will go here  
    
    // select
    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;
    
    // text
    case 'text':
    echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
    <br /><span class="description">'.$field['desc'].'</span>';
    break;
    
    } //end switch
    echo '</td></tr>';
        } // end foreach
        echo '</table>'; // end table
    }
    
    // Save the Data
    function save_custom_meta_descriptiontexts($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_descriptiontexts');

    The select saves the information properly, the input text does not. But, If I change the id for another that I create previously (for example 'post_id' that It's other input from a different custom post), It works. It's like WordPress does not accept new id's.

  4. Luismin
    Member
    Posted 10 months ago #

    I add this function to see if this was the problem but not....

    add_filter( 'postmeta_form_limit' , 'customfield_limit_increase' );
    function customfield_limit_increase( $limit ) {
        $limit = 1000;
        return $limit;
    }
  5. bcworkz
    Member
    Posted 10 months ago #

    No obvious problem is jumping out at me. As you say, new meta fields are not being created, but it is not WP preventing this AFAIK. I think there is a subtle flaw in you script such that update_post_meta() is not called if there is not an existing field. I can't identify where this flaw might be, I can only suggest you do a thorough trace through your code checking every variable and returned value is what you expect it to be and every part of any conditional only executes when it is supposed to.

    Somewhere in there, you will find things are not as they should be and hopefully that will indicate what needs to change to make things right. Sorry I can't give you a better answer, good luck with the debugging.

  6. Luismin
    Member
    Posted 10 months ago #

    Thank you.

    I'll see what I can do.

  7. Luismin
    Member
    Posted 10 months ago #

    Well, I "solved" the problem with a plugin. It's not the best way but it works. When I have more time I'll find out what was the problem.

    This design never ends jejejejejejej

Reply

You must log in to post.

About this Topic