Setting field order

Support General Setting field order

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #6749
    JohnnyJohnny
    Participant

    Is there a way to set the order of fields when appending fields to an existing metabox?

    Right now, they show at the bottom of the metabox and I'd like them to show at the top.

    <?php
    add_filter( 'rwmb_meta_boxes', 'MYPREFIX_register_meta_boxes', 20 );
    
    function MYPREFIX_register_meta_boxes( $meta_boxes ) {
    
    	global $meta_boxes;
    
    	foreach ( $meta_boxes as $k => $meta_box ) {
    		
    		if ( isset( $meta_box['id'] ) && 'metaboxname' == $meta_box['id'] ) {
    		
    			// Add custom fields to the "metaboxname" meta box
    			$meta_boxes[$k]['fields'][] = array(
    			'name'	=> __('Use Title', 'myid' ),
    			'id'	=> 'myid-use-title',
    			'clone'	=> false,
    			'type'	=> 'checkbox',
    			'std'	=> FALSE,
    			);
    	
    			$meta_boxes[$k]['fields'][] = array(
    			'name'	=> __('Use Header', 'myid' ),
    			'id'	=> 'myid-use-header',
    			'clone'	=> false,
    			'type'	=> 'checkbox',
    			'std'	=> TRUE,
    			);
    			
    		}	
    	
    	
    	}
    
    	// Return edited meta boxes
    	return $meta_boxes;
    }
    ?>
    #6763
    Anh TranAnh Tran
    Keymaster

    Hi Johnny,

    You can use PHP function array_splice to insert new fields at any position, like this:

    $field_1 = array(...);
    $position = 1;
    array_splice( $meta_boxes[$k]['fields'], $position, 1, array( $field_1 ) );
    // Do the same for $field_2, etc.

    For more info, please see this docs.

    #6781
    JohnnyJohnny
    Participant

    Thank you Anh! I will give this a try.

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Setting field order’ is closed to new replies.