Admin Meta Box

Home » Snippets » Admin Meta Box
0

Created with:

Meta Box Generator

Visibility: 

public

Creator: Mikhail

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
				/**
 * The WPTurbo Custom Meta Box class.
 *
 * This class is responsible for rendering a custom meta box in WordPress with user-defined fields/options.
 * It uses the WordPress add_meta_box() and update_post_meta() functions to render and save field values.
 *
 * @link              https://wpturbo.ai
 * @since             1.0.0
 * @package           WPTurbo
 * 
 */
class ShortStayPropertyMetaBox {

	/**
	 * Array that defines display locations.
	 *
	 * @since 1.0.0
	 * @access private
	 * @var array $display_locations The list of locations where this meta box should be displayed.
	 */
	private $display_locations = [
		'post',
	];
	
	/**
	 * Variables array that defines fields/options for the meta box.
	 *
	 * @since 1.0.0
	 * @access private
	 * @var array $fields The list of user defined fields/options.
	 */
	private $fields = [
		'property_title' => [
			'type' => 'text',
			'label' => 'Property Title',
			'default' => '',
		],
		'property_description' => [
			'type' => 'text',
			'label' => 'Property Description',
			'default' => '',
		],
		'booking_start_date' => [
			'type' => 'date',
			'label' => 'Booking Start Date',
			'default' => '',
		],
		'booking_end_date' => [
			'type' => 'date',
			'label' => 'Booking End Date',
			'default' => '',
		],
		'property_price' => [
			'type' => 'number',
			'label' => 'Property Price',
			'default' => '0',
		],
		'property_available' => [
			'type' => 'select',
			'label' => 'Property Availability',
			'default' => '0',
		],
	];
	
	/**
	 * WPTurbo_Custom_Meta_Box constructor.
	 *
	 * Adds actions to WordPress hooks "add_meta_boxes" and "save_post".
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		add_action( 'add_meta_boxes', [ $this, 'wpturbo_register_meta_boxes' ] );
		add_action( 'save_post', [ $this, 'wpturbo_save_meta_box_fields' ] );
	}
	
	/**
	 * Adds meta boxes to appropriate WordPress screens.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return void
	 */
	public function wpturbo_register_meta_boxes() : void {
		foreach ( $this->display_locations as $location ) {
			add_meta_box(
				'short_stay_property_meta_box', /* The id of our meta box. */
				'Property Details', /* The title of our meta box. */
				[ $this, 'wpturbo_render_meta_box_fields' ], /* The callback function that renders the metabox. */
				$location, /* The screen on which to show the box. */
				'normal', /* The placement of our meta box. */
				'high', /* The priority of our meta box. */
			);
		}
	}
	
	/**
	 * Renders the Meta Box and its fields.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @param WP_Post $post The post object.
	 *
	 * @return void
	 */
	public function wpturbo_render_meta_box_fields(WP_Post $post) : void {
		wp_nonce_field( 'short_stay_property_meta_box_data', 'short_stay_property_meta_box_nonce' );
		echo '<h3>Property Details</h3>';
		$html = '';
		foreach( $this->fields as $field_id => $field ){
			$meta_value = get_post_meta( $post->ID, $field_id, true );
			if ( empty( $meta_value ) && isset( $field['default'] ) ) {
				$meta_value = $field['default'];
			}
	
			$field_html = $this->wpturbo_render_input_field( $field_id, $field, $meta_value );
			$label = "<label for='$field_id'>{$field['label']}</label>";
			$html .= $this->wpturbo_format_field( $label, $field_html );
		}
		echo '<table class="form-table"><tbody>' . $html . '</tbody></table>';
	}
	
	/**
	 * Formats each field to table display.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @param string $label The field label.
	 * @param string $field The field HTML code.
	 *
	 * @return string
	 */
	public function wpturbo_format_field( string $label, string $field ): string {
		return '<tr class="form-field"><th>' . $label . '</th><td>' . $field . '</td></tr>';
	}
	
	/**
	 * Renders each individual field HTML code.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @param string $field_id The field ID.
	 * @param array $field The field configuration array.
	 * @param string $field_value The field value.
	 *
	 * @return string The HTML code.
	 */
	public function wpturbo_render_input_field( string $field_id, array $field, string $field_value): string {
		switch( $field['type'] ){
			case 'select': {
				$field_html = '<select name="'.$field_id.'" id="'.$field_id.'">';
					foreach( $field['options'] as $key => $value ) {
						$key = !is_numeric( $key ) ? $key : $value;
						$selected = '';
						if( $field_value === $key ) {
							$selected = 'selected="selected"';
						}
						$field_html .= '<option value="' . $key . '" ' . $selected . '>' . $value . '</option>';
					}
				$field_html .= '</select>';
				break;
			}
			case 'textarea': {
				$field_html = '<textarea name="' . $field_id . '" id="' . $field_id . '" rows="6">' . $field_value . '</textarea>';
				break;
			}
			default: {
				$field_html = "<input type='{$field['type']}' id='$field_id' name='$field_id' value='$field_value' />";
				break;
			}
		}
	
		return $field_html;
	}
	
	/**
	 * Called when this metabox is saved.
	 *
	 * Saves the new meta values of our metabox.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @param int $post_id The post ID.
	 *
	 * @return int The post ID.
	 */
	public function wpturbo_save_meta_box_fields( int $post_id ) {
		if ( ! isset( $_POST['short_stay_property_meta_box_nonce'] ) ) return;
	
		$nonce = $_POST['short_stay_property_meta_box_nonce'];
		if ( !wp_verify_nonce( $nonce, 'short_stay_property_meta_box_data' ) ) return;
	
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
	
		foreach ( $this->fields as $field_id => $field ) {
			if( isset( $_POST[$field_id] ) ){
				// Sanitize fields that need to be sanitized.
				switch( $field['type'] ) {
					case 'email': {
						$_POST[$field_id] = sanitize_email( $_POST[$field_id] );
						break;
					}
					case 'text': {
						$_POST[$field_id] = sanitize_text_field( $_POST[$field_id] );
						break;
					}
				}
				update_post_meta( $post_id, $field_id, $_POST[$field_id] );
			}
		}
	}
	
}

if ( class_exists( 'ShortStayPropertyMetaBox' ) ) {
	new ShortStayPropertyMetaBox();
}
			

Render Meta Box function

Register an account to save your snippets or go Pro to get more features.