Last active
August 5, 2017 02:05
-
-
Save deryckoe/3ed53f479b42322f38a3d7dceed06dab to your computer and use it in GitHub Desktop.
Cómo añadir campos personalizados a un Custom Post en WordPress
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 | |
function wpe_custom_post_init() { | |
$args = array( | |
'public' => true, | |
'label' => 'Libros' | |
); | |
register_post_type( 'book', $args ); | |
} | |
add_action( 'init', 'wpe_custom_post_init' ); |
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 | |
/* | |
* Registra el Meta Box asociado a 'book' | |
*/ | |
function wpe_add_mb() { | |
add_meta_box( | |
'seo-mb', | |
'SEO', // Titulo del Meta Box | |
'wpe_render_mb', // define funcion que imprime el contenido del Meta box | |
'book', // Custom Post al que esta asociada | |
'advanced', | |
'default' | |
); | |
} | |
add_action( 'add_meta_boxes', 'wpe_add_mb' ); | |
/* | |
* Imprime el contenido del Meta box | |
*/ | |
function wpe_render_mb( $post ) { | |
// Utilizamos nonce por seguridad | |
wp_nonce_field( 'wpe_keywords_nonce_save', 'wpe_keywords_nonce' ); | |
// Obtenemos los valores desde la base de datos | |
$meta_keywords = get_post_meta( $post->ID, '_keywords', true ); | |
$meta_description = get_post_meta( $post->ID, '_description', true ); | |
// El codigo html con los campos de texto y los valores | |
$html = '<table width="100%"> | |
<tr> | |
<td style="width: 20%" valign="top"><label for="">Palabras clave</label></td> | |
<td> | |
<input name="keywords" value="' . esc_attr( $meta_keywords ) . '" style="width: 100%;"> | |
</td> | |
</tr> | |
<tr> | |
<td style="width: 20%" valign="top"><label for="">Descripción</label></td> | |
<td> | |
<textarea name="description" style="width: 100%; height: 100px;">' . esc_attr( $meta_description ) . '</textarea> | |
</td> | |
</tr> | |
</table>'; | |
echo $html; | |
} |
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 | |
/* | |
* Revisa, sanea y guarda los valores del Meta Box en la base de datos. | |
*/ | |
function wpe_save_mb( $post_id ) { | |
// Utilizamos nonce por seguridad. Debe venir correctamente del Meta Box. | |
$nonce_name = isset( $_POST['wpe_keywords_nonce'] ) ? $_POST['wpe_keywords_nonce'] : ''; | |
$nonce_action = 'wpe_keywords_nonce_save'; | |
// Verifica si nonce existe | |
if ( ! isset( $nonce_name ) ) { | |
return; | |
} | |
// Verifica si nonce es valido | |
if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) ) { | |
return; | |
} | |
// Verifica si el usuario tiene permisos para editar posts | |
if ( ! current_user_can( 'edit_post', $post_id ) ) { | |
return; | |
} | |
// Verifica que este guardando un 'book' | |
if ( 'book' !== get_post_type() ) { | |
return; | |
} | |
// Verifica si no es un guardado automatico | |
if ( wp_is_post_autosave( $post_id ) ) { | |
return; | |
} | |
// Verifica si no es una revision | |
if ( wp_is_post_revision( $post_id ) ) { | |
return; | |
} | |
// Si todas las validaciones anteriores pasaron, | |
// entonces actualiza los valores en la base de datos. | |
update_post_meta( $post_id, '_keywords', sanitize_text_field( $_POST['keywords'] ) ); | |
update_post_meta( $post_id, '_description', sanitize_text_field( $_POST['description'] ) ); | |
} | |
add_action( 'save_post', 'wpe_save_mb' ); |
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 | |
/* | |
* Imprime el codigo HTML para SEO al mostrar un libro | |
*/ | |
function wpe_print_seo_tags() { | |
global $post; | |
// Obtenemos los valores desde la base de datos | |
$meta_keywords = get_post_meta( $post->ID, '_keywords', true ); | |
$meta_description = get_post_meta( $post->ID, '_description', true ); | |
// HTML con las etiquetas SEO y sus valores | |
$html = ' | |
<meta name="keywords" content="' . $meta_keywords . '" /> | |
<meta name="description" content="' . $meta_description . '" /> | |
'; | |
echo $html; | |
} | |
// Llama funcion que imprime etiquetas SEO con prioridad 1 | |
// para imprimir el codigo lo antes posible despues de <title></title> | |
add_action( 'wp_head', 'wpe_print_seo_tags', 1 ); |
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 | |
/* | |
* Reestablece los formatos de enlaces al desactivar o activar el plugin. | |
*/ | |
register_deactivation_hook( __FILE__, 'wpe_flush_rewrite_rules' ); | |
register_activation_hook( __FILE__, 'wpe_flush_rewrite_rules' ); | |
function wpe_flush_rewrite_rules() { | |
flush_rewrite_rules(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment