Written July 13, 2011
Tagged custom, order
In this tutorial, we will add 2 extra fields to the page object:
- Page Image: An image field (we can show this as a thumbnail in the column)
- Featured: A true / false field (we can filter the pages via this field)
1. Add extra page fields
Using the Advanced Custom Fields Plugin, add a new ACF group with the 2 fields. Note: The image field needs to save the attachment ID so we can easily retrieve the thumbnail version for the column.
2. Add a sprinkle of code
Now that our pages have extra fields, let’s add in the code to show the fields in the page’s columns. The 2 functions bellow hook into WordPress and override the column creation process. the “my_page_columns” function overrides what the columns are and the “my_custom_columns” function tells WordPress what to do with the columns.
<?php
/*-------------------------------------------------------------------------------
Custom Columns
-------------------------------------------------------------------------------*/
function my_page_columns($columns)
{
$columns = array(
'cb' => '<input type="checkbox" />',
'thumbnail' => 'Thumbnail',
'title' => 'Title',
'featured' => 'Featured',
'author' => 'Author',
'date' => 'Date',
);
return $columns;
}
function my_custom_columns($column)
{
global $post;
if($column == 'thumbnail')
{
echo wp_get_attachment_image( get_field('page_image', $post->ID), array(200,200) );
}
elseif($column == 'featured')
{
if(get_field('featured'))
{
echo 'Yes';
}
else
{
echo 'No';
}
}
}
add_action("manage_pages_custom_column", "my_custom_columns");
add_filter("manage_edit-page_columns", "my_page_columns");This is what your page columns should look like
3. And a pinch of salt: Sortable Columns
To finish of this tutorial, lets now allow the featured column to be sortable. This can be a very useful feature when working with lots of page / post objects.
<?php
/*-------------------------------------------------------------------------------
Sortable Columns
-------------------------------------------------------------------------------*/
function my_column_register_sortable( $columns )
{
$columns['featured'] = 'featured';
return $columns;
}
add_filter("manage_edit-page_sortable_columns", "my_column_register_sortable" );
?>Wrapping Up
I hope you enjoyed this simple but useful tutorial. We covered some advanced WordPress & PHP techniques and created an attractive and user friendly admin edit page. Be sure to tweet about this article, thanks for reading.
Thank you for reading
Join in the discussion bellow by tweeting about this post.