Skip to content

Instantly share code, notes, and snippets.

@Qubadi
Last active January 10, 2025 18:11
Show Gist options
  • Save Qubadi/bf450f2678e83cb1a260f6832de96dde to your computer and use it in GitHub Desktop.
Save Qubadi/bf450f2678e83cb1a260f6832de96dde to your computer and use it in GitHub Desktop.
Wordpress: CPT management and user post count display
The code here creates a secure WordPress admin page for managing the slugs of CPTs.
Administrators can add, remove, and save CPT slugs that will be used to display post counts for each CPT in the user list table.
This functionality is secure and integrates seamlessly with WordPress.
Copy the following PHP code and create a PHP snippet using your snippet plugins.
Paste the code into the plugin and save it.
Then, after that, go to CPT Settings in the toolbar dashboard of WordPress, and add all your CPT slug names there,
and click Save. After that, go to Users, and then All Users. There, you will see the CPT and posts counts.
__________________________________________
// Add the menu item for CPT settings
function register_cpt_settings_menu() {
if (!current_user_can('administrator')) return;
add_menu_page('CPT Management', 'CPT Settings', 'administrator', 'cpt-settings',
'cpt_settings_page', 'dashicons-admin-settings', 80);
}
add_action('admin_menu', 'register_cpt_settings_menu');
// Main settings page content
function cpt_settings_page() {
if (!current_user_can('administrator')) {
wp_die('Access denied');
}
// Save settings
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_cpt_settings'])) {
check_admin_referer('save_cpt_settings', 'cpt_settings_nonce');
if (!current_user_can('administrator')) {
wp_die('Access denied');
}
$cpt_slugs = array();
if (!empty($_POST['cpt_slugs']) && is_array($_POST['cpt_slugs'])) {
foreach ($_POST['cpt_slugs'] as $s) {
if ($s) $cpt_slugs[] = sanitize_text_field($s);
}
}
update_option('custom_cpt_slugs', $cpt_slugs, true);
echo '<div class="notice notice-success"><p>Settings saved.</p></div>';
}
$saved = get_option('custom_cpt_slugs', array());
?>
<div class="wrap">
<h1 style="font-size: 24px; margin-bottom: 20px; color: #007cba;">CPT Management</h1>
<form method="post" style="background: #ffffff; padding: 20px; border: 1px solid #ddd; border-radius: 8px;">
<?php wp_nonce_field('save_cpt_settings', 'cpt_settings_nonce'); ?>
<table class="form-table" style="width: 100%; border-spacing: 0 10px;">
<tr>
<th style="text-align: left; font-size: 18px;">Add Your CPT Slug</th>
</tr>
<tr>
<td>
<div id="cpt-slug-list" style="margin-bottom: 15px;">
<?php
if($saved) {
foreach ($saved as $slug) { ?>
<div class="cpt-slug-item" style="display: flex; align-items: center; margin-bottom: 10px;">
<input type="text" name="cpt_slugs[]" value="<?php echo esc_attr($slug); ?>" style="width: 300px; padding: 8px; border: 1px solid #ddd; border-radius: 4px; margin-right: 15px;">
<button type="button" class="button remove-cpt-slug" style="background-color: #dc3545; color: white; border: none; border-radius: 4px; padding: 6px 12px;">Remove</button>
</div>
<?php }
}
?>
</div>
<button type="button" id="add-cpt-slug" class="button" style="background-color: #28a745; color: white; border: none; border-radius: 4px; padding: 6px 15px;">Add CPT Slug</button>
</td>
</tr>
</table>
<?php submit_button('Save Settings', 'primary', 'submit_cpt_settings', false, ['style' => 'background-color: #007cba; color: white; border-radius: 4px; padding: 6px 15px;']); ?>
</form>
</div>
<script>
jQuery(document).ready(function($) {
$('#add-cpt-slug').on('click', function() {
$('#cpt-slug-list').append(
'<div class="cpt-slug-item" style="display: flex; align-items: center; margin-bottom: 10px !important;">' +
'<input type="text" name="cpt_slugs[]" style="width: 300px; padding: 8px; border: 1px solid #ddd; border-radius: 4px; margin-right: 10px;">' +
'<button type="button" class="button remove-cpt-slug" style="background-color: #dc3545; color: white; border: none; border-radius: 4px; padding: 8px 12px;">Remove</button>' +
'</div>'
);
});
$('#cpt-slug-list').on('click', '.remove-cpt-slug', function() {
$(this).parent().remove();
});
});
</script>
<style>
.cpt-slug-item input {
transition: all 0.2s ease;
}
.cpt-slug-item input:focus {
outline: none;
border-color: #007cba;
box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
}
.form-table td {
margin-bottom: 9px;
padding: 10px 0px !important;
line-height: 1.3;
vertical-align: middle;
}
</style>
<?php
}
// Add columns to users list
function add_dynamic_cpt_posts_column($cols) {
if (!current_user_can('administrator')) return $cols;
$slugs = get_option('custom_cpt_slugs', array());
foreach ($slugs as $slug) {
$pt = get_post_type_object($slug);
if ($pt) {
$cols[$slug] = '' . $pt->labels->singular_name;
}
}
return $cols;
}
add_filter('manage_users_columns', 'add_dynamic_cpt_posts_column');
// Display column content
function show_dynamic_cpt_posts_column($val, $col, $uid) {
if (!current_user_can('administrator')) return $val;
$slugs = get_option('custom_cpt_slugs', array());
if (!in_array($col, $slugs)) return $val;
$count = count_user_posts($uid, $col);
return $count ?
sprintf('<a href="%s">%d</a>',
admin_url("edit.php?post_type={$col}&author={$uid}"),
$count
) : '0';
}
add_action('manage_users_custom_column', 'show_dynamic_cpt_posts_column', 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment