Community Forums › Forums › Archived Forums › Design Tips and Tricks › Custom Field in post editor
Tagged: advanced custom field, funtion.php, genesis, Wordpress
- This topic has 2 replies, 2 voices, and was last updated 10 years ago by viorelepuran.
-
AuthorPosts
-
April 16, 2014 at 11:56 pm #100904viorelepuranMember
Hello!
I want to add a custom field , that will be visible in post editor, just below the field for post`s title.
I know about the Advanced Custom Filed plugin, i have used that with success, but now i want to get that same functionality without using a plugin, i need to do this throw theme`s funtion.php.So basicly, i need a text box below the box for the post title, and i need to know what will the meta key be named, so i can pull it in side posts.
Thank you for reading and trying to help me!
April 17, 2014 at 2:54 am #100916Brad DaltonParticipantYou can hand code this and hook it in the exact position you want.
April 17, 2014 at 3:02 am #100918viorelepuranMemberThank you Brad, i googled it, and i added a custom meta box, with a text field. now i can pull the value of that field, and i did it all in my function.php.
This is the code i used:/ Little function to return a custom field value
function wpshed_get_custom_field( $value ) {
global $post;$custom_field = get_post_meta( $post->ID, $value, true );
if ( !empty( $custom_field ) )
return is_array( $custom_field ) ? stripslashes_deep( $custom_field ) : stripslashes( wp_kses_decode_entities( $custom_field ) );return false;
}// Register the Metabox
function wpshed_add_custom_meta_box() {
add_meta_box( 'wpshed-meta-box', __( 'Ebay listing', 'textdomain' ), 'wpshed_meta_box_output', 'post', 'side', 'high' );
}
add_action( 'add_meta_boxes', 'wpshed_add_custom_meta_box' );// Output the Metabox
function wpshed_meta_box_output( $post ) {
// create a nonce field
wp_nonce_field( 'my_wpshed_meta_box_nonce', 'wpshed_meta_box_nonce' ); ?><p>
<label for="wpshed_textfield"><?php _e( 'Keyword', 'textdomain' ); ?>:</label>
<input type="text" name="wpshed_textfield" id="wpshed_textfield" value="<?php echo wpshed_get_custom_field( 'wpshed_textfield' ); ?>"/>
</p><?php
}// Save the Metabox values
function wpshed_meta_box_save( $post_id ) {
// Stop the script when doing autosave
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;// Verify the nonce. If insn't there, stop the script
if( !isset( $_POST['wpshed_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['wpshed_meta_box_nonce'], 'my_wpshed_meta_box_nonce' ) ) return;// Stop the script if the user does not have edit permissions
if( !current_user_can( 'edit_post' ) ) return;// Save the textfield
if( isset( $_POST['wpshed_textfield'] ) )
update_post_meta( $post_id, 'wpshed_textfield', esc_attr( $_POST['wpshed_textfield'] ) );}
add_action( 'save_post', 'wpshed_meta_box_save' ); -
AuthorPosts
- The topic ‘Custom Field in post editor’ is closed to new replies.