Advertisement
alchymyth

special custom field

Mar 7th, 2012
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.43 KB | None | 0 0
  1. // to set a predefined input field for custom field from within functions.php
  2. // 'Custom Field Content' field below post editor
  3. add_action('admin_menu', 'custom_meta_input');
  4. add_action('save_post', 'save_custom_meta_input');
  5. function custom_meta_input() {
  6.     add_meta_box('custom_meta_input', 'Custom Field Content', 'custom_meta_input_function', 'post', 'normal', 'high');
  7.     // 'Custom Field Content' is the label above the input area for this custom field
  8. }
  9. function custom_meta_input_function() {
  10.     global $post;
  11.     echo '<input type="hidden" name="custom_meta_input_hidden" id="custom_meta_input_hidden" value="'.wp_create_nonce('custom-meta-input-nonce').'" />';
  12.     echo '<input type="text" name="custom_meta_input" id="custom_meta_input" style="width:100%;" value="'.get_post_meta($post->ID,'this_custom_meta_input',true).'" />';
  13.     /* the custom field key is 'this_custom_meta_input';
  14.     // i.e. whereever you need to use this custom filed in a template,
  15.     // use <?php echo get_post_meta($post->ID,'this_custom_meta_input',true); ?>
  16.     */
  17. }
  18. function save_custom_meta_input($post_id) {
  19.     if (!wp_verify_nonce($_POST['custom_meta_input_hidden'], 'custom-meta-input-nonce')) return $post_id;
  20.     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
  21.     $customMetaInput = $_POST['custom_meta_input'];
  22.     update_post_meta($post_id, 'this_custom_meta_input', $customMetaInput);
  23. }
  24. ////END to set a predefined custom field from within functions.php//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement