Forum Replies Created

Viewing 14 replies - 1 through 14 (of 14 total)
  • Thread Starter webbuildermn

    (@webbuildermn)

    Hi thanks for your continued help. Not there yet. I tried a couple things. I tried (learning) debugging with some results. I noticed in the URL when I clicked on LastName, it entered in parameters, so I did see that orderby had a value and order did too. Reassuring. I think comes from the ‘manage_users_sortable_columns’. It was confirmatory:

    http://journey.local/wp-admin/users.php?orderby=LastName&order=asc

    I tried using methods you suggested as well as pre_get_posts and pre_get_user. I got a weird error there with

    Can’t use method return value in write context

    and squiggly lines even on VS Code when I try the pre_get_posts that others have suggested by code like this:
    $query->set( 'orderby', 'meta_value' );

    Last I tried the “users_list_table_query_args” you pointed to. Same results- page loads, but doesn’t sort. I got an interesting error when I forgot to change the name of the variable from pasted code. It showed up in the debug mode and said Notice: Undefined variable: vars in /app/public/wp-content/plugins/j-w-m/j-w-m.php on line 108 Warning:

      array_key_exists() expects parameter 2 to be array, null given in

    /app/public/wp-content/plugins/j-w-m/j-w-m.php on line 108

    because here

    add_filter( 'users_list_table_query_args', 'filter_users_list_table_query_args', 10, 1 );
    function filter_users_list_table_query_args( $args ) { 
      // make filter magic happen here... 
      if(array_key_exists('orderby', $vars)) {  


    of course I forgot to change $args to $vars. When I did, that error went away, page loaded like normal but that does tell us
    1. the function is being run
    2. When run, $vars is an array, as one would expect

    Just why doesn’t it modify and work as expected.

    Thread Starter webbuildermn

    (@webbuildermn)

    Thanks again Joy. The way I was trying to manipulate the query was by altering the query array before sending it off, which is what I read about and makes sense. In my code below, the query array is called $vars. I am trusting that through WP hooks and the guidance I’ve read, it is passing what I need and I am modifying and passing back what it needs.

    I was following the article referenced above because that’s the only one that mentions manipulating admin tables specifically. He uses $array[x] = “string” notation as opposed to “=>” notation other articles use, but again he’s the only one that describes how to sort or alter admin tables. Does his way still work?

    I like using formal reference documentation when possible but I can’t make sense of it in this case. I already added the chosen columns to the manage_sortable_columns array, although that doesn’t look like a WP_Posts_list_Table method. Remember it is not a post or a custom post or page but a user’s admin table.

    Anyway I added the two columns in. I don’t know if they need to be in order of priority as with adding columns to display. Anyway they are in there, now we just have to change the query and make them sort based on it.

    I’m not sure where my mistake is but I had an idea or thought. In my SQL query, I only return a single cell of the database table per WP iteration as it implements its hook, as opposed to an entire filtered column of last names from the database table. What I did works perfectly for displaying last names correctly where I want them, but maybe that’s why it doesn’t sort, as you can’t sort an array of 1×1. It’s a timing thing but I’m not sure if this is the reason, it’s just an idea. When I asked SQL to pull my entire filtered column, then for each person’s last name it said “array”, so I’d need to learn how to index by user_name, and so I’d have to probably pull a 2 column multi row array to link the two. Maybe it’s this??

    Also I’m used to other languages so I have no idea how to debug PHP or if you can look inside the value of variables in process as the code is running. I’m not sure as it’s not a compiled language… But I’m starting my course soon.

    Any tips and help you can provide is appreciated. I view everything as a learning experience.

    
    // SORT BLOCK
    // add columns to sortable columns array
    function sort_mishas_user_columns ($columns){
      $columns["FirstName"] = "FirstName";
      $columns["LastName"] = "LastName";
    	return $columns;
    }
    add_filter('manage_users_sortable_columns', 'sort_mishas_user_columns'); // the hook
    // modify/merge sort criteria array with new criteria --- I think there's an error here, something not working. It sorts by the username still
    function sort_az_users($vars) { 
      if(array_key_exists('orderby', $vars)) {  // Assuming this is correct and auto-created by the WP hook below
        if('LastName' == $vars['orderby']) {    // 'LastName' is the column header name that is clickable. Assuming it gets set to that on "click"
          $vars['orderby'] = 'meta_value';
          $vars['meta_key'] = 'last_name';      // 'last_name' is the name in the database under 'meta_key' 
        } 
      } 
      return $vars; 
    } 
    add_filter('request', 'sort_az_users'); // the hook
    // END SORT BLOCK
    Thread Starter webbuildermn

    (@webbuildermn)

    Thanks joy. Yeah I had that add-in for the site, but to get sort, we have to pay $49. It’s not just me being cheap. It’s a site I built for free for a friend/professor and also it’s good training for my upcoming bootcamp and we only need it for this one use. I was looking into the source code of that add-in and it’s intense (compared to what I’m used to), whereas if I got mine to work, it would be lean.

    Anyway, that’s a good reference you linked me but I don’t see any method that looks like it sorts the data. Is it something with views? How do I take the next step?

    I’ve been following this article btw, but there’s also one other article that talks about sorting columns that I’ve found, though that one doesn’t’ cover admin tables.

    Thread Starter webbuildermn

    (@webbuildermn)

    Wow, I have come a long way.

    I’m getting snagged at the end. I have all but completed my plug-in- entire code below.

    1. I get the first names and last names to show, pulled straight from the database, in their separate columns.

    2. These columns are sortable

    3. The problem is they sort based on the user name, not on their own column

    4. I read two articles about this, with different code to solve it. They each use different hooks. I tried them both, perhaps imperfectly.
    a) add_filter(‘request’, ‘bcw_sort_metabox’);
    b) add_action( ‘pre_get_posts’, ‘smashing_posts_orderby’ );

    I got very far, almost to the end but I can only sort by the first column, not by the column clicked.

    An idea is that I know this relates to querying the database differently and clicking the triangle is basically toggling ASC and DESC in SQL. However, my query selects only one cell each time, so I wonder if that plays a role.

    Here’s the code

    <?php
    /**
     * Plugin Name: j-w-m
     * Plugin URI: http://www.journeywithmisha.com/j-w-m
     * Description: The very first plugin that I have ever created.
     * Version: 1.0
     * Author: Gerald Ryan
     * Author URI: http://www.webbuildermn.com
     */
    
    // Experimental Sort Users //
    // 
    function add_custom_misha_columns($columns){
    	$columns["Nova"] = "Nova";
    	$columns["Brilliant.org"] = "Brilliant.org";
     	$columns["FirstName"] = "FirstName";
    	$columns["LastName"] = "LastName";
    	return $columns;
    }
    add_filter('manage_users_columns', 'add_custom_misha_columns');
    
    // // Populate the column
    function add_content_to_mishas_column($value, $column_name, $user_id){
      global $wpdb;
      $fname = $wpdb->get_var("SELECT meta_value FROM $wpdb->usermeta WHERE user_ID = $user_id AND meta_key = 'first_name' "  );
      $lname = $wpdb->get_var("SELECT meta_value FROM $wpdb->usermeta WHERE user_ID = $user_id AND meta_key = 'last_name' "  );
    	if ($column_name == "FirstName"){
        return $fname;
        // return "Romeo";
    	}
    	if ($column_name == "LastName"){
        return $lname;
        // return "Montague";
      }
    
      if ($column_name == "Brilliant.org"){
        return $user_id;
      }
      if ($column_name == "Nova"){
        return $value;
      }
    }
    
    add_action('manage_users_custom_column', 'add_content_to_mishas_column', 10, 3);
    
    function sort_mishas_user_columns ($columns){
      $columns["FirstName"] = "FirstName";
      $columns["LastName"] = "LastName";
    	return $columns;
    }
    
    // Add sortable column to Column Array
    add_filter('manage_users_sortable_columns', 'sort_mishas_user_columns');
    
    // // make our columns sortable based on right factor
    // add_action( 'pre_get_posts', 'user_az_orderby' );
    // function user_az_orderby( $query ) {
    //   if ( 'FirstName' === $query->get( 'orderby') ) {
    //     $query->set( 'orderby', 'meta_value' );
    //     $query->set( 'meta_key', 'FirstName' );
    //   }
    // }
    
    function user_az_orderby($vars) { 
      if(array_key_exists('orderby', $vars)) { 
        if('LastName' == $vars['orderby']) { 
          $vars['orderby'] = 'LastName'; 
          // $vars['meta_key'] = 'LastName'; 
        } 
      } 
      return $vars; 
    } 
    add_filter('request', 'user_az_orderby');
    Thread Starter webbuildermn

    (@webbuildermn)

    Great Progress: The code below inputs my user_id. As you all know, the Action hook requires the number of parameters. I didn’t realize I was limiting it to 2. You can tell it it is greater than it is, but not less, or you’ll lose functionality. You also can’t pass more arguments to your callback function than it is expected to have according to the hook- in this case “manage_users_custom_column”. This is what I am intuiting. So when I change the add_action to 3 parameters, and add a parameter- “$user_id”, which is what it is, but I could call it whatever I want- $Waldo- then I have access to the User ID. The first parameter is a string and starts Null of course. You’d not be unwise to name it $Value, and then you can grab whatever value you want- for instance the returned value from a SQL query, which is what I have to do.

    So, I have the user ID and there are two candidate tables we can get the info from- Users and User_Meta. I already know it’s User_Meta from the discussion prior. Looking at the DB table User_meta, user_id is not a unique key, so that presents an extra challenge.

    There is a get_user_data($user_ID) which gets a user data object, which might work for populating the table but I am not sure it has sorting functionality as that might be linked to SQL order_by. I could play around with that to see if I can populate the table but I think I’ll pass.

    There it is, in all its glory- a wp_usermeta table with a non unique user_id and associated meta_key of first_name and one of last_name.

    So how to find that? I’ll do some studying and if I find anything I’ll share.

    Great progress.

    
    // // Populate the column
    function add_content_to_mishas_column($value, $column_name, $user_id){
      $value = $user_id;
    	if ($column_name == "FirstName"){
    		return "Romeo";
    	}
    	if ($column_name == "LastName"){
    		return "Montague";
      }
    
      if ($column_name == "Brilliant.org"){
        return $user_id;
      }
      if ($column_name == "Nova"){
        return $value;
      }
    }
    
    add_action('manage_users_custom_column', 'add_content_to_mishas_column', 10, 3);

    Edit: This is the SQL command I need. Now how to embed or deliver it and receive the return:

    SELECT meta_value
    FROM <code>wp_usermeta</code>
    where user_ID = 98   // or whatever the dynamic user_id is. 
    and 
    meta_key = "first_name";  // and again for last_name

    This returns the right value I need. Now just delivering the query and receiving output. This is fun.

    • This reply was modified 6 years, 3 months ago by webbuildermn.
    Thread Starter webbuildermn

    (@webbuildermn)

    I found something. See code at bottom. I got it to do what I intended in terms of static row population. Now we have to make this dynamic, first with a variable like user_id and then with SQL query. This is actually pretty fun.

    I guess the $column_Name occupies the second argument in the callback function, which is why it wasn’t finding anything before. I don’t know what the first argument holds. It seems to hold null. When I try to return it, nothing. I can return the $column_name and then every cell is the same as the header. This is logical to me.

    I don’t know how this:
    apply_filters( 'manage_users_custom_column', string $output, string $column_name, int $user_id )

    pertains and maps to my callback function because I get that the $output is the callback function. When I put 10, 2 it seems to work. When I try do anything else I get argument count errors or I don’t know hwo to connect them but my goal was to a) figure out what is argument one, i.e. $what_is, and b) get the $user_id info so I can echo it and also maybe it will be necessary for SQL query. I’ve been tryin got follow this code signature.

    Anyway here’s the code I got to statically populate the table!

    // // Populate the column - this works, statically!!
    function add_content_to_mishas_column($what_is, $column_name){
    	if ($column_name == "FirstName"){
    		return "Romeo";
    	}
    	if ($column_name == "LastName"){
    		return "Montague";
      }
    }
    add_filter('manage_users_custom_column', 'add_content_to_mishas_column', 10, 2);

    I think I will need this soon as well.

    • This reply was modified 6 years, 3 months ago by webbuildermn.
    Thread Starter webbuildermn

    (@webbuildermn)

    1. I’m close to my goal but stuck near the end. There are two ways I can get to the end zone but I may need some help, if only a link or a point in the right direction.

    2. I’ve been using this reference to make sortable columns, taking it step by step. All my code is below. It is all inputted into the functions.php file of the theme editor. Edit: It is now in a custom plugin.

    3. I succeeded in adding custom column headers into my users table (Nova, Brilliant.org, “First Name” and “Last Name”. I could not get any data to be populated though, not when I used a variable $post_id or even when I just used echo. This same code worked in the post table yesterday. I got it to display echo 'post ID: '. $post_ID ; perfectly, but here in this table it’s giving me trouble.

    4. I also was able to make these columns sortable because it now shows FirstName as sortable link with the triangle button, even though there’s no data to sort.

    Where it goes wrong: I can’t get the first name or last name data. I don’t know how to do the SQL run.

    Alternatively, I had this plugin, AdminColumns that already has two columns populated, “First Name” and “Last Name”. If I could sort that, that would be the other way. What is interesting is I can’t even get a custom column to show up when that plugin is activated, so I have to deactivate it to get my code below to even work as expected. How/why does this occur?

    So those are my two ways- get the AdminColumns of “First Name” and “Last Name” to sort, or get my columns “FirstName” and “LastName” to get populated. I made them without spaces to differentiate the two.

    Honestly I’m surprised I got this far!! Appreciate all your inputs or even just pointers and links.

    
    // Experimental Sort Users //
    // 
    function add_custom_misha_columns($columns){
    	$columns["Nova"] = "Nova";
    	$columns["Brilliant.org"] = "Brilliant.org";
     	$columns["FirstName"] = "FirstName";
    	$columns["LastName"] = "LastName";
    	return $columns;
    }
    add_filter('manage_users_columns', 'add_custom_misha_columns');
    
    // // Populate the column
    function add_content_to_mishas_column($column_name, $post_id){ // edit: $user_id not $post_id
    	if ($column_name == "FirstName"){
    		echo "Romeo";
    	}
    	if ($column_name == "LastName"){
    		echo "Montague";
    	}
    }
    add_action('manage_users_custom_column', 'add_content_to_mishas_column', 10, 2);
    
    function sort_mishas_user_columns ($columns){
    	$columns["FirstName"] = "FirstName";
    	return $columns;
    }
    
    // Add sortable column to Column Array
    add_filter('manage_users_sortable_columns', 'sort_mishas_user_columns');
    
    
    • This reply was modified 6 years, 3 months ago by webbuildermn.
    • This reply was modified 6 years, 3 months ago by webbuildermn.
    Thread Starter webbuildermn

    (@webbuildermn)

    Cool, thanks. I don’t know why but I hate the idea of data or things being left over, but that answers my question.

    Thread Starter webbuildermn

    (@webbuildermn)

    Thanks again. I read the article. I think it’s exactly what I was looking for. Maybe tomorrow I will try put it into practice. As far as SQL queries, I’m not sure, but our AdminColumns already runs a query I suppose, and gives us First Name and Last name. Maybe we can just hitch a ride on that – if the name of the column would already be in the array. I don’t know 100% what I’m saying, but we’ll see.

    I succeeded on a local instance of the site to create a column with “post id: ” . $post_ID displayed. I could try practice sorting that but I got it made. I just put the code in the functions.php file of the theme(child) but I made my first plugin today so I could try it there as it’s supposed to be better.

    Long story short, I think this article was what I needed, a lot to play with and test.

    Thread Starter webbuildermn

    (@webbuildermn)

    Wow that is very helpful. Thank you, I’ll look into all of this.

    Thread Starter webbuildermn

    (@webbuildermn)

    I came up with a different method to make the site private. I used a plug-in Force login. This made the question irrelevant. Out of curiosity would still be cool to know but it’s not worth putting much time into learning. I guess I can’t delete this post so I thought I’d just reply and Mark resolved

    Thread Starter webbuildermn

    (@webbuildermn)

    That plug in work brilliantly thank you so much. It also solved another minor issue I had.

    I guess I was out of my depth in terms of coding. I did try to change the theme template to no avail. But the plug-in solved it anyway. As they usually do.

    When I first made the blog for him I used illustrator and I did some of customization. We kind of got stuck on that team. Now I use code snippets for the few things that are necessary and plugins I like the one you told me about.

    Thank you

    Thread Starter webbuildermn

    (@webbuildermn)

    Hi. Thanks for the advice. I am a word-press developer in training :). I am building a site for a friend/professor/mentory so it will be nice but not high end budget wise. But I like learning. It’s not a super high priority but I will try what you suggest- switching to parent, and disabling plugins. Thank you!

    Thread Starter webbuildermn

    (@webbuildermn)

    I’m so dumb. I am always so hasty. I mistakenly checked the box Role Based Access- subscriber, because I thought that was what made the users subscribers. I unchecked it. Now they can’t access.

Viewing 14 replies - 1 through 14 (of 14 total)