/**
* Generated by the WordPress Meta Box generator
* at http://jeremyhixon.com/tool/wordpress-meta-box-generator/
*/
function my_title_get_meta( $value ) {
global $post;
$field = get_post_meta( $post->ID, $value, true );
if ( ! empty( $field ) ) {
return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
} else {
return false;
}
}
function my_title_add_meta_box() {
add_meta_box(
'my_title-my-title',
__( 'My title', 'my_title' ),
'my_title_html',
'post',
'normal',
'default'
);
}
add_action( 'add_meta_boxes', 'my_title_add_meta_box' );
function my_title_html( $post) {
wp_nonce_field( '_my_title_nonce', 'my_title_nonce' ); ?>
<p>My description</p>
<p>
<label for="my_title_my_label"><?php _e( 'My label', 'my_title' ); ?></label><br>
<input type="text" name="my_title_my_label" id="my_title_my_label" value="<?php echo my_title_get_meta( 'my_title_my_label' ); ?>">
</p><?php
}
function my_title_save( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( ! isset( $_POST['my_title_nonce'] ) || ! wp_verify_nonce( $_POST['my_title_nonce'], '_my_title_nonce' ) ) return;
if ( ! current_user_can( 'edit_post', $post_id ) ) return;
if ( isset( $_POST['my_title_my_label'] ) )
update_post_meta( $post_id, 'my_title_my_label', esc_attr( $_POST['my_title_my_label'] ) );
}
add_action( 'save_post', 'my_title_save' );
/*
Usage: my_title_get_meta( 'my_title_my_label' )
*/
<?php
function add_user_metabox( $user ) {
?>
<h3>Section title</h3>
<table class="form-table">
<tr>
<th><label for="field_name">Field Label</label></th>
<td><input type="text" name="field_name" value="<?php echo esc_attr(get_the_author_meta( 'field_name', $user->ID )); ?>" class="regular-text" /></td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'add_user_metabox' );
add_action( 'edit_user_profile', 'add_user_metabox' );
function save_user_metabox( $user_id ) {
update_user_meta( $user_id, 'field_name', sanitize_text_field( $_POST['field_name'] ) );
}
add_action( 'personal_options_update', 'save_user_metabox' );
add_action( 'edit_user_profile_update', 'save_user_metabox' );