Skip to content

Instantly share code, notes, and snippets.

@lucymtc
Created April 13, 2016 22:42
Show Gist options
  • Select an option

  • Save lucymtc/31f19e90a7657d5df12a9e559ca782b6 to your computer and use it in GitHub Desktop.

Select an option

Save lucymtc/31f19e90a7657d5df12a9e559ca782b6 to your computer and use it in GitHub Desktop.
Ejemplos meetup WPValencia Introducción a hooks
<?php
/**
* Plugin Name: Ejemplos WPValencia meetup
* Plugin URI: http://wordpress.org/plugins
* Description: Ejemplos para presentacion Introducción a los hooks.
* Version: 0.0.1
* Author: Lucy Tomas
* Author URI: http://lucytomas.com
* License: GPLv2+
*/
/**
* Modifica el mensaje de error al identificarse desde wp-login.php
*/
function ejp_login_errors( $error ) {
$error = 'Los datos de acceso no son correctos.';
return $error;
}
add_filter( 'login_errors', 'ejp_login_errors' );
/**
* Primera letra de cada palabra en mayúscula.
*/
function ejp_the_title( $title ){
return ucwords( $title );
}
add_filter( 'the_title', 'ejp_the_title' );
/**
* Cambiar el placeholder del título
* wp-admin/edit-form-advanced.php - line 553
* $title_placeholder = apply_filters( 'enter_title_here', __( 'Enter title here' ), $post );
*/
function ejp_enter_title_here( $title ){
$screen = get_current_screen();
if( 'post' === $screen->post_type ){
$title = 'Cambiado!';
}
return $title;
}
add_filter( 'enter_title_here', 'ejp_enter_title_here' );
/////// META BOX
/**
* Crea un nuevo metabox en la página donde editamos un post
*/
function ejp_add_metabox() {
add_meta_box( 'ejp_dato_extra', 'Un dato extra', 'ejp_dato_extra_output', 'post', 'side' );
}
add_action( 'add_meta_boxes', 'ejp_add_metabox' );
/**
* Output del contenido de nuestro metabox
* en este caso un campo de texto.
*/
function ejp_dato_extra_output( $post ){
$dato_extra = get_post_meta( $post->ID, 'ejp_dato_extra', true );
wp_nonce_field( 'ejp_nombre_accion', 'ejp_dato_nonce' );
$output = '<p><em>El dato extra importante</p></em>';
$output .= '<input type="text" name="ejp_dato_extra" value="' . esc_attr( $dato_extra ) . '">';
echo $output;
}
/**
* Guarda el dato como metadata del post
*/
function ejp_save_dato_extra( $post_id ) {
if( ! isset( $_POST['ejp_dato_nonce'] ) || ! wp_verify_nonce( $_POST['ejp_dato_nonce'], 'ejp_nombre_accion' ) ) {
return false;
}
if( 'post' === $_POST['post_type'] ) {
if( ! current_user_can( 'edit_post', $post_id ) ) {
return false;
}
$value = sanitize_text_field( $_POST['ejp_dato_extra'] );
$result = update_post_meta( $post_id, 'ejp_dato_extra', $value );
if( false !== $result ) {
do_action( 'ejp_dato_extra_actualizado' );
}
}
}
add_action( 'save_post', 'ejp_save_dato_extra' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment