Custom metabox/fields

<?php
/* Creating meta box that contains custom fields and creating custom fileds*/
function rejauldu_add_custom_metabox() {
 add_meta_box(
 'rejauldu_meta', /* Meta box ID (used in the 'id' attribute for the meta box).*/
 'Job Listing', /*Title of the meta box.*/
 'rejauldu_meta_callback', /*Function that fills the box with the desired content. The function should echo its output.*/
 'rejauldu', /*(Optional)The screen or screens on which to show the box (such as a post type, 'link', or 'comment'). Accepts a single screen ID, WP_Screen object, or array of screen IDs. Default is the current screen. If you have used add_menu_page() or add_submenu_page() to create a new screen (and hence screen_id), make sure your menu slug conforms to the limits of sanitize_key() otherwise the 'screen' menu may not correctly render on your page. Default value: null*/
 'normal', /*(Optional) The context within the screen where the boxes should display. Available contexts vary from screen to screen. Post edit screen contexts include 'normal', 'side', and 'advanced'. Comments screen contexts include 'normal' and 'side'. Menus meta boxes (accordion sections) all use the 'side' context. Global. Default value: 'advanced'*/
 'core' /*(Optional) The priority within the context where the boxes should show ('high', 'low'). Default value: 'default'*/
 );
}
add_action('add_meta_boxes', 'rejauldu_add_custom_metabox'); /* add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 ); $tag is the action or moment when to call; $priority-If the same action or $tag is used anywhere else then functions will be called according to their $priority; $accepted_args will be the no parameters in $function_to_add*/


function rejauldu_meta_callback($post) {
 wp_nonce_field(basename(__FILE__), 'rejauldu_jobs_nonce'); /* nonce field to make sure that data coming from your wp form*/
 /*get_post_meta returns data from wp_postmeta table of database(post_id, key, value)*/
 $rejauldu_stored_meta = get_post_meta($post->ID); /*get_post_meta( int $post_id, string $key = '', bool $single = false )/ returns 1-D array like ['key1'=>'value1', 'key2'=>'value2'] of a specific post id*/
 ?>
 <div>
 <div class="meta-row">
 <div class="meta-th">
 <label for="job-id" class="rejauldu-row-title">Job ID</label>
 </div>
 <div class="meta-td">
 <input type="text" name="job_id" id="job-id" value="<?php
 /* esc_attr sanitizes data coming from database*/
 if(!empty($rejauldu_stored_meta['job_id'])) echo esc_attr($rejauldu_stored_meta['job_id'][0]);
 ?>" />
 </div>
 </div>
 </div>
 <div class="meta">
 <div class="meta-th">
 <span>Principle Duties</span>
 </div>
 </div>
 <div class="meta-editor"></div>
 <?php
 $content = get_post_meta($post->ID, 'principle_duties', true); /* get_post_meta( int $post_id, string $key = '', bool $single = false ); ruturns a single value*/
 $editor_id = 'principle_duties';
 $settings = array(
 'textarea_rows' => 8,
 'meta_buttons' => false
 );
 wp_editor($content, $editor_id, $settings); /*This function renders an editor in a page in the typical fashion used in Posts and Pages.*/
}

function rejauldu_meta_save($post_id) {
 //Checks save status
 $is_autosave = wp_is_post_autosave($post_id);
 $is_revision = wp_is_post_revision($post_id);
 $is_valid_nonce = ( isset($_POST['rejauldu_jobs_nonce']) && wp_verify_nonce($_POST['rejauldu_jobs_nonce']));
 
 //Exits script depending on save status
 if( $is_autosave || $is_revision || $is_valid_nonce) {
 return;
 }
 if(isset($_POST['job_id'])) {
 /* sanitize_text_field sanitizes data coming from user input*/
 update_post_meta($post_id, 'job_id', sanitize_text_field($_POST['job_id']));
 }
}
add_action('save_post', 'rejauldu_meta_save', 10, 3);

Labels: , ,

© copyright-2020 Rejaul