Skip to content

Instantly share code, notes, and snippets.

@full-stack-king
Created July 12, 2019 04:29
Show Gist options
  • Save full-stack-king/6760e4a9c671a85a7e1b3bd256278a9a to your computer and use it in GitHub Desktop.
Save full-stack-king/6760e4a9c671a85a7e1b3bd256278a9a to your computer and use it in GitHub Desktop.
Most used wordpress custom codes
<?php
//////////////////////
// Custom Post Type //
//////////////////////
/*
* Creating a function to create our CPT tours
*/
function custom_post_type_tours() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Tours', 'Post Type General Name', 'twentythirteen' ),
'singular_name' => _x( 'Tour', 'Post Type Singular Name', 'twentythirteen' ),
'menu_name' => __( 'Tours', 'twentythirteen' ),
'parent_item_colon' => __( 'Parent Tour', 'twentythirteen' ),
'all_items' => __( 'All Tours', 'twentythirteen' ),
'view_item' => __( 'View Tour', 'twentythirteen' ),
'add_new_item' => __( 'Add New Tour', 'twentythirteen' ),
'add_new' => __( 'Add New', 'twentythirteen' ),
'edit_item' => __( 'Edit Tour', 'twentythirteen' ),
'update_item' => __( 'Update Tour', 'twentythirteen' ),
'search_items' => __( 'Search Tour', 'twentythirteen' ),
'not_found' => __( 'Not Found', 'twentythirteen' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentythirteen' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'tours', 'twentythirteen' ),
'description' => __( 'Tours information', 'twentythirteen' ),
'menu_icon' => 'dashicons-location',
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'custom-fields', 'revisions', 'page-attributes', ),
// You can associate this CPT with a taxonomy or custom taxonomy.
'taxonomies' => array( 'genres' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
// Registering your Custom Post Type
register_post_type( 'tours', $args );
}
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init', 'custom_post_type_tours', 0 );
/**
* add a new column called order to admin tour listing screen
*/
function add_new_tour_column($post_columns) {
$post_columns['menu_order'] = "Order";
return $post_columns;
}
add_action('manage_edit-tours_columns', 'add_new_tour_column');
// re-order the custom column in listing page
function your_columns_head($defaults) {
$new = array();
$menu_order = $defaults['menu_order']; // save the menu_order column
unset($defaults['menu_order']); // remove it from the columns list
foreach($defaults as $key=>$value) {
if($key=='author') { // when we find the author
$new['menu_order'] = $menu_order; // put the menu_order column before it
}
$new[$key]=$value;
}
return $new;
}
add_filter('manage_tours_posts_columns', 'your_columns_head');
/**
* show values of tours order column
*/
function show_order_column($name){
global $post;
switch ($name) {
case 'menu_order':
$order = $post->menu_order;
echo $order;
break;
default:
break;
}
}
add_action('manage_tours_posts_custom_column','show_order_column');
/**
* make column sortable
*/
function order_column_register_sortable($columns){
$columns['menu_order'] = 'menu_order';
return $columns;
}
add_filter('manage_edit-tours_sortable_columns','order_column_register_sortable');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment