Este gist contiene los ejemplos de mi entrada Creación de Plugins en WordPress (II): Organización y Trucos en WPrincipiante.
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 | |
/** | |
* Este fichero define el contenido de la meta box de títulos. | |
* | |
* Como se trata de una meta box, esta plantilla únicamente se usa cuando se | |
* está editando una entrada concreta. La plantilla supone que las siguientes | |
* variables están definidas: | |
* * $val {string} El valor de la extensión de título de la entrada actual. | |
* | |
* @author David Aguilera <david.aguilera@neliosoftware.com> | |
* | |
* @link http://wprincipiante.es | |
* @since 1.0.0 | |
*/ | |
?> | |
<label for="wprincipiante-extension-titulo">Texto:</label> | |
<input name="wprincipiante-extension-titulo" type="text" value="<?php | |
echo esc_attr( $val ); | |
?>" /> |
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 | |
/** | |
* Este fichero contiene toda la lógica relacionada con el funcionamiento de | |
* la meta box de títulos. En concreto, se encarga de añadir la meta box en | |
* la UI de WordPress y de guardar los valores que pide al usuario. | |
* | |
* @author David Aguilera <david.aguilera@neliosoftware.com> | |
* | |
* @link http://wprincipiante.es | |
* @since 1.0.0 | |
*/ | |
/** | |
* Esta función añade nuestra meta box de ejemplo. | |
* | |
* @link http://wprincipiante.es | |
* @since 1.0.0 | |
*/ | |
function wprincipiante_add_meta_boxes() { | |
add_meta_box( | |
'wprincipiante-extension-titulo', | |
'Extensión del Título', | |
'wprincipiante_print_extension_titulo_meta_box' | |
); | |
} | |
add_action( 'add_meta_boxes_post', 'wprincipiante_add_meta_boxes' ); | |
/** | |
* Esta función es el callback que se utiliza para dibujar la meta box. | |
* | |
* @param WP_POST $post la entrada que se está editando. | |
* | |
* @link http://wprincipiante.es | |
* @since 1.0.0 | |
*/ | |
function wprincipiante_print_extension_titulo_meta_box( $post ) { | |
$post_id = $post->ID; | |
$val = get_post_meta( $post_id, '_wprincipiante_extension_titulo', true ); | |
include 'partials/meta-box-extension-titulo.php'; | |
} | |
/** | |
* Esta función se encarga de guardar el valor de la meta box. | |
* | |
* @param int $post el identificador de la entrada que se va a guardar. | |
* | |
* @link http://wprincipiante.es | |
* @since 1.0.0 | |
*/ | |
function wprincipiante_save_extension_titulo( $post_id ) { | |
// Si se está guardando de forma automática, no hacemos nada. | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { | |
return; | |
} | |
// Si nuestro campo de texto no está disponible, no hacemos nada. | |
if ( ! isset( $_REQUEST['wprincipiante-extension-titulo'] ) ) { | |
return; | |
} | |
// Ahora sí, coger el valor del campo de texto y limpiarlo por seguridad. | |
$texto = trim( sanitize_text_field( $_REQUEST['wprincipiante-extension-titulo'] ) ); | |
// Guardarlo en el campo personalizado "_wprincipiante_extension_titulo" | |
update_post_meta( $post_id, '_wprincipiante_extension_titulo', $texto ); | |
} | |
add_action( 'save_post', 'wprincipiante_save_extension_titulo' ); |
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 | |
/** | |
* Este fichero contiene las funciones necesarias para modificar el front-end. | |
* | |
* @author David Aguilera <david.aguilera@neliosoftware.com> | |
* | |
* @link http://wprincipiante.es | |
* @since 1.0.0 | |
*/ | |
/** | |
* Dada una cierta entrada, le añade la extensión de título que tenga definida. | |
* | |
* @param string $title el título que se va a pintar por pantalla. | |
* @param string $id el id de la entrada asociada a este título. | |
* | |
* @return string el título junto con la extensión que la entrada tenga definida. | |
* | |
* @link http://wprincipiante.es | |
* @since 1.0.0 | |
*/ | |
function wprincipiante_cambiar_titulo( $title, $id ) { | |
$texto = get_post_meta( $id, '_wprincipiante_extension_titulo', true ); | |
if ( ! empty( $texto ) ) { | |
$title = $title . ' ' . $texto; | |
} | |
return $title; | |
} | |
add_filter( 'the_title', 'wprincipiante_cambiar_titulo', 10, 2 ); |
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: Ejemplo de WPrincipiante | |
* Plugin URI: http://wprincipiante.es | |
* Description: Este plugin modifica los títulos de las entradas. | |
* Version: 1.0.0 | |
* Author: David Aguilera | |
* Author URI: http://neliosoftware.com | |
* Requires at least: 4.0 | |
* Tested up to: 4.3 | |
* | |
* Text Domain: wprincipiante-ejemplo | |
* Domain Path: /languages/ | |
*/ | |
defined( 'ABSPATH' ) or die( '¡Sin trampas!' ); | |
if ( is_admin() ) { | |
require_once( 'admin/meta-box-extension-titulo.php' ); | |
} else { | |
require_once( 'public/cambia-titulos.php' ); | |
} |
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 | |
if($aux1 && !empty($aux2 )) | |
{ wprincipiante_update( $post,$aux2); | |
wprincipiante_ok(); | |
} | |
else{ wprincipiante_error(2);} |
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 | |
if ( $is_data_valid && !empty( $name ) ) { | |
wprincipiante_update_post_name( $post, $name ); | |
wprincipiante_print_success_message(); | |
} else { | |
wprincipiante_print_error_message( WPRINCIPIANTE_INVALID_DATA_ERR ); | |
} |
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 suma( $a, $b ) { | |
// Suma los valores $a y $b | |
$c = $a + $b; | |
// Devuelve la suma de $a y $b | |
return $c; | |
} |
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
/** | |
* Esta función renderiza la colección. | |
* | |
* Para cada elemento de la colección, tnemos que crear su propia | |
* vista, renderizarla y añadirla a la vista padre (es decir, la | |
* propia lista de la colección). | |
* | |
* @return {AccountListView} esta instancia. | |
*/ | |
render: function() { | |
// Lo primero que hacemos es lanzar un evento "render", de tal | |
// forma que las vistas anidadas viejas puedan ser eliminadas. | |
this.trigger( 'nelioab:render' ); | |
// Preparamos la nueva vista de la colección. | |
this.$el.html( this.template() ); | |
var list = this.$( '.list' ); | |
this.collection.each( function( action ) { | |
// Creamos la vista para uno de los elementos. | |
var view = new ActionView( action ); | |
list.append( view.render().el ); | |
// Y hacemos que escuche el evento de renderizado. Como ya | |
// hemos visto, cuando se genere ese evento, habrá que | |
// eliminar la vista `view`, puesto que se generará una | |
// nueva y esta ya no será necesaria. Con ello, conseguimos | |
// evitar "memory leaks". | |
view.listenTo( this, 'nelioab:render', view.close ); | |
} | |
}, | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment