Advertisement
Guest User

custom metabox is not saving LINK value when updated

a guest
Mar 29th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. <?php
  2. add_action( 'init', 'codex_team_init' );
  3. function codex_team_init() {
  4. $labels = array(
  5. 'name' => _x( 'team', 'post type general name', 'your-plugin-textdomain' ),
  6. 'singular_name' => _x( 'teams', 'post type singular name', 'your-plugin-textdomain' ),
  7. 'menu_name' => _x( 'Team', 'admin menu', 'your-plugin-textdomain' ),
  8. 'name_admin_bar' => _x( 'team', 'add new on admin bar', 'your-plugin-textdomain' ),
  9. 'add_new' => _x( 'Add New', 'team', 'your-plugin-textdomain' ),
  10. 'add_new_item' => __( 'Add New team', 'your-plugin-textdomain' ),
  11. 'new_item' => __( 'New team', 'your-plugin-textdomain' ),
  12. 'edit_item' => __( 'Edit team', 'your-plugin-textdomain' ),
  13. 'view_item' => __( 'View team', 'your-plugin-textdomain' ),
  14. 'all_items' => __( 'All team', 'your-plugin-textdomain' ),
  15. 'search_items' => __( 'Search team', 'your-plugin-textdomain' ),
  16. 'parent_item_colon' => __( 'Parent team:', 'your-plugin-textdomain' ),
  17. 'not_found' => __( 'No team found.', 'your-plugin-textdomain' ),
  18. 'not_found_in_trash' => __( 'No team found in Trash.', 'your-plugin-textdomain' )
  19. );
  20.  
  21. $args = array(
  22. 'labels' => $labels,
  23. 'public' => true,
  24. 'publicly_queryable' => true,
  25. 'show_ui' => true,
  26. 'show_in_menu' => true,
  27. 'query_var' => true,
  28. 'rewrite' => array( 'slug' => 'team' ),
  29. 'supports' => array( 'title', 'editor','page-attributes','thumbnail' ),
  30. 'capability_type' => 'post',
  31. 'has_archive' => true,
  32. 'hierarchical' => false,
  33. 'menu_position' => null,
  34. 'menu_icon' => get_bloginfo('template_directory') . '/img/icon4.png'
  35. );
  36.  
  37. register_post_type( 'team', $args );
  38. }
  39.  
  40. $prefix = 'mmc_';
  41. $post_types = array('post', 'page');
  42. //set up the box and teh fields inside
  43. $meta_box = array(
  44. 'id' => 'meta-box',
  45. 'title' => 'Social Icon', //title of box
  46.  
  47. 'context' => 'side', //normal, advanced or side
  48. 'priority' => 'default',
  49. 'fields' => array( //and now for the custom fields in the box
  50.  
  51. array(
  52. 'name' => 'Icon',
  53. 'id' => $prefix . 'icon',
  54. 'type' => 'select',
  55. 'options' => array('fa-google' => 'google',
  56. 'fa-facebook' => 'facebook',
  57. 'fa-skype' => 'skype',
  58. 'fa-twitter' => 'twitter',
  59. 'fa-yahoo' => 'yahoo',
  60. 'fa-youtube' => 'youtube',
  61. )
  62. )
  63. )
  64. );
  65.  
  66. add_action('admin_menu', 'theme_add_box');
  67.  
  68. // Add meta box
  69. function theme_add_box() {
  70. global $meta_box;
  71. global $post_types;
  72.  
  73. foreach($post_types as $type){
  74. //(id, title, callback, post type, context, priority, callback args)
  75. add_meta_box($meta_box['id'], $meta_box['title'], 'social_meta_box', $type='team', $meta_box['context'], $meta_box['priority']);
  76. }
  77. }
  78. function social_meta_box() {
  79. global $meta_box, $post;
  80.  
  81. // Use nonce for verification
  82. echo '<input type="hidden" name="theme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  83.  
  84. echo '<table class="form-table">';
  85.  
  86. foreach ($meta_box['fields'] as $field) {
  87. // get current post meta data
  88. $meta = get_post_meta($post->ID, $field['id'], true);
  89. $title = get_post_meta($post->ID, '_title', true);
  90. echo '<tr>',
  91. '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
  92. '<td>';
  93. switch ($field['type']) {
  94. case 'text':
  95. echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
  96. '<br />', $field['desc'];
  97. break;
  98. case 'textarea':
  99. echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
  100. '<br />', $field['desc'];
  101. break;
  102. case 'select':
  103. echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  104. foreach ($field['options'] as $value => $name) {
  105. echo '<option value="'.$value.'" ';
  106. echo $meta == $value ? ' selected="selected"' : '', '>', $name, '</option>';
  107. }
  108. echo '</select>';
  109. break;
  110. case 'radio':
  111. foreach ($field['options'] as $option) {
  112. echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
  113. }
  114. break;
  115. case 'checkbox':
  116. echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
  117. break;
  118. }
  119. echo '</tr><tr><td>';
  120. echo '<i class="fa fa-2x '.$meta.'"></i>';
  121. echo 'Link:</td><td><input type="text" name="_title" value="' . $title . '" class="widefat"></td></tr>';
  122. }
  123.  
  124. echo '</table>';
  125. }
  126. add_action('save_post', 'theme_save_data');
  127. function theme_save_data($post_id) {
  128. global $meta_box;
  129.  
  130. // verify nonce
  131. if (!wp_verify_nonce($_POST['theme_meta_box_nonce'], basename(__FILE__))) {
  132. return $post_id;
  133. }
  134.  
  135. // check autosave
  136. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  137. return $post_id;
  138. }
  139.  
  140. // check permissions
  141. if ('page' == $_POST['post_type']) {
  142. if (!current_user_can('edit_page', $post_id)) {
  143. return $post_id;
  144. }
  145. } elseif (!current_user_can('edit_post', $post_id)) {
  146. return $post_id;
  147. }
  148.  
  149. foreach ($meta_box['fields'] as $field) {
  150. $old = get_post_meta($post_id, $field['id'], true);
  151. $new = $_POST[$field['id']];
  152.  
  153. if ($new && $new != $old) {
  154. update_post_meta($post_id, $field['id'], $new);
  155. } elseif ('' == $new && $old) {
  156. delete_post_meta($post_id, $field['id'], $old);
  157. }
  158. }
  159. $events_meta['_title'] = $_POST['_title'];
  160. foreach ($events_meta as $key => $value) { // Cycle through the $events_meta array!
  161. if( $post->post_type == 'revision' ) return; // Don't store custom data twice
  162. $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
  163. if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
  164. update_post_meta($post->ID, $key, $value);
  165. } else { // If the custom field doesn't have a value
  166. add_post_meta($post->ID, $key, $value);
  167. }
  168. if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
  169. }
  170. }
  171. $events_meta['_title'] = $_POST['_title'];
  172. function social_style(){
  173. $style_url = plugins_url('css/font-awesome.css', __FILE__);
  174. wp_enqueue_style( 'fontawesome', $style_url );
  175. ?>
  176.  
  177. <?php
  178. }
  179. add_action('wp_enqueue_scripts','social_style');
  180. add_action('admin_enqueue_scripts','social_style');
  181.  
  182. //theme template tag
  183. function social_icon(){
  184. global $prefix;
  185. global $post;
  186. $icon = get_post_meta($post->ID, $prefix.'icon', true);
  187. $link=get_post_meta($post->ID, '_title', true);
  188. if($icon && $link != ''): ?>
  189. <a href="<?php echo $link; ?> " class="fa <?php echo $icon; ?> mmc-fontawesome-meta-icon"></a>
  190. <?php endif;
  191. }
  192. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement