plugin development - Getting data from dynamically allocated metaboxes within a custom post

admin2025-06-02  0

I need to dynamically allocated metaboxes for a data entry type feature. Each Metabox represents a set of data associated with member such as name, address, city etc. Not sure what I am doing incorrectly. The metaboxes do display correctly, but when attempting to read the data, it only returns the data for the last metabox allocated.

As a test, I allocated 2 metaboxes, with unique names and tried various methods of naming an individual field as input. This same field is within every metabox, the only thing different is which metabox. From a input point of view, each of these metaboxes will contain different live data, but of the same type. This code below, is back to simple test code. Still no luck. Could really use some help.

function add_manual_form() 
 {
  $args = array('Index'=>'1');
  add_meta_box( 'List_Group1',
           __( 'List Records' ),
               'TEST_card',
               'xxxlist',
               'normal',
               'high',
               $args
             );

  $args = array('Index'=>'2');              
  add_meta_box( 'List_Group2',
            __( 'List Records' ),
               'TEST_card',
               'xxxlist',
               'normal',
               'high',
               $args
            );
}
add_action( 'add_meta_boxes', 'add_manual_form' );
add_action( 'save_post', 'save_TESTCARD');

function TEST_card($post_ID, $Arg)
{
$Index = $Arg['args']['Index']; 
?>
 <select name="field1" id="field1" class="postbox">
     <option value="">Select something...</option>
     <option value="something">Something</option>
     <option value="else">Else</option>
 </select>
<input type="hidden" id="index" name="index" value=<?php echo $Index; ?>>

 <?php
 }


function save_TESTCARD($post_ID) 
{
if (array_key_exists('field1', $_POST)) 
   {
   $InputValue = $_POST['field1'];
   $Index = $_POST['index'];
   DebugLog($InputValue);
   DebugLog((string)$Index);
   }
}

The debug output log shows the following:

06-03-2019, 06:20:33: else

06-03-2019, 06:20:33: 2

The values for the second metabox. The values for the first metabox never come in.

Could use any insight anyone has.

I need to dynamically allocated metaboxes for a data entry type feature. Each Metabox represents a set of data associated with member such as name, address, city etc. Not sure what I am doing incorrectly. The metaboxes do display correctly, but when attempting to read the data, it only returns the data for the last metabox allocated.

As a test, I allocated 2 metaboxes, with unique names and tried various methods of naming an individual field as input. This same field is within every metabox, the only thing different is which metabox. From a input point of view, each of these metaboxes will contain different live data, but of the same type. This code below, is back to simple test code. Still no luck. Could really use some help.

function add_manual_form() 
 {
  $args = array('Index'=>'1');
  add_meta_box( 'List_Group1',
           __( 'List Records' ),
               'TEST_card',
               'xxxlist',
               'normal',
               'high',
               $args
             );

  $args = array('Index'=>'2');              
  add_meta_box( 'List_Group2',
            __( 'List Records' ),
               'TEST_card',
               'xxxlist',
               'normal',
               'high',
               $args
            );
}
add_action( 'add_meta_boxes', 'add_manual_form' );
add_action( 'save_post', 'save_TESTCARD');

function TEST_card($post_ID, $Arg)
{
$Index = $Arg['args']['Index']; 
?>
 <select name="field1" id="field1" class="postbox">
     <option value="">Select something...</option>
     <option value="something">Something</option>
     <option value="else">Else</option>
 </select>
<input type="hidden" id="index" name="index" value=<?php echo $Index; ?>>

 <?php
 }


function save_TESTCARD($post_ID) 
{
if (array_key_exists('field1', $_POST)) 
   {
   $InputValue = $_POST['field1'];
   $Index = $_POST['index'];
   DebugLog($InputValue);
   DebugLog((string)$Index);
   }
}

The debug output log shows the following:

06-03-2019, 06:20:33: else

06-03-2019, 06:20:33: 2

The values for the second metabox. The values for the first metabox never come in.

Could use any insight anyone has.

Share Improve this question edited Mar 6, 2019 at 7:25 Debbie Kurth asked Mar 6, 2019 at 6:09 Debbie KurthDebbie Kurth 4323 silver badges14 bronze badges 2
  • The index <input> should be placed outside the select. – Sally CJ Commented Mar 6, 2019 at 7:13
  • that is true. I cut and pasted the code and in the actual test code, it is outside the select. The results are the same. Only the second of the metabox's data is collected. – Debbie Kurth Commented Mar 6, 2019 at 7:24
Add a comment  | 

1 Answer 1

Reset to default 0

Found a solution, although you have to change your premise a bit.

  1. Really can not add more than one metabox that uses a common draw and save callback.
  2. The names have to unique for the fields, just like in standard form processing.

So what I did it this 1 metabox, with my variation of repeatable fields.

function add_manual_form() 
 { 
  add_meta_box( 'List_Group1',
              __( 'List Records', 'myplugin_textdomain'  ),
                 'TEST_card',
                 'mmdlist',
                 'normal',
                 'high'
                  );            
  }
 add_action( 'add_meta_boxes', 'add_manual_form' );
 add_action( 'save_post', 'save_TESTCARD');

function TEST_card($post_ID, $Arg)
{
$RecordCnt = TotalRecordsInDatabase($post_ID);
$RecordCnt +=1;
for($i=1; $i<=$RecordCnt; $i ++) 
DrawRecordSet($i);

?>

<form id="ADD_LIST_RECORD" name="ADD_LIST_RECORD" method="post" action="">
 <input type="submit" name="ADD_LIST_RECORD" id="ADD_LIST_RECORD" value="ADD    RECORD" />
</form>


<?php
}


function DrawRecordSet($Index)
{
?>

 <select name="field1_<?php echo $Index; ?>">
 <option value="">Select something...</option>
 <option value="something">Something</option>
 <option value="else">Else</option>
 <option value="too">too</option>
 </select>

<select name="field2_<?php echo $Index; ?>">
  <option value="">Select something...</option>
  <option value="stuff">1</option>
  <option value="junk">Else</option>
  <option value="too">too</option>
</select>
<br /><br /><br />
<?php
// Add a responsive button to add more record sets
}


function save_TESTCARD($post_ID) 
  {
  for($i=1; $i<=3; $i ++)
    {
    if( array_key_exists( 'field1_'.$i, $_POST ) )
      {
      $InputValue = $_POST[ 'field1_'.$i ];
      DebugLog('Field1_'.$i.': '.$InputValue);
      }

   if( array_key_exists( 'field2_'.$i, $_POST ) )
     {   
     $InputValue = $_POST[ 'field2_'.$i];
     DebugLog('Field2_'.$i.': '.$InputValue);
     }
  }

}

To add a new record and save the present ones, I have added a old-style HTML submit button. This button passing nothing force the save & redraw routine. No data is passed.

This is just test code. It demonstrates the following:

  1. How to dynamically add same fields to a meta box
  2. How to ensure that these new fields have unique names
  3. How to retrieve the data from these fields

And there ya go for repeatable fields within a metabox, without all the other overhead. So unless someone can explain to me why this solution is problem..I am going for it. What I like is that it is small and tight code, using the database as a simulated variable. The only heavy use, is hitting the database, however since this is for the admin section of WordPress, it really does not matter.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748832966a314100.html

最新回复(0)