按IP地址的Wordpress可排序列

时间:2017-05-20 02:02:46

标签: php wordpress jquery-ui-sortable

我已经能够在我的Wordpress用户页面中添加一个可排序的列,显示用户最后知道的IP地址。

我似乎遇到的问题是让列按IP的数字排序,而不是按字母顺序排序用户名。

我觉得我错过了一些简单的东西,可以使用另一双眼睛。

    <?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();

0 个答案:

没有答案