使用迭代器和 update_user_meta() 动态添加复选框到 profile.php 和 edit-user.php 在 WordPress 使用数组

Using Iterator and update_user_meta() to Dynamically Add Checkboxes to profile.php and edit-user.php in WordPress Using an Array

所以,我一直在为这件事焦头烂额,这比我愿意承认的时间还要长。我正在尝试在 profile.php 和 user-edit.php 中为由数组填充的插件创建一堆复选框。对于初学者来说,这有效:

//<!-- "HTML" -->
function profile_fields($user) {
    $user_id =  $user->ID;

    <input type='hidden' name="user_id" value="<?php echo $user_id ?>" />
    <input type="checkbox" id="some-setting" name='some-setting'
    <?php if (get_user_meta( $user->ID, 'somePref', true ) == '1'){ echo 'checked'; } ?> />
    <label for="some-setting">TEST 2</label>


//PHP
function update_field_value($user_id) {
    if ( isset($_POST['some-settings'], $user_id ) && $_POST['some-settings'] == 'on') { 
        update_user_meta( $user_id, 'somePref', '1'); 
    } else { 
        update_user_meta( $user_id, 'somePref', NULL); 
    }
}

但是,我所生成的 Undefined offset...Trying to access array offset on value of type null... 在使用时我无法追踪其来源:

class Some_Class
   {


//Array of user specialties to be displayed
var $userSpecialtiesList;

//Array of ids for input fields
var $userSpecialtiesIDs = array();

//Array of meta values for input fields
var $metaValues = array();

//Array of meta value names for use with accessing info
var $metaNames = array();

/**
* initialize
*
* I use this to populate the array and call add_action 
*/
function initialize(){
   $this->userSpecialtiesList = array(
          /* some information here */
   );
   add_action( 'show_user_profile', array($this, 'extra_profile_fields') );
   add_action( 'edit_user_profile', array($this, 'extra_profile_fields') );
   add_action( 'personal_options_update', array($this, 'update_field_value') ); 
   add_action( 'edit_user_profile_update', array($this, 'update_field_value') );
}

/**
* extra_profile_fields
* I use this to generate the input fields that the user sees
*/
function extra_profile_fields( $user ) {
   $user_id =  $user->ID;

   //I used this for testing
   //$meta_value = get_user_meta( $user->ID, 'somePref', true );
   
   ?> 
   <h3><?php _e("Some Title", "blank"); ?></h3>
   <table class="form-table">
       <!-- Some unrelated stuff here -->
       <tr>
           <th><h4><?php _e("Specialties"); ?></h4></th>
           <td>
           <fieldset>
           <!-- Some more unrelated stuff here -->
           <?php
              $this->generateCheckboxes($user_id);?>
           </fieldset>
       </td>
     </tr>
   </table>
   <?php
}

/**
* generateID
*
* Partially complete function to generate field ids and meta keys
*/
function generateID($phrase, $arrValue, $style){
   //Remove illiegal characters and leave only alphanumeric
   $arrValueUpdated = preg_replace('/[^a-zA-Z\d:]/', "", $arrValue);
   
   switch($style){
       //Use Dashes
       case $style == "-": 
       case $style == "dash":
           return strtolower($phrase) . "-" . strtolower($arrValueUpdated);
       
       //Use camelCase
       case $style == "camelCase":
           return strtolower($phrase) . ucfirst($arrValueUpdated);
       
       //Default is alltogether
       default:
           return strtolower($phrase) . strtolower($arrValueUpdated);  
   }
}

/**
* generateCheckboxes
* 
* I use this to generate checkboxes from the array instantiated above
*/
function generateCheckboxes($userID){
   
   foreach($this->userSpecialtiesList as $userSpecialty){
       //Create unique id for input fields and add it to array for later
       $inputID = $this->myGenerateID("specialty", $userSpecialty, "dash");
       array_push( $this->userSpecialtiesIDs, $inputID );
   
       
       
       //Create meta key and add it to array of meta keys for use with update_user_meta later
       $metaName = $this->myGenerateID("pref", $userSpecialty, "camelCase");
       array_push( $this->metaNames, $metaName );
       
       //Create variable for meta values and add it to array for later
       $meta_value = get_user_meta( $userID, $metaName, true );
       array_push( $this->metaValues, $meta_value );

?>
               <div class="my-checkbox-class">
                   <input type='hidden' name="user_id" value="<?php echo $userID ?>" />
                   <input type="checkbox" id="<?php echo $inputID; ?>" name='<?php echo $inputID; ?>'
                   <?php if ($meta_value == '1'){ echo 'checked'; } ?> />
                   <label for='<?php echo $inputID; ?>'><?php _e($userSpecialty); ?></label>
               </div>
   
<?php } 


function update_field_value($user_id) {
   $user_id = $_POST['user_id'];
   $uslIndex = 0;

   foreach($this->userSpecialtiesList as $userSpecialty){
       if ( isset($_POST[$this->userSpecialtiesIDs[$uslIndex]], $user_id ) && $_POST[$this->userSpecialtiesIDs[$uslIndex]] == 'on') { 
           update_user_meta( $user_id, $this->metaNames[$uslIndex], '1'); 
       } else { 
           update_user_meta( $user_id, $this->metaNames[$uslIndex], NULL); 
       }           
       $uslIndex++;
    }
 }
}
   function some_class_func() {
       global $some_class_func;

       // Instantiate only once.
       if( !isset($some_class_func) ) {
           $some_class_func = new Some_Class();
           $some_class_func->initialize();
       }
       
       return $some_class_func;
   }

   // Instantiate
   some_class_func();

在 post 中,为了清楚起见,我混淆了一些变量名并删除了一些不必要的位;我也意识到,当我经历它时,我有一些来自重写它的部分的工件,这可能是我的问题的一部分,但我选择留下它们。

如果我自己得到这个,那么我会 post 为每个人提供解决方案;不过无论如何,我提前非常感谢您提供的所有答案/帮助!

有时将问题分开会更容易解决问题

$postval = isset($this->userSpecialtiesIDs[$uslIndex]) ? $this->userSpecialtiesIDs[$uslIndex] : null;
$user_id = $user_id ?? null;

$condition1 = $user_id && $postval && isset($_POST[$postval]);
$condition2 = $postval && $_POST[$postval] === 'on';

if ($condition1 && $condition2) {
// do the thing
}

和...

if ($user_id && isset($this->metaNames[$uslIndex])) update_user_meta( $user_id, $this->metaNames[$uslIndex], NULL); 

好吧,看来我的问题可能是范围之一,所以我所做的基本上只是在别处创建一个数组并将其交给更新函数。所以,我几乎不理会 initialize()extra_profile_fields()generateID(),而是做了以下更改:

   //Generate the input tag ID and meta key / name 
   // and then create the HTML for checkboxes
   function generateCheckboxes($userID){
    
        foreach($this->userSpecialtiesList as $userSpecialty){
            $inputID = $this->generateID("specialty", $userSpecialty, "dash");
            $metaName = $this->generateID("pref", $userSpecialty, "camelCase"); ?>
                    <div class="prof-checkbox">
                        <input type='hidden' name="user_id" value="<?php echo $userID ?>" />
                        <input type="checkbox" id="<?php echo $inputID; ?>" name='<?php echo $inputID; ?>'
                        <?php if (get_user_meta( $userID, $metaName, true ) == '1'){ echo 'checked'; } ?> />
                        <label for='<?php echo $inputID; ?>'><?php _e($userSpecialty); ?></label>
                    </div>
    <?php } 
    }

   
   /**
    * generate_array()
    * Creates, populates, and returns a multi-dimensional array holding
    * the input tag's id, meta key, and text value.
    *
    * @return $userSpecialtiesArr Array    A multi-dimensional array holding the input tag's id ("id"), meta key "meta", and text "text"
    */
   function generate_array(){
        $userSpecialtiesArr = array();
        
        foreach($this->userSpecialtiesList as $userSpecialty){
            
            $userSpecialtyPKG = array(
                "text"=>$userSpecialty,
                "id"=>$this->HgenerateID("specialty", $userSpecialty, "dash"),
                "meta"=>$this->generateID("pref", $userSpecialty, "camelCase"),
            );
            
            array_push($userSpecialtiesArr, $userSpecialtyPKG);
        }
        return $userSpecialtiesArr;
    }


function update_field_value($user_id) {
        
        $user_id = $_POST['user_id'];
        //Generate array to hold the neccessary information
        $userSpecialtiesArr = $this->generate_array();
        
        //Use an iterator to dynamically add the necessary unique ID and meta key.
        foreach($userSpecialtiesArr as $userSpecialty) :
        
            if ( isset($_POST[$userSpecialty['id']], $user_id ) && $_POST[$userSpecialty['id']] == 'on') { 
                update_user_meta( $user_id, $userSpecialty['meta'], '1'); 
            } else { 
                update_user_meta( $user_id, $userSpecialty['meta'], NULL); 
            }
        
        endforeach;
    
    }

不确定这是否对任何人有帮助,但如果没有帮助,Kinglish 也有一个很好的答案,希望两者都能减少有人因为同样的事情而拔头发。