0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
/**
* Plugin Name: Custom Post Status Table
* Plugin URI: https://srctamil.ch/
* Description: Custom table view for displaying and updating post statuses.
* Version: 1.0.0
* Author: Gnanaguna
* Author URI: https://srctamil.ch/
* License: GPL2
*/
// Register the custom table view
function wpturbo_register_custom_table_view() {
add_menu_page( 'Custom Table View', 'Custom Table View', 'manage_options', 'custom-table-view', 'wpturbo_render_custom_table_view', 'dashicons-admin-post', 25 );
}
add_action( 'admin_menu', 'wpturbo_register_custom_table_view' );
// Render the custom table view
function wpturbo_render_custom_table_view() {
global $wpdb;
// Query for post statuses 'Publish' with date format and order by date in descending order
$statuses = $wpdb->get_results( "
SELECT ID, post_title, post_status, DATE_FORMAT(post_date, '%d.%m.%Y') AS formatted_date
FROM {$wpdb->prefix}posts
WHERE post_type = 'post'
ORDER BY post_date DESC
");
// Output the table
echo '<table id="custom-table" class="wp-list-table widefat fixed striped">';
echo '<thead><tr><th>Post ID</th><th>Title</th><th>Status</th><th>Date</th><th>Update Status</th><th>View Post</th></tr></thead>';
echo '<tbody>';
foreach ( $statuses as $status ) {
echo '<tr>';
echo '<td>' . $status->ID . '</td>';
echo '<td>' . $status->post_title . '</td>';
echo '<td>' . $status->post_status . '</td>';
echo '<td>' . $status->formatted_date . '</td>';
echo '<td>
<form method="post" action="' . esc_url( admin_url( 'admin-post.php' ) ) . '">
<input type="hidden" name="action" value="wpturbo_update_post_status">
<input type="hidden" name="post_id" value="' . $status->ID . '">
<select name="new_status">
<option value="publish">Publish</option>
<option value="draft">Draft</option>
<option value="pending">Pending</option>
</select>
<input type="submit" value="Update">
</form>
</td>';
echo '<td><a href="' . esc_url( admin_url( 'post.php?post=' . $status->ID ) ) . '">View Post</a></td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
// Enqueue DataTables library and initialize the table
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'datatables', 'https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js', array( 'jquery' ), '1.10.24', true );
wp_enqueue_style( 'datatables', 'https://cdn.datatables.net/1.10.24/css/jquery.dataTables.min.css', array(), '1.10.24' );
wp_add_inline_script( 'datatables', '
jQuery(document).ready(function($) {
$("#custom-table").DataTable();
});
' );
}
// Handle post status update
function wpturbo_handle_post_status_update() {
if ( isset( $_POST['action'] ) && $_POST['action'] === 'wpturbo_update_post_status' ) {
$new_status = $_POST['new_status'];
$post_id = absint( $_POST['post_id'] );
// Validate and update post status
if ( in_array( $new_status, array( 'publish', 'draft', 'pending' ), true ) ) {
$updated = wp_update_post( array(
'ID' => $post_id,
'post_status' => $new_status,
) );
if ( $updated ) {
wp_redirect( admin_url( 'admin.php?page=custom-table-view&status=success' ) );
exit;
}
}
wp_redirect( admin_url( 'admin.php?page=custom-table-view&status=error' ) );
exit;
}
}
add_action( 'admin_post_wpturbo_update_post_status', 'wpturbo_handle_post_status_update' );
Custom table view for displaying and updating post statuses.