• Resolved theshivanjali

    (@theshivanjali)


    I have created a meta box that has a field called Quantity and IMEI Number.
    I want to create a conditional logic that helps me adding the meta box on the basis of Quantity. as

    For Example:
    If I enter 10 in Quantity, then it should be able to create 10 new boxes to enter the IMEI Number.

    Please let me know if I can achieve this with the help of meta boxes or not!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Anh Tran

    (@rilwis)

    Hi,

    That can be done with our Meta Box Conditional Logic extension.

    Thread Starter theshivanjali

    (@theshivanjali)

    Hi Anh,

    I have already installed the Meta Box Conditional Logic extension and read it’s documentation. But I wasn’t really able to achieve what I wanted. Can you please help?

    Plugin Author Anh Tran

    (@rilwis)

    Hi @theshivanjali ,

    I think I misunderstood your question. I thought you already created meta boxes and use the Conditional Logic extension to toggle them.

    Looks like you want to create meta boxes dynamically. I think that’s still doable like this:

    – You create a quantity field for entering the number of meta boxes
    – In your rwmb_meta_boxes filter callback, you need to get that number and register meta boxes based on that number

    Here is a sample code:

    add_filter( 'rwmb_meta_boxes', function( $meta_boxes ) {
    	// Quantity field.
    	$meta_boxes[] = [
    		'title' => 'Quantity',
    		'fields' => [
    			[
    				'id' => 'quantity',
    				'type' => 'number',
    				'name' => 'Quantity',
    			]
    		],
    	];
    
    	// Dynamic create meta boxes.
    	if ( isset( $_GET['post'] ) ) { // If edit post only.
    		$quantity = get_post_meta( $_GET['post'], 'quantity', true );
    		for ( $i = 0; $i < $quantity; $i++ ) {
    			$meta_boxes[] = [
    				'title' => "IMEI #$i",
    				'fields' => [
    					[
    						'id' => "imei_$i",
    						'name' => 'IMEI',
    					]
    				],
    			];
    		}
    	}
    
    	return $meta_boxes;
    } );

    Just a note that because the meta boxes are dynamically created, you need to save post to see them.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How To Add Meta Box Dynamically’ is closed to new replies.