Wordpress : Custom columns for edit / post views in the admin


/ Published in: PHP
Save to your folder(s)

The code references a custom post type (mysite_listings) and adds custom columns for editing the listings in the admin. This adds custom fields onto the edit / list views.

Note: You will want to change instances of "mysite_listings" with your own post type as well update the column I've defined to match your own.


Copy this code and paste it in your HTML
  1. //custom columns
  2. add_action("manage_posts_custom_column", "my_custom_columns_fields");
  3. add_filter("manage_edit-mysite_listings_columns", "my_listing_columns");
  4.  
  5. add_action("admin_init", "admin_init");
  6.  
  7. //custom meta box below post description when editing
  8. function admin_init(){
  9. add_meta_box("my-custom-columns-meta", "Listing Details", "my_custom_columns", "mysite_listings", "normal", "low");
  10. }
  11.  
  12. //define listing columns
  13. function my_listing_columns($columns)
  14. {
  15. $columns = array(
  16. "cb" => "<input type=\"checkbox\" />",
  17. "title" => "Listing Title",
  18. "description" => "Description",
  19. "address" => "Address",
  20. "website" => "Website",
  21. "phone" => "Phone Number",
  22. "comments" => "Comments"
  23. );
  24. return $columns;
  25. }
  26. //show the custom columns per post->ID
  27. function my_custom_columns_fields($column)
  28. {
  29. global $post;
  30. $custom = get_post_custom($post->ID);
  31.  
  32. if ("ID" == $column) echo $post->ID;
  33. elseif ("description" == $column) echo $post->post_content;
  34. elseif ("address" == $column) echo $custom['address'][0];
  35. elseif ("website" == $column) echo $custom['website'][0];
  36. elseif ("phone" == $column) echo $custom['phone'][0];
  37. elseif ("comments" == $column) echo $post->comments;
  38. }
  39. //build basic form elements to reference custom column fields for input
  40. function my_custom_columns($column)
  41. {
  42. global $post;
  43. $custom = get_post_custom($post->ID);
  44.  
  45. $address = $custom['address'][0];
  46. $website = $custom['website'][0];
  47. $phone = $custom['phone'][0];
  48. ?>
  49. <p><label>Address:</label><br />
  50. <textarea cols="50" rows="5" name="address"><?php echo $address; ?></textarea></p>
  51. <p><label>Website:</label><br />
  52. <input type="text" name="website" size="100" value="<?php echo $website; ?>" /></p>
  53. <p><label>Phone:</label><br />
  54. <input type="text" name="phone" size="100" value="<?php echo $phone; ?>" /></p>
  55. <?php
  56. }
  57.  
  58. // hook into save post with our meta columns
  59. add_action('save_post', 'save_details');
  60.  
  61. // grab all the form inputs and save them associated by post->ID
  62. function save_details(){
  63. global $post;
  64.  
  65. update_post_meta($post->ID, "address", $_POST["address"]);
  66. update_post_meta($post->ID, "website", $_POST["website"]);
  67. update_post_meta($post->ID, "phone", $_POST["phone"]);
  68.  
  69. }

URL: http://codex.wordpress.org/Custom_Fields

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.