Home › Forums › WordPress Plugins › Add column to All Users page
- This topic has 3 replies, 2 voices, and was last updated 4 years, 10 months ago by vinny.
-
AuthorPosts
-
-
November 8, 2019 at 1:43 am #18834Jamie FurlongParticipant
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
-
November 8, 2019 at 1:31 pm #18874vinnyKeymaster
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);
-
November 8, 2019 at 8:48 pm #18899Jamie FurlongParticipant
Thanks for getting back to me, Vinny. Could I trouble you to place that all in one block of code?
-
November 10, 2019 at 7:21 pm #19010vinnyKeymaster
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 ); } }
-
-
AuthorPosts
- You must be logged in to reply to this topic.