Advertisement
alchymyth

meta title custom fields

Aug 31st, 2013
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.67 KB | None | 0 0
  1. @@h@ this part goes into functions.php
  2.  
  3. // 'Custom Meta Title' field below post/page editor
  4. add_action('admin_menu', 'custom_meta_title');
  5. add_action('save_post', 'save_custom_meta_title');
  6. function custom_meta_title() {
  7. add_meta_box('custom_meta_title', 'Add meta title <small>(if left empty, blabla)</small>', 'custom_meta_title_input_function', 'post', 'normal', 'high');
  8. add_meta_box('custom_meta_title', 'Add meta title <small>(if left empty, blabla)</small>', 'custom_meta_title_input_function', 'page', 'normal', 'high');
  9. }
  10. function custom_meta_title_input_function() {
  11. global $post;
  12. echo '<input type="hidden" name="custom_meta_title_input_hidden" id="custom_meta_title_input_hidden" value="'.wp_create_nonce('custom-meta-title-nonce').'" />';
  13. echo '<input type="text" name="custom_meta_title_input" id="custom_meta_title_input" style="width:100%;" value="'.get_post_meta($post->ID,'_custom_meta_title',true).'" />';
  14. }
  15. function save_custom_meta_title($post_id) {
  16. if (!wp_verify_nonce($_POST['custom_meta_title_input_hidden'], 'custom-meta-title-nonce')) return $post_id;
  17. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
  18. $customMetatitle = $_POST['custom_meta_title_input'];
  19. update_post_meta($post_id, '_custom_meta_title', $customMetatitle);
  20. }
  21.  
  22. @@h@ this part goes into header.php to replace the <title>....</title> section
  23.  
  24. <title><?php
  25. if( is_single() || is_page() ) :
  26.     $text = get_post_meta($post->ID,'_custom_meta_title',true);
  27.     if( $text ) :
  28.     echo esc_attr( $text );
  29.     else :
  30.     wp_title( '|', true, 'right' ); //default if no custom field input
  31.     endif;
  32. else :
  33. wp_title( '|', true, 'right' ); //any other than single post or page
  34. endif; ?></title>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement