Advertisement
alchymyth

source custom field

May 21st, 2013
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.47 KB | None | 0 0
  1. // to set a predefined input field for custom field from within functions.php
  2. // 'Source' 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', 'Source', 'custom_meta_input_function', 'post', 'normal', 'high');
  7.     // 'Source' 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 '<span>Input Source: </span><input style="width: 100%;" type="text" name="custom_meta_input_value" id="custom_meta_input_value" value="'.get_post_meta($post->ID,'_source_custom_meta_input_value',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,'_source_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.     $customMetaInputValue = $_POST['custom_meta_input_value'];
  22.     update_post_meta($post_id, '_source_custom_meta_input_value', $customMetaInputValue);
  23. }
  24. ////END to set a predefined custom field from within functions.php//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement