I have been able to add a sortable column to my WordPress userpage that displays the users last know IP address.

The problem I seem to be having is getting the column to sort by the IP’s numbers and not the usernames alphabetically.

I feel im missing something simple and could use another set of eyes.

    <?php
class Register_IP_Multisite {
    public function __construct() {
        add_action( 'init', array( &$this, 'init' ) );
    }
    public function init() {
        add_action( 'user_register', array( $this,'log_ip') );
        add_action( 'edit_user_profile', array( $this,'edit_user_profile') );
        add_action( 'manage_users_custom_column', array( $this,'columns'), 10, 3);  
        add_filter( 'manage_users_sortable_columns', array( $this ,'users_sortable_columns_wsp') );
        add_filter( 'request', array( $this ,'users_orderby_column_wsp') ); 
        add_filter( 'plugin_row_meta', array( $this ,'donate_link'), 10, 2 );
        if ( is_multisite() ) {
            add_filter('wpmu_users_columns', array( $this ,'column_header_signup_ip'));
        } else {
            add_filter('manage_users_columns', array( $this ,'column_header_signup_ip'));
        }
    }

    public function log_ip($user_id){
        $ip = $_SERVER['REMOTE_ADDR']; //Get the IP of the person registering
        update_user_meta($user_id, 'signup_ip', $ip); //Add user metadata to the usermeta table
    }

    public function edit_user_profile() {
            $user_id = (int) $_GET['user_id'];
    ?>
            <h3><?php _e('Signup IP Address', 'register-ip-mutisite'); ?></h3>
            <p style="text-indent:15px;"><?php
            $ip_address = get_user_meta($user_id, 'signup_ip', true);
            echo $ip_address;
            ?></p>
    <?php
    }

    public function column_header_signup_ip($column_headers) {
        $column_headers['signup_ip'] = __('IP Address', 'register-ip-multisite');
        return $column_headers;
    }

    public function users_sortable_columns_wsp($column_headers) {
          $customwsp = array(
          // meta column id => sortby value used in query
          'signup_ip'    => 'user_ip_address',
          );
    return wp_parse_args($customwsp, $column_headers);
    }

    public function users_orderby_column_wsp( $varswsp ) {
        if ( isset( $varswsp['orderby'] ) && 'signup_ip' == $varswsp['orderby'] ) {
                $varswsp = array_merge( $varswsp, array(
                        'meta_key' => 'signup_ip',
                        'orderby' => 'meta_value'
                ) );
        }
    return $varswsp;
    }

    public function columns($value, $column_name, $user_id) {
        global $modewsp;
        $modewsp = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
        if ( $column_name == 'signup_ip' ) {
            $ip = get_user_meta($user_id, 'signup_ip', true);
            if ($ip != ""){
                $theip = $ip;
                if ( has_filter('ripm_show_ip') ) {
                    $theip = apply_filters('ripm_show_ip', $theip);
                }
                return $theip;
            } else {
                $theip = '<em>'.__('None Recorded', 'register-ip-multisite').'</em>';
                return $theip;
           }
        }
        $user_ip_address   = strtotime(get_date_from_gmt($user->signup_ip));

        return $value;
    }
}
new Register_IP_Multisite();

Read more here: WordPress sortable column by IP address


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: