This site is running Voxel Theme Version: 1.6.0 | Elementor Plugin Version: 3.28.4

Join the Voxel Guide Community!

Get Involved

Add Approve and Unapprove Quick Actions

Description/Instructions

Adding the ability to approve and unapprove a post using the quick actions in the post list.

If this snippet helped, feel free to buy me a taco :)

// Inject minimal CSS to ensure visibility
add_action('admin_head', function() {
global $pagenow;
$custom_post_types = get_post_types(array('public' => true, '_builtin' => false), 'names');
if ($pagenow === 'edit.php' && isset($_GET['post_type']) && in_array($_GET['post_type'], $custom_post_types)) {
echo '
.custom-approve-button, span.approve, span.approve a {
display: inline-block !important;
visibility: visible !important;
color: green !important;
}
.custom-unapprove-button, span.unapprove, span.unapprove a {
display: inline-block !important;
visibility: visible !important;
color: orange !important;
}
.row-actions span.approve, .row-actions span.unapprove { display: inline !important; }
';
}
}, 9999);

// Handle custom action redirects
add_action('admin_init', function() {
$custom_post_types = get_post_types(array('public' => true, '_builtin' => false), 'names');
if (isset($_GET['post_type']) && in_array($_GET['post_type'], $custom_post_types) && isset($_GET['custom_action']) && isset($_GET['post']) && isset($_GET['_wpnonce'])) {
$post_id = intval($_GET['post']);
$nonce = sanitize_text_field($_GET['_wpnonce']);
$action = sanitize_text_field($_GET['custom_action']);
$post_type = sanitize_text_field($_GET['post_type']);

if (wp_verify_nonce($nonce, $action . '_' . $post_type . '_' . $post_id)) {
wp_safe_redirect(admin_url('edit.php?post_type=' . $post_type . '&action=' . $action . '&post=' . $post_id . '&_wpnonce=' . $nonce));
exit;
} else {
wp_die('Security check failed. Please try again.');
}
}
});

// Handle the approve action
add_action('admin_action_approve', 'handle_custom_post_approve');
function handle_custom_post_approve() {
if (!current_user_can('edit_posts')) {
wp_die('Unauthorized access.');
}

$post_id = isset($_GET['post']) ? intval($_GET['post']) : 0;
$nonce = isset($_GET['_wpnonce']) ? sanitize_text_field($_GET['_wpnonce']) : '';
$post_type = isset($_GET['post_type']) ? sanitize_text_field($_GET['post_type']) : '';

if (!$post_id || !$post_type || !wp_verify_nonce($nonce, 'approve_' . $post_type . '_' . $post_id)) {
wp_die('Invalid request.');
}

$post = get_post($post_id);
if ($post && $post->post_type === $post_type) {
$result = wp_update_post(array(
'ID' => $post_id,
'post_status' => 'publish'
));

if (is_wp_error($result)) {
wp_die('Failed to approve post: ' . $result->get_error_message());
}

wp_safe_redirect(admin_url('edit.php?post_type=' . $post_type));
exit;
}

wp_die('Invalid post.');
}

// Handle the unapprove action
add_action('admin_action_unapprove', 'handle_custom_post_unapprove');
function handle_custom_post_unapprove() {
if (!current_user_can('edit_posts')) {
wp_die('Unauthorized access.');
}

$post_id = isset($_GET['post']) ? intval($_GET['post']) : 0;
$nonce = isset($_GET['_wpnonce']) ? sanitize_text_field($_GET['_wpnonce']) : '';
$post_type = isset($_GET['post_type']) ? sanitize_text_field($_GET['post_type']) : '';

if (!$post_id || !$post_type || !wp_verify_nonce($nonce, 'unapprove_' . $post_type . '_' . $post_id)) {
wp_die('Invalid request.');
}

$post = get_post($post_id);
if ($post && $post->post_type === $post_type) {
$result = wp_update_post(array(
'ID' => $post_id,
'post_status' => 'pending'
));

if (is_wp_error($result)) {
wp_die('Failed to unapprove post: ' . $result->get_error_message());
}

wp_safe_redirect(admin_url('edit.php?post_type=' . $post_type));
exit;
}

wp_die('Invalid post.');
}

add_filter('post_row_actions', 'add_custom_post_approval_actions', 99999, 2);
function add_custom_post_approval_actions($actions, $post) {
// Get all public custom post types, excluding built-in ones
$custom_post_types = get_post_types(array('public' => true, '_builtin' => false), 'names');

// Check if the post type is a custom post type
if (!in_array($post->post_type, $custom_post_types)) {
return $actions;
}

// Store default actions
$trash = $actions['trash'] ?? null;
unset($actions['trash']);

// Initialize new actions array
$new_actions = [];

// Add Approve/Unapprove
if ($post->post_status === 'pending') {
$new_actions['approve'] = sprintf(
'%s',
wp_nonce_url(admin_url('edit.php?post_type=' . $post->post_type . '&custom_action=approve&post=' . $post->ID), 'approve_' . $post->post_type . '_' . $post->ID),
$post->ID,
__('Approve')
);
} elseif ($post->post_status === 'publish') {
$new_actions['unapprove'] = sprintf(
'%s',
wp_nonce_url(admin_url('edit.php?post_type=' . $post->post_type . '&custom_action=unapprove&post=' . $post->ID), 'unapprove_' . $post->post_type . '_' . $post->ID),
$post->ID,
__('Unapprove')
);
}

// Merge default actions
$new_actions = array_merge($new_actions, $actions);

// Restore Trash if needed
if ($trash) {
$new_actions['trash'] = $trash;
}

return $new_actions;
}

  • PHP
Copy Code

Let's Chat About this Snippet

Chat Toggle
Voxel Guide AI
Voxel Guide AI
Voxel Guide AI
Voxel Guide AI
Ask me anything about Voxel!
Send
Powered by AI24