Skip to content

Instantly share code, notes, and snippets.

@alispx
Created January 7, 2016 19:58
Show Gist options
  • Save alispx/92ebf1e771314af42fe9 to your computer and use it in GitHub Desktop.
Save alispx/92ebf1e771314af42fe9 to your computer and use it in GitHub Desktop.
add_action('add_meta_boxes', 'alispx_meta_boxes');
function alispx_meta_boxes() {
add_meta_box(
'alispx-meta-box',
__( 'Phone Model', THEMO_TEXT_DOMAIN ),
'alispx_meta_box_callback',
'page'
);
}
function alispx_meta_box_callback( $post ) {
wp_nonce_field( 'alispx_meta_box', 'alispx_meta_box_noncename' );
$post_meta = get_post_meta( $post->ID, 's4root_phone_model', true );
$current_value = '';
if ( ! empty( $post_meta ) ) {
$current_value = $post_meta;
}
?>
<div class="form-wrap">
<div class="form-field" style="clear:both;">
<label class="label" for="s4root_phone_model">Phone Model</label>
<input name="s4root_phone_model" id="s4root_phone_model" type="text" value="<?php echo esc_attr( $current_value ); ?>">
<p>Enter the phone model</p>
</div>
</div>
<?php
}
add_action( 'save_post', 'alispx_save_custom_fields', 10, 2 );
function alispx_save_custom_fields( $post_id, $post ) {
if ( 'page' != $post->post_type || ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( !isset( $_POST['alispx_meta_box_noncename'] ) || ! wp_verify_nonce( $_POST['alispx_meta_box_noncename'], 'alispx_meta_box' ) ) {
return;
}
if( isset($_POST['s4root_phone_model']) && $_POST['s4root_phone_model'] != "" ) {
update_post_meta( $post_id, 's4root_phone_model', sanitize_text_field( $_POST['s4root_phone_model'] ) );
} else {
if ( isset( $post_id ) ) {
delete_post_meta($post_id, 's4root_phone_model');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment