-
-
Save DavidPeralvarez/c3be8591c4f86ff2265a99d355efce3f to your computer and use it in GitHub Desktop.
Cómo utilizar los metadatos en tu sitio web
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> | |
<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> | |
<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> | |
<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'] ); | |
endif; | |
if( isset($_POST['dp_mbmd_option_2']) ): | |
update_post_meta( $post_id, '_dp_mbmd_option_2', $_POST['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; | |
} | |
// Definimos los estilos del Meta Box | |
add_action( 'admin_enqueue_scripts', 'dp_mbmd_admin_styles' ); | |
function dp_mbmd_admin_styles( $current_page ){ | |
// wp_die( $current_page ); | |
$dp_mbmd_load = array('post-new.php', 'post.php'); | |
if( in_array($current_page,$dp_mbmd_load) ): | |
wp_enqueue_style( 'dp_mbmd_meta_box_styles', plugins_url( '/css/meta-boxes.css' , __FILE__ ) ); | |
endif; | |
} | |
add_filter( 'the_content', 'dp_mbmd_rendering_metadata' ); | |
function dp_mbmd_rendering_metadata($content){ | |
global $post; | |
/* Nos aseguramos de que estamos en el detalle de un post y que estamos dentro | |
del bucle principal */ | |
if( is_singular('post') && in_the_loop() && is_main_query() ): | |
// $dp_mbmd_select = get_post_meta( $post->ID,'_dp_mbmd_songs', true ); | |
$dp_mbmd_select = $post->_dp_mbmd_songs; | |
if( isset($dp_mbmd_select) ): | |
$dp_mbmd_song = ''; | |
switch ($dp_mbmd_select) { | |
case 'cancion-1': | |
$dp_mbmd_song = 'Nombre canción 1'; | |
break; | |
case 'cancion-2': | |
$dp_mbmd_song = 'Nombre canción 2'; | |
break; | |
case 'cancion-3': | |
$dp_mbmd_song = 'Nombre canción 3'; | |
break; | |
default: | |
$dp_mbmd_song = 'No le gusta ninguna canción'; | |
break; | |
} | |
$content = '<p class="song">Canción favorita: ' . $dp_mbmd_song . '</p>' . $content; | |
endif; | |
endif; | |
return $content; | |
} |
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
#dp_mbmd_ej_1 label{ | |
display: block; | |
} | |
#dp_mbmd_ej_1 input[type="text"]{ | |
padding: 6px 8px; | |
} | |
#dp_mbmd_ej_1 input[type="text"], | |
#dp_mbmd_ej_1 textarea, | |
#dp_mbmd_ej_1 select{ | |
width: 100%; | |
border-radius: 4px; | |
border: 1px solid #8d96a0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment