Shop Forums WordPress Plugins Add column to All Users page

Tagged: 

Viewing 3 reply threads
  • Author
    Posts
    • #18834
      Jamie Furlong
      Participant

      Hi Vinny,

      I’d like to add a column to the users page which displays whether they have selected immediate notifications or not. I came across this code and wondered what I’d have to change in order to display this:

      /*this will add column in user list table*/
      
      function add_column( $column ) {
          $column['text_checkbox'] = 'Immediate Notifications';
          return $column;
      }
      add_filter( 'manage_users_columns', 'add_column' );
      
      /*this will add column value in user list table*/
      function add_column_value( $val, $column_name, $user_id ) {
          switch($column_name) {
      
              case 'text_checkbox' :
                  return get_user_meta( $user_id, 'my_key_name', true ); ;
                  break;
      
                 default:
          }
      }
      add_filter( 'manage_users_custom_column', 'add_column_value', 10, 3 );

      I think the one line I need to change is

      return get_user_meta( $user_id, 'my_key_name', true ); ;

      where I replace ‘my_key_name’ with the meta value for immediate notifications. Also this is a straight copy from somewhere and I think there’s an extra semi-colon there.

      Any pointers? TIA

    • #18874
      vinny
      Keymaster

      Hi Jamie,

      The simplest way is to use

      get_user_meta( $user_id, 'BbpnnsDigest_force_notification', $single=true);

      However, that may break if I ever decide to change how the preference is stored. A better, but more complex option is to load the recipients DAO and load it from there. However, because load_lib() is protected, you’ll have to override it somewhere:

      
      class jamies_digest_override extends BbpnnsDigest {
          public function __construct(){ /* NOOP */ }
      
          public function load_lib( $name, $params = array(), $force_reload = false ){
              return parent::load_lib( $name, $params, $force_reload );
          }
      }
      

      and then replace your get_user_meta line with this:

      
      $obj = new jamies_digest_override();
      $rec_dao = $obj->load_lib( 'dal/recipients_dao' );
      return $rec_dao->get_user_force_notif_pref(null, $user_id);
      
    • #18899
      Jamie Furlong
      Participant

      Thanks for getting back to me, Vinny. Could I trouble you to place that all in one block of code?

    • #19010
      vinny
      Keymaster

      This is what it should look like. Note that I haven’t tested it, so please run it in your dev site first.

      /*this will add column in user list table*/
      
      function add_column( $column ) {
          $column['text_checkbox'] = 'Immediate Notifications';
          return $column;
      }
      add_filter( 'manage_users_columns', 'add_column' );
      
      /*this will add column value in user list table*/
      function add_column_value( $val, $column_name, $user_id ) {
          switch($column_name) {
              
              case 'text_checkbox' :
      
                  $obj = new jamies_digest_override();
                  $rec_dao = $obj->load_lib( 'dal/recipients_dao' );
                  return $rec_dao->get_user_force_notif_pref(null, $user_id);
                  
                  break;
                  
              default:
          }
      }
      add_filter( 'manage_users_custom_column', 'add_column_value', 10, 3 );
      
      class jamies_digest_override extends BbpnnsDigest {
          public function __construct(){ /* NOOP */ }
          
          public function load_lib( $name, $params = array(), $force_reload = false ){
              return parent::load_lib( $name, $params, $force_reload );
          }
      }
Viewing 3 reply threads
  • You must be logged in to reply to this topic.