Created
July 26, 2023 08:17
-
-
Save luiskabes-arch/5b649a3f5dfb0b7f6ebf494f0600f9e1 to your computer and use it in GitHub Desktop.
Add Featured Post on Custom Post Type (With AJAX)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* At the default, this is will add 'featured' meta post on the 'post' post type, to adjust it with your need | |
* use your IDE editor and use search & replace from your IDE to replace 'featured' & 'post' | |
*/ | |
/** | |
* Register the 'Featured' meta box. | |
*/ | |
function add_featured_meta_box() { | |
add_meta_box( | |
'featured_meta_box', | |
'Featured', | |
'featured_meta_box_callback', | |
'post', | |
'side', | |
'default' | |
); | |
} | |
add_action('add_meta_boxes', 'add_featured_meta_box'); | |
/** | |
* Display the 'Featured' meta box. | |
*/ | |
function featured_meta_box_callback($post) { | |
$featured = get_post_meta($post->ID, 'featured', true); | |
?> | |
<input type="checkbox" name="featured" value="1" <?php checked($featured, 1); ?> /> | |
<label for="featured">Featured</label> | |
<?php | |
} | |
/** | |
* Save the 'Featured' meta box data. | |
*/ | |
function save_featured_meta_box_data($post_id) { | |
$is_featured = array_key_exists('featured', $_POST) ? $_POST['featured'] : 0; | |
update_post_meta($post_id, 'featured', $is_featured); | |
} | |
add_action('save_post', 'save_featured_meta_box_data'); | |
/** | |
* Add the 'Featured' column to the admin list view. | |
*/ | |
function add_featured_column($columns) { | |
$columns['featured'] = 'Featured'; | |
return $columns; | |
} | |
add_filter('manage_edit-post_columns', 'add_featured_column'); | |
/** | |
* Fill the 'Featured' column in the admin list view. | |
*/ | |
function fill_featured_column($column, $post_id) { | |
if ('featured' === $column) { | |
$featured = get_post_meta($post_id, 'featured', true); | |
$nonce = wp_create_nonce('update_featured_meta'); | |
echo '<input type="checkbox" class="featured_checkbox" value="1" ' . checked($featured, 1, false) . ' data-post_id="' . $post_id . '" data-nonce="' . $nonce . '">'; | |
echo ($featured == 1) ? '<span class="featured-text"> Yes</span>' : '<span class="featured-text"> No</span>'; | |
} | |
} | |
add_action('manage_post_posts_custom_column', 'fill_featured_column', 10, 2); | |
/** | |
* Create nonce field for the 'Featured' meta. | |
*/ | |
function featured_meta_nonce_field() { | |
wp_nonce_field('update_featured_meta', 'featured_meta_nonce'); | |
} | |
add_action('add_meta_boxes', 'featured_meta_nonce_field'); | |
/** | |
* AJAX handler to update the 'Featured' meta field. | |
*/ | |
function update_featured_meta() { | |
// Check for nonce validity | |
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'update_featured_meta')) { | |
wp_die(__('Invalid nonce', 'text_domain')); | |
} | |
// Update meta and return the result | |
$post_id = $_POST['post_id']; | |
$featured = $_POST['featured']; | |
update_post_meta($post_id, 'featured', $featured); | |
echo ($featured ? 'Yes' : 'No'); | |
wp_die(); | |
} | |
add_action('wp_ajax_update_featured_meta', 'update_featured_meta'); | |
/** | |
* Add an AJAX script to handle column update in the admin. | |
*/ | |
function add_featured_column_ajax_script() { | |
if ('edit-post' !== get_current_screen()->id) { | |
return; | |
} | |
?> | |
<script> | |
jQuery(document).ready(function($) { | |
$('.column-featured').on('click', '.featured_checkbox', function() { | |
var $this = $(this); | |
var post_id = $this.data('post_id'); | |
var featured = $this.is(':checked') ? 1 : 0; | |
var nonce = $this.data('nonce'); | |
$.ajax({ | |
url: ajaxurl, | |
type: 'POST', | |
data: { | |
action: 'update_featured_meta', | |
post_id: post_id, | |
featured: featured, | |
nonce: nonce | |
}, | |
success: function(response) { | |
$this.closest('td').find('.featured-text').text(response); | |
} | |
}); | |
}); | |
}); | |
</script> | |
<?php | |
} | |
add_action('admin_footer', 'add_featured_column_ajax_script'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment