Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // to set a predefined input field for custom field from within functions.php
- // 'Custom Field Content' field below post editor
- add_action('admin_menu', 'custom_meta_input');
- add_action('save_post', 'save_custom_meta_input');
- function custom_meta_input() {
- add_meta_box('custom_meta_input', 'Custom Field Content', 'custom_meta_input_function', 'post', 'normal', 'high');
- // 'Custom Field Content' is the label above the input area for this custom field
- }
- function custom_meta_input_function() {
- global $post;
- echo '<input type="hidden" name="custom_meta_input_hidden" id="custom_meta_input_hidden" value="'.wp_create_nonce('custom-meta-input-nonce').'" />';
- 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).'" />';
- /* the custom field key is 'this_custom_meta_input';
- // i.e. whereever you need to use this custom filed in a template,
- // use <?php echo get_post_meta($post->ID,'this_custom_meta_input',true); ?>
- */
- }
- function save_custom_meta_input($post_id) {
- if (!wp_verify_nonce($_POST['custom_meta_input_hidden'], 'custom-meta-input-nonce')) return $post_id;
- if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
- $customMetaInput = $_POST['custom_meta_input'];
- update_post_meta($post_id, 'this_custom_meta_input', $customMetaInput);
- }
- ////END to set a predefined custom field from within functions.php//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement