Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

Untitled Snippet

class Rcrm_Breakdown_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(
			'breakdowns_meta',
			__( 'Informations', 'text_domain' ),
			array( $this, 'render_metabox' ),
			'breakdowns',
			'normal',
			'default'
		);

	}

	public function render_metabox( $post ) {

		// Retrieve an existing value from the database.
		$rcrm_meta_price = get_post_meta( $post->ID, 'rcrm_meta_price', true );
		$rcrm_meta_icon = get_post_meta( $post->ID, 'rcrm_meta_icon', true );

		// Set default values.
		if( empty( $rcrm_meta_price ) ) $rcrm_meta_price = 'Nous contacter';
		if( empty( $rcrm_meta_icon ) ) $rcrm_meta_icon = '';

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

		echo '	<tr>';
		echo '		<th><label for="rcrm_meta_price" class="rcrm_meta_price_label">' . __( 'Price', 'text_domain' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="rcrm_meta_price" name="rcrm_meta_price" class="rcrm_meta_price_field" placeholder="' . esc_attr__( 'Price', 'text_domain' ) . '" value="' . esc_attr( $rcrm_meta_price ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="rcrm_meta_icon" class="rcrm_meta_icon_label">' . __( 'Icon', 'text_domain' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="rcrm_meta_icon" name="rcrm_meta_icon" class="rcrm_meta_icon_field" placeholder="' . esc_attr__( 'Icon code', 'text_domain' ) . '" value="' . esc_attr( $rcrm_meta_icon ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '</table>';

	}

	public function save_metabox( $post_id, $post ) {

		// Sanitize user input.
		$rcrm_new_meta_price = isset( $_POST[ 'rcrm_meta_price' ] ) ? sanitize_text_field( $_POST[ 'rcrm_meta_price' ] ) : '';
		$rcrm_new_meta_icon = isset( $_POST[ 'rcrm_meta_icon' ] ) ? sanitize_text_field( $_POST[ 'rcrm_meta_icon' ] ) : '';

		// Update the meta field in the database.
		update_post_meta( $post_id, 'rcrm_meta_price', $rcrm_new_meta_price );
		update_post_meta( $post_id, 'rcrm_meta_icon', $rcrm_new_meta_icon );

	}

}

new Rcrm_Breakdown_Meta_Box;