0

I have created a custom page which shows the list of Users(Role = 'Subscriber'). I named this page "Reporting" & It is based on wp_list_table. Now I want to show the user details (having custom layout & user meta). I am not sure How to create a link of user detail page or create a detail page.

Here is the code which shows User lists Data. class-cisco-connex-core-wp-reporting-table.php

class WP_Reporting_Table
{
    /**
     * Constructor
     */
    public function __construct()
    {
        // Hook - Registering Scripts
        add_action( 'admin_menu', array ( $this, 'reporting_users' ) );
        add_action( 'admin_head', array( $this, 'admin_header' ) );
    }

    /**
     * reporting_users function.
     *
     * @access public
     */
    public function reporting_users( $hook_suffix )
    {
        if ( isset ( $_GET['user_id'] ) && !empty ( $_GET['user_id'] ) ) {
            add_action('edit_user_profile', 'cc_reporting_detail');
        } else {
            add_menu_page( 'Reporting', 'Reporting', 'manage_options', 'wp-reporting-table-users', array ( $this, 'cc_reporting' ) );
        }
    }

    public function cc_reporting()
    {
        $usersTable = new WP_Reporting_Table_Users();
        $usersTable->prepare_items();
        ?>
            
                
                

Reporting Page

display(); ?> '; echo '.wp-list-table .column-title { width: 30%; }'; echo '.wp-list-table .column-email { width: 30%; }'; echo '.wp-list-table .column-engagement_score { width: 20%; }'; echo '.wp-list-table .column-date { width: 20%; }'; echo '.wp-list-table .column-title img { float: left; margin-right: 10px; margin-top: 1px; }'; echo ''; } public function cc_reporting_detail() { echo $_GET['user_id']; } } new WP_Reporting_Table(); // WP_List_Table is not loaded automatically so we need to load it in our application if( ! class_exists( 'WP_List_Table' ) ) { require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); }

class-cisco-connex-core-users.php

class WP_Reporting_Table_Users extends WP_List_Table
{

    /**
     * Prepare the items for the table to process
     *
     * @return Void
     */
    public function prepare_items()
    {
        $columns = $this->get_columns();
        $hidden = $this->get_hidden_columns();
        $sortable = $this->get_sortable_columns();

        $data_user = $this->table_data();
        usort( $data_user, array( &$this, 'sort_data' ) );

        $perPage = 20;
        $currentPage = $this->get_pagenum();
        $totalItems = count($data_user);

        $this->set_pagination_args( array(
            'total_items' => $totalItems,
            'per_page'    => $perPage
        ) );

        $data_user = array_slice($data_user,(($currentPage-1)*$perPage),$perPage);

        $this->_column_headers = array($columns, $hidden, $sortable);
        $this->items = $data_user;
    }

    /**
     * Override the parent columns method. Defines the columns to use in your listing table
     *
     * @return Array
     */
    public function get_columns()
    {
        $columns = array(
            'title'       => __('Name', ''),
            'email' => __('Email', ''),
            'engagement_score'  => __('Engagement Score (Weekly Change)', ''),
            'date'      => __('Date', '')
        );
        return $columns;
    }

    /**
     * Define which columns are hidden
     *
     * @return Array
     */
    public function get_hidden_columns()
    {
        return array();
    }

    /**
     * Define the sortable columns
     *
     * @return Array
     */
    public function get_sortable_columns()
    {
        return array('email' => array('email', false));
    }

    /**
     * Get the table data
     *
     * @return Array
     */
    private function table_data()
    {
        $data = array();

        $args = array(
            'role' => 'Subscriber'
        );
        $user_query = new WP_User_Query( $args );


        if ( ! empty( $user_query->results ) ) {
            foreach ( $user_query->results as $user ) {
                $user_picture = get_user_meta($user->ID, 'rdp_ll_picture_url', TRUE);
                $user_detail_link = 'Detail';
                $data[] = array(
                    'title' => 'display_name .'" style="width: 32px;">' . $user->display_name . $user_detail_link,
                    'email' => $user->user_email,
                    'engagement_score' => '10',
                    'date' => date('m/d/Y',strtotime( $user->user_registered ))
                );
            }
        }
        return $data;
    }

    /**
     * Define what data to show on each column of the table
     *
     * @param  Array $item        Data
     * @param  String $column_name - Current column name
     *
     * @return Mixed
     */
    public function column_default( $item, $column_name )
    {
        switch( $column_name ) {
            case 'title':
            case 'email':
            case 'engagement_score':
            case 'date':
                return $item[ $column_name ];
            default:
                return print_r( $item, true ) ;
        }
    }

    /**
     * Allows you to sort the data by the variables set in the $_GET
     *
     * @return Mixed
     */
    private function sort_data( $a, $b )
    {
        // Set defaults
        $orderby = 'title';
        $order = 'asc';

        // If orderby is set, use this as the sort column
        if(!empty($_GET['orderby']))
        {
            $orderby = $_GET['orderby'];
        }

        // If order is set use this as the order
        if(!empty($_GET['order']))
        {
            $order = $_GET['order'];
        }
        $result = strnatcmp( $a[$orderby], $b[$orderby] );

        if($order === 'asc')
        {
            return $result;
        }
        return -$result;
    }
}

Your prompt response would be highly appreciated.

Thanks

0

It sounds like maybe you need to add a settings page, (https://developer.wordpress.org/plugins/settings/custom-settings-page/) This would be good if you had something really custom (customer user meta) and a layout that you wanted to keep separate from the regular user pages.

Otherwise, why not just link to the core wordpress user pages? Just build your links like WordPress does and pass the user_id into the query string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.