View User Entry Page

User
class Pending_User_Approval_List extends WP_List_Table {

// Constructor
public function __construct() {
parent::__construct( [
'singular' => 'user', // Singular name of the item
'plural' => 'users', // Plural name of the item
'ajax' => false, // Whether this table supports ajax
] );
}

// Define the columns for the table
public function get_columns() {
// Return an array of columns with the column name as the key and label as the value
return [
'username' => 'Username',
'email' => 'Email',
'signup' => 'Sign Up Date',
'actions' => 'Actions',
];
}

// Prepare the items for the table
public function prepare_items() {
global $wpdb;

$per_page = 10; // Number of items per page
$current_page = $this->get_pagenum();

// Get the users with 'aiowps_account_status' set to 'lock'
$args = [
'post_type' => 'apply_lising_front',
'posts_per_page' => $per_page,
'paged' => $current_page,
'post_status'=>'pending',
'fields' => 'all',
];

// Use WP_Query to get the posts
$query = new WP_Query($args);
$users = $query->posts; // Get the posts
$total_users = $query->found_posts;

// Prepare the data (rows)
$data = [];
foreach ( $users as $user ) {
$dataser=unserialize($user->post_content);
$data[] = [
'username' => $dataser['first_name'].' '.$dataser['last_name'],
'email' => $dataser['email'],
'signup' => date('F j, Y at g:i a', strtotime($user->post_date)),
'actions' => sprintf(
'Approve',
admin_url('admin.php?action=userwp_approve_user&user_id=' . $user->ID)
).' | '.sprintf(
'Cancel',
admin_url('admin.php?action=userwp_cancel_user&user_id=' . $user->ID)
).sprintf(
'View Entry',
admin_url('admin.php?action=viewentry&user_id=' . $user->ID)
)
];
}
$this->set_pagination_args([
'total_items' => $total_users, // Total number of items
'per_page' => $per_page, // Number of items per page
'total_pages' => ceil($total_users / $per_page) // Total number of pages
]);
// Set the data for the table
$this->items = $data;
// Getting column names
$columns = $this->get_columns();

// Setting columns to hide
$hidden = $this->get_hidden_columns();

// print_r($columns);
// Setting column names to sort by
$sortable = $this->get_sortable_columns();

// Showing columns in table
$this->_column_headers = array($columns,$hidden,$sortable);
// Pagination

}

public function get_hidden_columns()
{
return array('id');
}

// Add column names that has sortable functionality
public function get_sortable_columns()
{
return array(

);
}

public function column_default($item, $column_name)
{
switch ($column_name) {
case 'username':

return $item['username'];

break;
case 'email':

return $item['email'];

break;
case 'signup':

return $item['signup'];

break;
case 'actions':
return $item['actions'];
break;
default:
return isset($item[$column_name]) ? $item[$column_name] : '';
}
}

// Column output
public function column_username( $item ) {
return esc_html( $item['username'] );
}

public function column_actions( $item ) {
return $item['actions'];
}
}

// Display the list table
function userwp_display_pending_users() {
$pending_users_table = new Pending_User_Approval_List();
$pending_users_table->prepare_items();
?>

Pending User Approvals

Assistant
To create a "View Entry" page where you can show detailed information for each user, you can follow these steps:

### 1. Create the "View Entry" Action

In your code, you have already created a link to the "View Entry" page in the `actions` column. The link is generated with this part of your code:

sprintf(
    '<a href="%s">View Entry</a>',
    admin_url('admin.php?action=viewentry&user_id=' . $user->ID)
)

This is directing the user to an `admin.php` URL with the action `viewentry` and the user ID as a query parameter.

### 2. Handle the Custom Action in WordPress

Next, you need to handle the `viewentry` action in WordPress. You can do this by adding a custom function that will be triggered when the user clicks the "View Entry" link.

Add the following code to your plugin or theme's `functions.php` file:

// Hook to catch the 'viewentry' action and show the user details page
add_action('admin_action_viewentry', 'userwp_view_user_entry');

function userwp_view_user_entry() {
    // Check if the 'user_id' parameter is present
    if (isset($_GET['user_id'])) {
        $user_id = intval($_GET['user_id']);
        
        // Get the user details using the user ID
        $user = get_post($user_id);

        if ($user) {
            // Unserialize post content for additional user details
            $user_details = unserialize($user->post_content);
            
            // Display user details
            echo '<div class="wrap">';
            echo '<h2>User Details</h2>';
            echo '<p><strong>Username:</strong> ' . esc_html($user_details['first_name'] . ' ' . $user_details['last_name']) . '</p>';
            echo '<p><strong>Email:</strong> ' . esc_html($user_details['email']) . '</p>';
            echo '<p><strong>Sign Up Date:</strong> ' . date('F j, Y at g:i a', strtotime($user->post_date)) . '</p>';
            // Add any other user details you want to display
            echo '</div>';
        } else {
            echo '<div class="wrap"><p>User not found.</p></div>';
        }
    } else {
        echo '<div class="wrap"><p>No user ID specified.</p></div>';
    }
}

### 3. Add Navigation Back to the List

It might be helpful to allow the user to navigate back to the pending users list. After displaying the user details, you can add a back link like this:

echo '<p><a href="' . admin_url('admin.php?page=pending_user_approvals') . '">Back to Pending Users</a></p>';

Make sure to replace `'pending_user_approvals'` with the correct page slug if it's different.

### 4. Test the Functionality

Once you add this code, go to the "Pending User Approvals" table in the WordPress admin. Click the "View Entry" link next to any user, and it should take you to a page showing the details of that user.

User
display in admin interface
Assistant
To display the user details in the WordPress admin interface properly when you click the "View Entry" link, you need to ensure the page is loaded within the WordPress admin dashboard. Here's how you can do it:

### 1. Add a Custom Admin Page to Display the User Details

You'll need to create a custom admin page that will show the details of the selected user. You can do this by using `add_menu_page` or `add_submenu_page` to add a new page to the admin menu. Since the "View Entry" link is already pointing to `admin.php?action=viewentry&user_id=`, you can hook into this action to display the user details.

### 2. Handle the Action and Display the Details

Here's how you can modify your code to properly show the user details on a custom admin page.

#### Step 1: Hook into the `admin_action_viewentry`

Update your `functions.php` file or plugin code to handle the `viewentry` action and display the user details on an admin page.

// Hook into the 'viewentry' action in the admin interface
add_action('admin_action_viewentry', 'userwp_view_user_entry_page');

function userwp_view_user_entry_page() {
    // Ensure the user is logged in and has permissions
    if ( !current_user_can( 'manage_options' ) ) {
        wp_die( 'You do not have sufficient permissions to view this page.' );
    }

    // Check if the user_id is set in the query parameter
    if (isset($_GET['user_id'])) {
        $user_id = intval($_GET['user_id']);
        
        // Get the user post (or any other data if stored elsewhere)
        $user = get_post($user_id);

        if ($user) {
            // Unserialize post content if necessary (as per your earlier code)
            $user_details = unserialize($user->post_content);
            
            // Display the user details
            echo '<div class="wrap">';
            echo '<h2>User Details</h2>';
            echo '<p><strong>Username:</strong> ' . esc_html($user_details['first_name'] . ' ' . $user_details['last_name']) . '</p>';
            echo '<p><strong>Email:</strong> ' . esc_html($user_details['email']) . '</p>';
            echo '<p><strong>Sign Up Date:</strong> ' . date('F j, Y at g:i a', strtotime($user->post_date)) . '</p>';
            // Add any other user details you want to display
            echo '</div>';
        } else {
            echo '<div class="wrap"><p>User not found.</p></div>';
        }

        // Display a back link to go back to the list
        echo '<p><a href="' . admin_url('admin.php?page=pending_user_approvals') . '">Back to Pending Users</a></p>';
    } else {
        echo '<div class="wrap"><p>No user ID specified.</p></div>';
    }
}

#### Step 2: Update the "View Entry" Link

In your `Pending_User_Approval_List` class, update the "View Entry" link to point to the correct admin URL. You want to ensure that it takes the user to the newly created admin page for user details:

// Replace this line:
'<a href="%s">View Entry</a>',
admin_url('admin.php?action=viewentry&user_id=' . $user->ID)

// With this:
'<a href="%s">View Entry</a>',
admin_url('admin.php?action=viewentry&user_id=' . $user->ID)

#### Step 3: Ensure the Admin Menu is Correct

In your code, you may want to ensure that the page you are linking to is part of the admin menu. If you're using a submenu page for "Pending User Approvals," make sure that it's registered properly.

You can add this inside your plugin or `functions.php` file to register a submenu for displaying the pending users list:

// Add a submenu for Pending Users
add_action('admin_menu', 'userwp_add_pending_users_menu');

function userwp_add_pending_users_menu() {
    add_submenu_page(
        'users.php',            // Parent menu (Users)
        'Pending User Approvals', // Page title
        'Pending Approvals',     // Menu title
        'manage_options',        // Capability required to view the page
        'pending_user_approvals', // Slug for the page
        'userwp_display_pending_users' // Callback function to display the page
    );
}

#### Step 4: Display the Table for Pending Users

Your `userwp_display_pending_users` function already handles the display of the pending users list. If you've registered the submenu page correctly, WordPress will display the pending users table under the "Users" menu. The "View Entry" link will open the user details page in the admin interface.

#### Step 5: Test the Functionality

1. Go to **Users > Pending Approvals** in the admin menu.
2. You should see the list of pending users.
3. Click on "View Entry" next to a user.
4. This will take you to the "View Entry" page, where the details of the user will be displayed.

Make sure that you're logged in with an admin user who has the necessary permissions to view this page. If you encounter any issues, double-check the query parameters and ensure your custom admin actions are set up correctly.

Leave a Comment

Your email address will not be published. Required fields are marked *