Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

entradas

class jrs_qhadi_mtb_eventos_entradas {

	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(
			'qhe_mtb_entradas',
			__( 'Tipo de entradas', 'quehaceradondeir' ),
			array( $this, 'render_metabox' ),
			'post',
			'advanced',
			'default'
		);

	}

	public function render_metabox( $post ) {

		// Add nonce for security and authentication.
		wp_nonce_field( 'qhadi_nonce_action', 'qhadi_nonce' );

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

		// Set default values.
		if( empty( $qhadi_radio_entrada ) ) $qhadi_radio_entrada = '';
		if( empty( $qhadi_ ) ) $qhadi_ = '';

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

		echo '	<tr>';
		echo '		<th><label for="qhadi_radio_entrada" class="qhadi_radio_entrada_label">' . __( 'Tipo de entrada', 'quehaceradondeir' ) . '</label></th>';
		echo '		<td>';
		echo '			<label><input type="radio" name="qhadi_radio_entrada" class="qhadi_radio_entrada_field" value="qhadi_ent-lib" ' . checked( $qhadi_radio_entrada, 'qhadi_ent-lib', false ) . '> ' . __( 'Entrada libre', 'quehaceradondeir' ) . '</label><br>';
		echo '			<label><input type="radio" name="qhadi_radio_entrada" class="qhadi_radio_entrada_field" value="qhadi_ent-pag" ' . checked( $qhadi_radio_entrada, 'qhadi_ent-pag', false ) . '> ' . __( 'Entrada de pago', 'quehaceradondeir' ) . '</label><br>';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="qhadi_" class="qhadi__label">' . __( '', 'quehaceradondeir' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="number" id="qhadi_" name="qhadi_" class="qhadi__field" placeholder="' . esc_attr__( '', 'quehaceradondeir' ) . '" value="' . esc_attr( $qhadi_ ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '</table>';

	}

	public function save_metabox( $post_id, $post ) {

		// Add nonce for security and authentication.
		$nonce_name   = isset( $_POST['qhadi_nonce'] ) ? $_POST['qhadi_nonce'] : '';
		$nonce_action = 'qhadi_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 the user has permissions to save data.
		if ( ! current_user_can( 'edit_post', $post_id ) )
			return;

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

		// Check if it's not a revision.
		if ( wp_is_post_revision( $post_id ) )
			return;

		// Sanitize user input.
		$qhadi_new_radio_entrada = isset( $_POST[ 'qhadi_radio_entrada' ] ) ? $_POST[ 'qhadi_radio_entrada' ] : '';
		$qhadi_new_ = isset( $_POST[ 'qhadi_' ] ) ? floatval( $_POST[ 'qhadi_' ] ) : '';

		// Update the meta field in the database.
		update_post_meta( $post_id, 'qhadi_radio_entrada', $qhadi_new_radio_entrada );
		update_post_meta( $post_id, 'qhadi_', $qhadi_new_ );

	}

}

new jrs_qhadi_mtb_eventos_entradas;