如何在自定义的WordPress管理页面上保存由Foreach循环生成的带有多个复选框选项的设置字段?

时间:2017-08-29 作者:Amy Singleton
    function ld_reports_plus_register_submenu_page(){
    add_submenu_page(
        \'ld-reports-plus\',
        \'LearnDash Reports Plus Settings\',
        \'Report Settings\',
        \'manage_options\',
        \'ld-reports-plus-settings\',
        \'ld_reports_settings_submenu_page\'
    );
add_settings_section(
      \'bp_reg_fields\',                          //Name of the section
      \'BuddyPress Profile Fields\',                      //Title for the  section
      \'ld_report_bp_section_callback_function\',                         //Callback function for the section
      \'ld-reports-plus-settings\'                //Page where the section will be located
  );               
add_settings_field(
    \'ldrp_bp_options\',                                  //Name of the field
    \'BuddyPress Options\',                               //Title for the field
    \'ld_report_bp_fields_callback_function\',                            //Callback function for the field
    \'ld-reports-plus-settings\',                                 //Page where the field will be located
    \'bp_reg_fields\' );                             //Section where the field will be located
register_setting( \'ld-reports-plus-settings\', \'ldrp_bp_options\' );

}
add_action( \'admin_menu\', \'ld_reports_plus_register_submenu_page\' );

function ld_reports_settings_submenu_page(){  
  echo \'<div class="wrap">\';     
    echo "<h1>" . __( \'LearnDash Reports Plus\', \'ld-reports-plus\' ) . "</h1>";
    echo \'<form method="post" action="options.php">\';        
      settings_fields(\'ld-reports-plus-settings\');
      do_settings_sections(\'ld-reports-plus-settings\');      
      submit_button(\'Add Report Fields\');          
    echo \'</form>\';
  echo \'</div>\'; 
}

function ld_report_bp_section_callback_function(){
echo \'<p>Add BuddyPress Registration Field data to LearnDash Reports.</p>\';
function ld_report_bp_fields_callback_function(){
    $profile_groups = BP_XProfile_Group::get( array( \'fetch_fields\' => true ) );
    if ( !empty( $profile_groups ) ) {
    foreach ( $profile_groups as $profile_group ) {
           if ( !empty( $profile_group->fields ) ) {                
               foreach ( $profile_group->fields as $field ) {
                //echo $field->id . \' - \' . $field->name . \'<br/>\';
         $options = get_option( \'ldrp_bp_options\' );
                echo \'<input type="checkbox" id="\' . $field->id  . \'" name="\' . $field->name . \'" value="1"\' . checked( 1, $options[$field->id], false ) . \'/>\';
                echo \'<label for="\' . $field->name . \'">\' . $field->name . \'</label>\' . \'<br/>\';
               }
           }
       }
    }
}
}
1 个回复
SO网友:dperezac

您可以使用该功能xprofile_get_field_data( $field, $user_id, $multi_format)

 * @param mixed  $field        The ID of the field, or the $name of the field.
 * @param int    $user_id      The ID of the user.
 * @param string $multi_format How should array data be returned? \'comma\' if you want a
 *                             comma-separated string; \'array\' if you want an array.

结束

相关推荐

Ajax with plugins returns 0

我正在尝试在中创建按钮edit-comments 表,我想使用Ajax和一个插件来完成这项任务。这是插件/无电子邮件/无电子邮件下插件的代码。php:<?php /* Plugin Name: Approve Comment but don\'t send email Plugin URI: \"\" Description: Approve commnet or Trash Commnet but don\'t send the email.