Add your custom columns to WordPress admin panel tables
Quick Tip: Add Custom Columns in WordPress Manage Screens
require_once ('functions/city.php');
city.php
function city_modify_post_table( $column ) {
$column['country'] = 'Country';
$column['post_thumbnail'] = 'Photo';
$column['menu_order'] = 'Order';
$column['hot'] = 'Hot';
return $column;
}
add_filter( 'manage_city_posts_columns', 'city_modify_post_table' );
function city_modify_post_table_row( $column_name, $post_id ) {
$custom_fields = get_post_custom( $post_id );
$post = get_post( $post_id );
switch ($column_name) {
case 'country':
echo get_post($custom_fields['country'][0])->post_title;
break;
case 'post_thumbnail' :
echo get_the_post_thumbnail($post_id, "thumbnail");
break;
case 'menu_order':
echo $post->menu_order;
break;
case 'hot' :
echo $custom_fields['hot'][0] ? '<span class="dashicons dashicons-yes" style="color: green"></span>' : '<span class="dashicons dashicons-no-alt" style="color: red"></span>';
break;
default:
}
}
add_action('manage_city_posts_custom_column', 'city_modify_post_table_row', 10, 2);
//add_filter( 'manage_post_posts_columns', 'wl_modify_post_table' ); - default posts only
//add_action('manage_page_posts_custom_column', 'ST4_columns_content', 10, 2); - default pages only
How to Create Custom WordPress Write/Meta Boxes
add_action( 'add_meta_boxes', 'cd_meta_box_add_city' );
function cd_meta_box_add_city(){
add_meta_box( 'my-meta-box-posts', 'Custom fields', 'cd_meta_box_city', 'city', 'normal', 'high' );
}
function cd_meta_box_city(){
//echo 'What you put here, show\'s up in the meta box';
$values = get_post_custom($post->ID);
$h1 = isset( $values['h1'] ) ? esc_attr( $values['h1'][0] ) : '';
$short_description = isset( $values['short_description'] ) ? esc_attr( $values['short_description'][0] ) : '';
$hot = isset( $values['hot'] ) ? esc_attr( $values['hot'][0] ) : '';
$country = isset( $values['country'] ) ? esc_attr( $values['country'][0] ) : '';
$args = array('posts_per_page' => -1, 'orderby' => 'post_order', 'order' => 'ASC', 'post_type' => "country", 'post_parent' => 0);
query_posts($args);
$countries = get_posts( $args );
?>
<label for="h1">H1</label><br />
<input type="text" name="h1" id="h1" value="<?php _e($h1) ?>" class="regular-text" />
<br />
<label for="short_description">Short Description</label><br />
<textarea name="short_description" id="short_description" rows="8" cols="80"><?php _e($short_description) ?></textarea>
<br />
<label for="hot">Hot</label><br />
<input type="checkbox" name="hot" id="hot" value="1" <?php if($hot): ?> checked="checked"<?php endif; ?> />
<p>
<label for="country">Country</label>
<select name="country" id="country">
<? if(!empty($countries)): ?>
<?php foreach ( $countries as $p ) : ?>
<option value="<?php _e($p->ID) ?>" <?php selected($country, $p->ID); ?>><?php _e($p->post_title) ?></option>
<?php endforeach; ?>
<? endif; ?>
<?php wp_reset_postdata();?>
<?php wp_reset_query();?>
</select>
</p>
<?php
}
add_action( 'save_post', 'wl_meta_box_save_city' );
function wl_meta_box_save_city( $post_id ){
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !current_user_can( 'edit_post' ) ) return;
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchors can only have href attribute
)
);
if( isset( $_POST['h1'] ) )
update_post_meta( $post_id, 'h1', esc_attr( $_POST['h1'] ) );
if( isset( $_POST['short_description'] ) )
update_post_meta( $post_id, 'short_description', $_POST['short_description'] );
$hot = $_POST['hot'] ? 1 : 0;
update_post_meta( $post_id, 'hot', $hot );
if( isset( $_POST['country'] ) )
update_post_meta( $post_id, 'country', esc_attr( $_POST['country'] ) );
}
Display a custom post type’s media library inline on the WordPress edit page screen
Backoffice list change order
function change_order( $query ) {
if(is_admin() && $query->is_main_query() && $query->query_vars["post_type"] == "newsletter" ){
$query->set( 'order' , 'desc' );
$query->set( 'orderby', 'date_query');
}
}
add_action( 'pre_get_posts', 'change_order', 1 );
Adding buttons at list overview of post type
add_filter('views_edit-newsletter','export_button');
function export_button($views){
$views['export'] = '<a href="'.admin_url( 'admin-post.php?action=print.csv' ).'" class="primary">Export</a>';
return $views;
}
add_action( 'admin_post_print.csv', 'print_csv' );
function print_csv(){}
Adding filter by custom field
//Admin Filter BY custom fields
add_action( 'restrict_manage_posts', 'wl_admin_posts_filter_restrict_manage_posts' );
function wl_admin_posts_filter_restrict_manage_posts(){
$type = 'post';
if (isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
//only add filter to post type you want
if ('plan' == $type){
//change this to the list of values you want to show
//in 'label' => 'value' format
$values = array(
_('Inspire me') => 1,
_('Not inspire me') => 0
);
?>
<select name="inspireme">
<option value="3"><?php _e('All '); ?></option>
<?php
$current_v = isset($_GET['inspireme'])? $_GET['inspireme']:'';
foreach ($values as $label => $value) {
printf
(
'<option value="%s"%s>%s</option>',
$value,
$value == $current_v? ' selected="selected"':'',
$label
);
}
?>
</select>
<?php
}
}
add_filter( 'parse_query', 'wl_posts_filter' );
/**
* if submitted filter by post meta
* @param (wp_query object) $query
*
* @return Void
*/
function wl_posts_filter( $query ){
global $pagenow;
$type = 'post';
if (isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
if ( 'plan' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['inspireme']) && $_GET['inspireme'] != ''&& $_GET['inspireme'] !=3) {
$query->query_vars['meta_key'] = 'inspireme';
$query->query_vars['meta_value'] = $_GET['inspireme'];
}
}