top of page

Creating a custom Meta Box on Backend in Wordpress

  • Writer: سُلَيْمَان بْن دَاوُوْد
    سُلَيْمَان بْن دَاوُوْد
  • Apr 15, 2024
  • 1 min read
// register meta box
function meta_fields_add_meta_box(){
    add_meta_box(
        'meta_fields_meta_box', 
        __( '<span style="color:red;">Course Booking URL</span>', 'textdomain' ), 
        'meta_fields_build_meta_box_callback', 
        'page', 
        'side',
        'default',
        // hide the meta box in Gutenberg
        array('__back_compat_meta_box' => true)
    );
}
add_action( 'add_meta_boxes', 'meta_fields_add_meta_box' );

// build meta box content
function meta_fields_build_meta_box_callback( $post ){
    wp_nonce_field( 'meta_fields_save_meta_box_data', 'meta_fields_nonce' );

    $paymenturl = get_post_meta( $post->ID, '_meta_fields_payment_url', true );
    $courseid   = get_post_meta( $post->ID, '_meta_fields_course_id', true );


   ?>
   	<table id="courseurlandid">
	    <tr>
	        <td>
	            <label for="meta_fields_payment_url">Payment URL:</label>
	        </td>
	        <td>
	            <input type="text" id="meta_fields_payment_url" name="meta_fields_payment_url" value="<?php echo esc_attr( $paymenturl ); ?>" class="meta-field-input" />
	        </td>
	    </tr>
	    <tr>
	        <td>
	            <label for="meta_fields_course_id">Course ID:</label>
	        </td>
	        <td>
	            <input type="text" id="meta_fields_course_id" name="meta_fields_course_id" value="<?php echo esc_attr( $courseid ); ?>" class="meta-field-input" />
	        
	        </td> 
	    </tr>
	</table>

   <?php
}

// save meta box data
function meta_fields_save_meta_box_data( $post_id ){
    if ( ! isset( $_POST['meta_fields_payment_url'] ) )
        return;

    if ( ! isset( $_POST['meta_fields_course_id'] ) )
        return;

    if ( ! wp_verify_nonce( $_POST['meta_fields_nonce'], 'meta_fields_save_meta_box_data' ) )
        return;

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;

    if ( ! current_user_can( 'edit_post', $post_id ) )
        return;

	$paymenturl = sanitize_text_field( $_POST['meta_fields_payment_url'] );
	$courseid = sanitize_text_field( $_POST['meta_fields_course_id'] );

	$paymenturl_old = get_post_meta( $post_id, '_meta_fields_payment_url', true );
	$courseid_old = get_post_meta( $post_id, '_meta_fields_course_id', true );

	if (empty($paymenturl_old) || empty($courseid_old)) {
	    update_post_meta( $post_id, '_meta_fields_payment_url', $paymenturl );
	    update_post_meta( $post_id, '_meta_fields_course_id', $courseid );
	}

}
add_action( 'save_post', 'meta_fields_save_meta_box_data' );

A meta box gets created on a post type page in the side and shows 2 fields and values from those 2 fields would be saved in post meta once the page is saved

 
 
 

Recent Posts

See All
Duplicate Category in Wordpress

The following code helps to duplicate the default wordpress category by help of code. // Add Duplicate option for categories in WordPress...

 
 
 
C#.NET and Its Associated Codes

1- Creating an Desktop Application in C#.NET C# ADO.NET & MySQL MAIN C# and MySQL ADO.NET with Crystals Reports Ready Code 2- Handling...

 
 
 

Comments


© 2020 by syednazrulhassan

bottom of page