Posted By

eridesigns on 10/30/12


Tagged

post type custom metabox


Versions (?)

Custom Post Type with a Metabox field


 / Published in: PHP
 

Does not work at the moment. So don't copy it.

  1. <?php
  2.  
  3. function add_post_type($name, $args = array()) {
  4. add_action('init', function() use($name, $args) {
  5.  
  6. $upper = ucwords($name);
  7. $name = strtolower(str_replace(' ','_',$name));
  8.  
  9. $args = array_merge(
  10.  
  11. 'public'=> true,
  12. 'label' => "All $upper" . 's',
  13. 'labels' => array('add_new_item' => "Add New $upper"),
  14. 'support' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields'),
  15. 'taxonomies' => array('post_tag','category')
  16. ),
  17.  
  18. $args
  19.  
  20. );
  21.  
  22. register_post_type('$name', $args);
  23. });
  24.  
  25. }
  26.  
  27.  
  28. //now we create and register a taxonomy
  29. function add_taxonomy($name, $post_type, $args = array()) {
  30. $name = strtolower($name);
  31.  
  32. add_action('init', function() use($name, $post_type, $args) {
  33. $args = array_merge(
  34. 'label' => ucwords($name),
  35.  
  36. ),
  37. $args
  38. );
  39. register_taxonomy($name, $post_type, $args);
  40. });
  41. }
  42.  
  43. /************************************************************
  44.   Now we add the names of the custom post type and taxonomies
  45. *************************************************************/
  46.  
  47.  
  48. add_post_type('snippet', array(
  49. 'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields'),
  50. 'taxonomies' => array('post_tag')
  51. ));
  52.  
  53.  
  54. add_taxonomy('language', 'snippet');
  55.  
  56. /************************************************************
  57.   Creating Metaboxes
  58. *************************************************************/
  59.  
  60. add_action('add_meta_boxes', function() {
  61. add_meta_box(
  62. 'er_snippet_info',
  63. 'Snippet Info',
  64. 'er_snippet_info_cb',
  65. 'snippet',
  66. 'normal',
  67. 'high'
  68. );
  69. });
  70.  
  71. function er_snippet_info_cb() {
  72. global $post;
  73. $url = get_post_custom($post->ID);
  74.  
  75. ?>
  76.  
  77. <label for="er_associated_url">Associated URL: </label>
  78. <input type="text" name="er_associated_url" id="er_associated_url" value="<?php echo $url; ?>" />
  79.  
  80. <?php
  81. }
  82. add_action('save_post', function () {
  83. global $post;
  84. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
  85. //security check
  86. if ($_POST && !wp_verify_nonce($_POST['er_nonce'], _FILE_)) {
  87. if ( isset ($_POST['er_associated_url']) ) {
  88. update_post_meta($post->ID, 'er_associated_url', $_POST['er_associated_url']);
  89. }
  90. }
  91.  
  92. });
  93. ?>

Report this snippet  

You need to login to post a comment.