/meta-boxes-metadata.php Secret
Last active
November 29, 2019 13:29
Star
You must be signed in to star a gist
Añadimos más campos (radio, checkbox, select, textarea)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Meta Boxes y Metadata | |
Description: Aprendiendo a usar Meta Boxes y Metadata. | |
*/ | |
add_action( 'add_meta_boxes', 'dp_mbmd_register_meta_boxes' ); | |
function dp_mbmd_register_meta_boxes(){ | |
add_meta_box( 'dp_mbmd_ej_1', 'Título del meta box', 'dp_mbmd_ej_1_display', 'post', 'advanced', 'low'); | |
} | |
// Añadimos los campos de formulario a nuestro meta box | |
function dp_mbmd_ej_1_display( $post ){ | |
//obtenemos los metadata de este post | |
$dp_mbmd_metadata = get_post_meta( $post->ID ); | |
?> | |
<!-- text --> | |
<p> | |
<label for="dp_mbmd_dino">Tu dinosaurio favorito:</label> | |
<input type="text" name="dp_mbmd_dino" id="dp_mbmd_dino" value="<?php if( isset( $dp_mbmd_metadata['_dp_mbmd_dino'] ) ) echo $dp_mbmd_metadata['_dp_mbmd_dino'][0]; ?>"> | |
</p> | |
<!-- checkbox --> | |
<p> | |
<label for="dp_mbmd_option_1"> | |
<input type="checkbox" name="dp_mbmd_option_1" id="dp_mbmd_option_1" value="yes" <?php if( isset( $dp_mbmd_metadata['_dp_mbmd_option_1'] ) ) checked($dp_mbmd_metadata['_dp_mbmd_option_1'][0], 'yes'); ?>> | |
Opción 1</label> | |
<br> | |
<label for="dp_mbmd_option_2"> | |
<input type="checkbox" name="dp_mbmd_option_2" id="dp_mbmd_option_2" value="yes" <?php if( isset( $dp_mbmd_metadata['_dp_mbmd_option_2'] ) ) checked($dp_mbmd_metadata['_dp_mbmd_option_2'][0], 'yes'); ?>> | |
Opción 2</label> | |
</p> | |
<!-- radio --> | |
<p> | |
<label for="dp_mbmd_color_1"> | |
<input type="radio" name="dp_mbmd_color" id="dp_mbmd_color_1" value="color-1" <?php if( isset( $dp_mbmd_metadata['_dp_mbmd_color'] ) ) checked($dp_mbmd_metadata['_dp_mbmd_color'][0], 'color-1'); ?>> | |
Color 1</label> | |
<br> | |
<label for="dp_mbmd_color_2"> | |
<input type="radio" name="dp_mbmd_color" id="dp_mbmd_color_2" value="color-2" <?php if( isset( $dp_mbmd_metadata['_dp_mbmd_color'] ) ) checked($dp_mbmd_metadata['_dp_mbmd_color'][0], 'color-2'); ?>> | |
Color 2</label> | |
</p> | |
<!-- select --> | |
<p> | |
<label for="dp_mbmd_songs">Tu cación favorita:</label> | |
<select name="dp_mbmd_songs" id="dp_mbmd_songs"> | |
<option value="0">-- elige canción</option> | |
<option value="cancion-1" <?php if( isset( $dp_mbmd_metadata['_dp_mbmd_songs'] ) ) selected($dp_mbmd_metadata['_dp_mbmd_songs'][0], 'cancion-1'); ?>>Nombre canción 1</option> | |
<option value="cancion-2" <?php if( isset( $dp_mbmd_metadata['_dp_mbmd_songs'] ) ) selected($dp_mbmd_metadata['_dp_mbmd_songs'][0], 'cancion-2'); ?>>Nombre canción 2</option> | |
<option value="cancion-3" <?php if( isset( $dp_mbmd_metadata['_dp_mbmd_songs'] ) ) selected($dp_mbmd_metadata['_dp_mbmd_songs'][0], 'cancion-3'); ?>>Nombre canción 3</option> | |
</select> | |
</p> | |
<!-- textarea --> | |
<p> | |
<label for="dp_mbmd_message">Cuéntame tu problema:</label> | |
<br> | |
<textarea name="dp_mbmd_message" id="dp_mbmd_message" rows="8" cols="80"><?php if( isset( $dp_mbmd_metadata['_dp_mbmd_message'] ) ) echo $dp_mbmd_metadata['_dp_mbmd_message'][0]; ?></textarea> | |
</p> | |
<?php | |
} | |
// Guardamos los meta datos | |
add_action( 'save_post', 'dp_mbmd_save_meta' ); | |
function dp_mbmd_save_meta( $post_id ){ | |
// comprobamos si hay datos en el campo del formulario | |
if( isset($_POST['dp_mbmd_dino']) ): | |
// añadirlo o modificar el metadata | |
update_post_meta( $post_id, '_dp_mbmd_dino', $_POST['dp_mbmd_dino'] ); | |
endif; | |
if( isset($_POST['dp_mbmd_option_1']) ): | |
update_post_meta( $post_id, '_dp_mbmd_option_1', $_POST['dp_mbmd_option_1'] ); | |
else: | |
delete_post_meta( $post_id, '_dp_mbmd_option_1' ); | |
endif; | |
if( isset($_POST['dp_mbmd_option_2']) ): | |
update_post_meta( $post_id, '_dp_mbmd_option_2', $_POST['dp_mbmd_option_2'] ); | |
else: | |
delete_post_meta( $post_id, '_dp_mbmd_option_2' ); | |
endif; | |
if( isset($_POST['dp_mbmd_color']) ): | |
update_post_meta( $post_id, '_dp_mbmd_color', $_POST['dp_mbmd_color'] ); | |
endif; | |
if( isset($_POST['dp_mbmd_songs']) ): | |
update_post_meta( $post_id, '_dp_mbmd_songs', $_POST['dp_mbmd_songs'] ); | |
endif; | |
if( isset($_POST['dp_mbmd_message']) ): | |
update_post_meta( $post_id, '_dp_mbmd_message', $_POST['dp_mbmd_message'] ); | |
endif; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment