Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

Products Features Meta Box

class Features_Meta_Box {

	public function __construct() {

		if ( is_admin() ) {
			add_action( 'load-post.php',     array( $this, 'init_metabox' ) );
			add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
		}

	}

	public function init_metabox() {

		add_action( 'add_meta_boxes',        array( $this, 'add_metabox' )         );
		add_action( 'save_post',             array( $this, 'save_metabox' ), 10, 2 );

	}

	public function add_metabox() {

		add_meta_box(
			'product_features',
			__( 'Características', 'lanton' ),
			array( $this, 'render_features' ),
			'products',
			'normal',
			'default'
		);

	}

	public function render_features( $post ) {

		// Add nonce for security and authentication.
		wp_nonce_field( 'features_nonce_action', 'features_nonce' );

		// Retrieve an existing value from the database.
		$features_type = get_post_meta( $post->ID, 'features_type', true );
		$features_material = get_post_meta( $post->ID, 'features_material', true );
		$features_yarn_shape = get_post_meta( $post->ID, 'features_yarn_shape', true );
		$features_pile_height = get_post_meta( $post->ID, 'features_pile_height', true );
		$features_dtex = get_post_meta( $post->ID, 'features_dtex', true );
		$features_gauge = get_post_meta( $post->ID, 'features_gauge', true );
		$features_stitch = get_post_meta( $post->ID, 'features_stitch', true );
		$features_density = get_post_meta( $post->ID, 'features_density', true );

		// Set default values.
		if( empty( $features_type ) ) $features_type = '';
		if( empty( $features_material ) ) $features_material = '';
		if( empty( $features_yarn_shape ) ) $features_yarn_shape = '';
		if( empty( $features_pile_height ) ) $features_pile_height = '';
		if( empty( $features_dtex ) ) $features_dtex = '';
		if( empty( $features_gauge ) ) $features_gauge = '';
		if( empty( $features_stitch ) ) $features_stitch = '';
		if( empty( $features_density ) ) $features_density = '';

		// Form fields.
		echo '<table class="form-table">';

		echo '	<tr>';
		echo '		<th><label for="features_type" class="features_type_label">' . __( 'Type', 'lanton' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="features_type" name="features_type" class="features_type_field" placeholder="' . esc_attr__( 'Ej: Mint', 'lanton' ) . '" value="' . esc_attr( $features_type ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="features_material" class="features_material_label">' . __( 'Material', 'lanton' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="features_material" name="features_material" class="features_material_field" placeholder="' . esc_attr__( 'Ej: PP+PE', 'lanton' ) . '" value="' . esc_attr( $features_material ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="features_yarn_shape" class="features_yarn_shape_label">' . __( 'Yarn Shape', 'lanton' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="features_yarn_shape" name="features_yarn_shape" class="features_yarn_shape_field" placeholder="' . esc_attr__( 'Ej: spine', 'lanton' ) . '" value="' . esc_attr( $features_yarn_shape ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="features_pile_height" class="features_pile_height_label">' . __( 'Pile Height', 'lanton' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="features_pile_height" name="features_pile_height" class="features_pile_height_field" placeholder="' . esc_attr__( 'Ej: 20mm', 'lanton' ) . '" value="' . esc_attr( $features_pile_height ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="features_dtex" class="features_dtex_label">' . __( 'Dtex', 'lanton' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="features_dtex" name="features_dtex" class="features_dtex_field" placeholder="' . esc_attr__( 'Ej: 11000', 'lanton' ) . '" value="' . esc_attr( $features_dtex ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="features_gauge" class="features_gauge_label">' . __( 'Gauge', 'lanton' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="features_gauge" name="features_gauge" class="features_gauge_field" placeholder="' . esc_attr__( 'Ej: 3/8', 'lanton' ) . '" value="' . esc_attr( $features_gauge ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="features_stitch" class="features_stitch_label">' . __( 'Stitch', 'lanton' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="features_stitch" name="features_stitch" class="features_stitch_field" placeholder="' . esc_attr__( 'Ej: 200', 'lanton' ) . '" value="' . esc_attr( $features_stitch ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="features_density" class="features_density_label">' . __( 'Density', 'lanton' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="features_density" name="features_density" class="features_density_field" placeholder="' . esc_attr__( 'Ej: 21000', 'lanton' ) . '" value="' . esc_attr( $features_density ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '</table>';

	}

	public function save_metabox( $post_id, $post ) {

		// Add nonce for security and authentication.
		$nonce_name   = isset( $_POST['features_nonce'] ) ? $_POST['features_nonce'] : '';
		$nonce_action = 'features_nonce_action';

		// Check if a nonce is set.
		if ( ! isset( $nonce_name ) )
			return;

		// Check if a nonce is valid.
		if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) )
			return;

		// Check if it's not an autosave.
		if ( wp_is_post_autosave( $post_id ) )
			return;

		// Sanitize user input.
		$features_new_type = isset( $_POST[ 'features_type' ] ) ? sanitize_text_field( $_POST[ 'features_type' ] ) : '';
		$features_new_material = isset( $_POST[ 'features_material' ] ) ? sanitize_text_field( $_POST[ 'features_material' ] ) : '';
		$features_new_yarn_shape = isset( $_POST[ 'features_yarn_shape' ] ) ? sanitize_text_field( $_POST[ 'features_yarn_shape' ] ) : '';
		$features_new_pile_height = isset( $_POST[ 'features_pile_height' ] ) ? sanitize_text_field( $_POST[ 'features_pile_height' ] ) : '';
		$features_new_dtex = isset( $_POST[ 'features_dtex' ] ) ? sanitize_text_field( $_POST[ 'features_dtex' ] ) : '';
		$features_new_gauge = isset( $_POST[ 'features_gauge' ] ) ? sanitize_text_field( $_POST[ 'features_gauge' ] ) : '';
		$features_new_stitch = isset( $_POST[ 'features_stitch' ] ) ? sanitize_text_field( $_POST[ 'features_stitch' ] ) : '';
		$features_new_density = isset( $_POST[ 'features_density' ] ) ? sanitize_text_field( $_POST[ 'features_density' ] ) : '';

		// Update the meta field in the database.
		update_post_meta( $post_id, 'features_type', $features_new_type );
		update_post_meta( $post_id, 'features_material', $features_new_material );
		update_post_meta( $post_id, 'features_yarn_shape', $features_new_yarn_shape );
		update_post_meta( $post_id, 'features_pile_height', $features_new_pile_height );
		update_post_meta( $post_id, 'features_dtex', $features_new_dtex );
		update_post_meta( $post_id, 'features_gauge', $features_new_gauge );
		update_post_meta( $post_id, 'features_stitch', $features_new_stitch );
		update_post_meta( $post_id, 'features_density', $features_new_density );

	}

}

new Features_Meta_Box;