通过CheckBox分配用户角色功能

时间:2018-06-25 17:55:48

标签: php wordpress

我正在创建一个有关用户角色功能的迷你插件。我正在尝试通过CheckBox分配功能,因为我已将自定义功能保存在wp_option表中,但是我遇到了一个问题:
如何从wp_option表中获取此自定义功能,并通过CheckBox将其分配给角色。

这是我的PHP代码:

<?php
    /*
     Plugin Name: Custom Role
     Plugin URI: www.customroleandcapability.com
     Description: A custom role plugin
     Version: 1.0.0
     Author: jai
     Author URI:  www.customroleandcapability.com
    */    
     register_activation_hook(__FILE__,'activation');
      function activation(){
        add_role('teacher', __('Teacher'),
        array( ));
    }

    add_action('admin_menu', 'my_menu_pages');
    function my_menu_pages(){

        add_menu_page('Custom Role', 'Custom Role', 'manage_options', 'my_menu', 'my_menu_output' );   
    }

    function my_menu_output(  ) {

      global $wp_roles;

      if(isset($_POST['submit'])){
          unset($_POST['submit']);

          echo "<pre>"; 
          print_r($_POST); 
          echo "</pre>";

        $args = array('Administrator_read_posts','Administrator_edit_posts','Administrator_delete_posts','Editor_read_posts', 'Editor_edit_posts','Editor_delete_posts','Author_read_posts','Author_edit_posts','Author_delete_posts',                 'Subscriber_read_posts','Subscriber_edit_posts','Subscriber_delete_posts','Administrator_read_pages',                    'Administrator_edit_pages','Administrator_delete_pages','Editor_read_pages','Editor_edit_pages','Editor_delete_pages',   'Author_read_pages','Author_edit_pages','Author_delete_pages','Subscriber_read_pages','Subscriber_edit_pages',           'Subscriber_delete_pages','Administrator_read_media','Administrator_edit_media','Administrator_delete_media',            'Editor_read_media','Editor_edit_media','Editor_delete_media','Author_read_media','Author_edit_media',                   'Author_delete_media','Subscriber_read_media','Subscriber_edit_media','Subscriber_delete_media','Contributor_read_posts',
          'Contributor_edit_posts','Contributor_delete_posts','Contributor_read_pages','Contributor_edit_pages',                   'Contributor_delete_pages','Contributor_read_media','Contributor_edit_media','Contributor_delete_media','Teacher_read_posts','Teacher_edit_posts','Teacher_delete_posts','Teacher_read_pages','Teacher_edit_pages','Teacher_delete_pages','Teacher_read_media','Teacher_edit_media','Teacher_delete_media');



            foreach($args as $key => $val){

                if(!empty($_POST[$val]) && ($_POST[$val] == 1)){
                    update_site_option( $val, $_POST[$val] );
                }else{
                    update_site_option( $val, '0' );
                    }      
                }
                    echo get_site_option('read_posts'); 



                  $current_user = wp_get_current_user();
                  $user_id = $current_user->ID;
                  echo 'User ID: ' . $user_id . '<br />';
                  if ( 0 == $current_user->ID) {
                      echo "Not logged in";
                  } else {
                      echo "Logged in";
                  }

              }       
          ?>

以下是HTML代码:

  <div class="wrap" >
     <h1>Edit Role <a class="page-title-action" href="my_menu.php">Add New </a></h1>
       <form name="simple_form" method="post" action="admin.php?page=my_menu" enctype="multipart/form-data" >
        <table class="wp-list-table widefat fixed ">
         <thead>
           <tr>  
           <th class="column-cap">Capability</th>
           <?php foreach ( $wp_roles->roles as $key=>$value ): ?>
           <th><a href=" " ><?php echo $value['name'];  ?></a></th>
          <?php endforeach; ?>
         </tr>
        </thead>  
     <h1 id="menu_setting" class="handle">User Role & Capabilities</h1><br>
    <table class=" wp-list-table widefat fixed">
  <thead style="background-color:white;">
  <tr>

  <td>
    <a><i class="dashicons dashicons-admin-post"></i>  
      <span class="label">Posts</span></a>
       </td>
         <?php foreach ( $wp_roles->roles as $key=>$value ): ?>     
          <td> 
           <input id="CheckboxId" name="<?php echo $value['name'];  ?>_read_posts" type="checkbox" value="1"
           <?php if(get_site_option($key.'_'.'read_posts')=="1"){echo "checked='checked'";} ?> />read<br>

           <input id="CheckboxId" name="<?php echo $value['name'];  ?>_edit_posts" type="checkbox" value="1"
           <?php if(get_site_option($key.'_'.'edit_posts')=="1"){ echo "checked='checked'";} ?> />edit<br>

          <input id="CheckboxId" name="<?php echo $value['name'];  ?>_delete_posts" type="checkbox" value="1"
        <?php if(get_site_option($key.'_'.'delete_posts')=="1"){echo "checked='checked'";} ?>  />delete
      </td>
    <?php endforeach; ?>
     </tr>  

  <tr>
    <td>
      <a><i class="dashicons dashicons-admin-page"></i>
       <span class="label">Pages</span></a></td>
        <?php foreach ( $wp_roles->roles as $key=>$value ): ?>
         <td>
          <input id="CheckboxId" name="<?php echo $value['name'];  ?>_read_pages" type="checkbox" value="1"
           <?php if(get_site_option($key.'_'.'read_pages')=="1"){echo "checked";} ?> />read <br>

           <input id="CheckboxId" name="<?php echo $value['name'];  ?>_edit_pages" type="checkbox" value="1"
           <?php if(get_site_option($key.'_'.'edit_pages')=="1"){echo "checked";} ?> />edit<br>

          <input id="CheckboxId" name="<?php echo $value['name'];  ?>_delete_pages" type="checkbox" value="1"
        <?php if(get_site_option($key.'_'.'delete_pages')=="1"){echo "checked";} ?> >delete
       </td>  
     <?php endforeach; ?>
  </tr>

  <tr>
    <td>
      <a><i class="dashicons dashicons-admin-media"></i>
        <span class="label">Media</span></a></td>
          <?php foreach ( $wp_roles->roles as $key=>$value ): ?> 
            <td>
              <input id="CheckboxId" name="<?php echo $value['name'];  ?>_read_media" type="checkbox" value="1"
              <?php if(get_site_option($key.'_'.'read_media')=="1"){echo "checked";} ?> />read <br>

              <input id="CheckboxId" name="<?php echo $value['name'];  ?>_edit_media" type="checkbox" value="1"
              <?php if(get_site_option($key.'_'.'edit_media')=="1"){echo "checked";} ?> />edit<br>

              <input id="CheckboxId" name="<?php echo $value['name'];  ?>_delete_media" type="checkbox" value="1"
              <?php if(get_site_option($key.'_'.'delete_media')=="1"){echo "checked";} ?> >delete      
            </td>
          <?php endforeach; ?>  
       </tr>

      </table>
        <br>
          <input class="button button-primary" type="submit" id="submit" name="submit" value="Submit">
            </thead>
            </table>
          </form>      
       <script> 
     </script>
   <?php } 

0 个答案:

没有答案