Advertisement
Guest User

Untitled

a guest
Jun 7th, 2013
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?
  2. if(!class_exists('catchyTitle')) :
  3.  
  4.     class catchyTitle
  5.     {
  6.         // Add meta box
  7.         public static function metaBox()
  8.         {            
  9.             $screen = get_current_screen();  
  10.             if('post' != $screen->post_type)
  11.                 return;
  12.  
  13.             add_meta_box('catchy_title', 'Short Title', array('catchyTitle', 'metaBoxShow'), 'post', 'normal', 'high');
  14.         }        
  15.  
  16.         // Show meta box
  17.         public static function metaBoxShow()
  18.         {
  19.             global $post;
  20.  
  21.             // Use nonce for verification
  22.             wp_nonce_field(plugin_basename( __FILE__ ), 'catchy_title_nonce');
  23.  
  24.             // Get current value
  25.             $meta = get_post_meta($post->ID, 'catchy_title', true);
  26.  
  27.             echo '            
  28.            <div id="titlewrap">
  29.                <label for="catchy_title" class="">Enter shorter title here</label>
  30.                <input type="text" autocomplete="off" id="catchy_title" value="'.$meta.'" size="20" name="catchy_title">
  31.            </div>';
  32.         }
  33.  
  34.         // Save the meta data on form post
  35.         public static function saveMeta($postId)
  36.         {
  37.             // Did the user intend to change this value
  38.             if(!isset($_POST['catchy_title_nonce']) || !wp_verify_nonce($_POST['catchy_title_nonce'], plugin_basename( __FILE__ )))
  39.                 return;
  40.  
  41.             // Check autosave
  42.             if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  43.                 return $postId;
  44.             }
  45.  
  46.             // Check permissions
  47.             if(!current_user_can('edit_post', $postId))
  48.                 return $postId;
  49.  
  50.             $old = get_post_meta($postId, 'catchy_title', true);
  51.             $new = $_POST['catchy_title'];
  52.  
  53.             if ($new && $new != $old) {
  54.                 update_post_meta($postId, 'catchy_title', $new);
  55.             } elseif ('' == $new && $old) {
  56.                 delete_post_meta($postId, 'catchy_title', $old);
  57.             }
  58.         }
  59.     }
  60.    
  61.     if(is_admin())
  62.     {
  63.         add_action('admin_head',  array('catchyTitle', 'metaBox'));
  64.         add_action('save_post', array('catchyTitle', 'saveMeta'));
  65.     }
  66.  
  67. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement