Skip to content

Instantly share code, notes, and snippets.

@humayunahmed8
Last active October 18, 2023 04:42
Show Gist options
  • Save humayunahmed8/2c311aacd9680457c0dacdfd5a7c66f1 to your computer and use it in GitHub Desktop.
Save humayunahmed8/2c311aacd9680457c0dacdfd5a7c66f1 to your computer and use it in GitHub Desktop.
Add WP custom admin column in CPT
<?php
// Add custom meta field to admin column for custom post type "story"
function add_story_custom_meta_to_admin_column($column_name, $post_id) {
if ($column_name == 'custom_meta_column') {
$stories_meta = get_post_meta($post_id, 'stories-metabox', true);
// Check if the 'title-keyword' key exists in the meta data
if (array_key_exists('title-keyword', $stories_meta)) {
$title_keyword = esc_html($stories_meta['title-keyword']);
echo $title_keyword;
} else {
echo __('Splyt', 'lhcorp');
}
}
}
// Add custom meta field header to admin column for custom post type "story"
function add_story_custom_meta_column_header($columns) {
// Find the position of the date column
$date_position = array_search('date', array_keys($columns));
// Insert the custom column before the date column
$columns = array_slice($columns, 0, $date_position) +
array('custom_meta_column' => 'Stories Title Keyword') +
array_slice($columns, $date_position);
return $columns;
}
// Make the custom column sortable
function make_story_custom_meta_column_sortable($columns) {
$columns['custom_meta_column'] = 'custom_meta_column';
return $columns;
}
// Add custom meta box to quick edit section
function add_story_custom_meta_to_quick_edit() {
global $post_type;
if ('story' == $post_type) {
add_action('quick_edit_custom_box', 'render_quick_edit_custom_box', 10, 2);
}
}
// Render the custom meta box content in quick edit section
function render_quick_edit_custom_box($column_name, $post_type) {
if ($column_name != 'custom_meta_column') {
return;
}
$stories_meta = get_post_meta(get_the_ID(), 'stories-metabox', true);
$title_keyword = isset($stories_meta['title-keyword']) ? esc_html($stories_meta['title-keyword']) : '';
?>
<fieldset class="inline-edit-col-right inline-edit-story">
<div class="inline-edit-col">
<label class="inline-edit-group">
<span class="title"><?php _e('Stories Title Keyword', 'lhcorp'); ?></span>
<input type="text" name="title_keyword" value="<?php echo $title_keyword; ?>">
</label>
</div>
</fieldset>
<?php
}
// Save the custom meta data when quick edit is saved
function save_story_custom_meta_from_quick_edit($post_id) {
if (!current_user_can('edit_post', $post_id)) {
return;
}
if (isset($_REQUEST['title_keyword'])) {
$title_keyword = sanitize_text_field($_REQUEST['title_keyword']);
update_post_meta($post_id, 'stories-metabox', ['title-keyword' => $title_keyword]);
}
}
// Hook the functions
add_filter('manage_story_posts_columns', 'add_story_custom_meta_column_header');
add_action('manage_story_posts_custom_column', 'add_story_custom_meta_to_admin_column', 10, 2);
add_filter('manage_edit-story_sortable_columns', 'make_story_custom_meta_column_sortable');
add_action('admin_enqueue_scripts', 'add_story_custom_meta_to_quick_edit');
add_action('save_post', 'save_story_custom_meta_from_quick_edit');
<?php
function lhcorp_metabox_and_options($metaboxes) {
$metaboxes = array();
// Stories CPT Metaboxes
$metaboxes[] = array(
'id' => 'stories-metabox',
'title' => 'Stories Setting',
'post_type' => 'story',
'context' => 'normal',
'priority' => 'high',
'sections' => array(
array(
'name' => 'stories_settings_section',
'fields' => array(
array(
'id'=>'title-keyword',
'type'=>'text',
'title'=>__('Title Keyword','lhcorp'),
'desc' => '<img src="' . get_template_directory_uri() . '/assets/img/stories-filter.png" style="width: 100%;" />',
'default'=>__('Splyt','lhcorp')
),
array(
'id'=>'excerpt',
'type'=>'textarea',
'title'=>__('Story Excerpt','lhcorp'),
'default'=>__('Lorem ipsum dolor sit amet consectetur adipisicing.','lhcorp')
),
array(
'id'=>'button-label',
'type'=>'text',
'title'=>__('Stories Button Label','lhcorp'),
'default'=>__('Read their story','lhcorp')
),
array(
'id' => 'button-url-type',
'type' => 'select',
'title' => __('Stories Button URL Type', 'lhcorp'),
'options' => array(
'new_tab' => __('New Tab', 'lhcorp'),
'same_tab' => __('Same Tab', 'lhcorp'),
'permalink' => __('Permalink', 'lhcorp'),
),
),
array(
'id' => 'button-link-wp-page',
'type' => 'select',
'title' => __('Select WordPress Page', 'lhcorp'),
'options' => lhcorp_get_wp_pages_options(), // You need to implement the function lhcorp_get_wp_pages_options()
'dependency' => array('button-url-type', '==', 'same_tab'),
),
array(
'id' => 'button-link-custom-url',
'type' => 'text',
'title' => __('Stories Button Custom URL', 'lhcorp'),
'default'=>__('#','lhcorp'),
'dependency' => array('button-url-type', '==', 'new_tab'),
),
),
),
),
);
return $metaboxes;
}
add_filter('cs_metabox_options', 'lhcorp_metabox_and_options');
// Framework Init
function lhcorp_framework_options($options){
$options = array();
}
add_filter( 'cs_framework_options', 'lhcorp_framework_options' );
// Remove Framework
function lhcorp_shortcode_options($options){
$options = array();
}
add_filter('cs_shortcode_options', 'lhcorp_shortcode_options');
// Remove Customizer
function lhcorp_customizer($options){
$options = array();
}
add_filter('cs_customize_options', 'lhcorp_customizer');
// Framework Init
// function lhcorp_option_settings($settings){
// $settings = array();
// $settings = array(
// 'menu_title' => esc_html__('LazukCorp. Setting','lhcorp'),
// 'menu_type' => 'theme', // menu, submenu, options, theme, etc.
// 'menu_slug' => esc_html__('lhcorp-theme-options','lhcorp'),
// 'ajax_save' => true,
// 'show_reset_all' => true,
// 'framework_title' => esc_html__('LazukCorp - by Humayun Ahmed','lhcorp'),
// );
// return $settings;
// }
// add_filter('cs_framework_settings', 'lhcorp_option_settings');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment