Fragmentos de código para hacer modificaciones útiles en WordPress.
Incluídos en la entrada http://wprincipiante.es/codigo-util-wordpress/
Fragmentos de código para hacer modificaciones útiles en WordPress.
Incluídos en la entrada http://wprincipiante.es/codigo-util-wordpress/
| <?php | |
| // Más info en https://codex.wordpress.org/Plugin_API/Filter_Reference/excerpt_length | |
| function mi_tamano_extractos( $length ) { | |
| return 30; // número de palabras a incluir en el extracto | |
| } | |
| add_filter( 'excerpt_length', 'mi_tamano_extractos' ); |
| <?php | |
| // Más info en https://codex.wordpress.org/Plugin_API/Filter_Reference/registration_redirect | |
| function mi_redireccion_a_usuarios( $registration_redirect ) { | |
| return home_url( '/bienvenido/' ); // redirecciona a http://tudominio.com/bienvenido/ | |
| } | |
| add_filter( 'registration_redirect', 'mi_redireccion_a_usuarios' ); |
| <?php | |
| // Más info en https://developer.wordpress.org/reference/hooks/comment_form_default_fields/ | |
| function elimina_campos_form_comentario( $fields ) { | |
| unset( $fields['url'] ); // tus usuarios no tendrán que poner una URL para comentar | |
| return $fields; | |
| } | |
| add_filter( 'comment_form_default_fields', 'elimina_campos_form_comentario' ); |
| <?php | |
| // Más info en https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts | |
| function limita_resultados_pagina_busqueda() { | |
| if ( is_search() ) { | |
| set_query_var( 'posts_per_archive_page', 10 ); // Sólo 10 resultados en la búsqueda | |
| } | |
| } | |
| add_filter( 'pre_get_posts', 'limita_resultados_pagina_busqueda' ); |
| <?php | |
| function modo_mantenimiento_on() { | |
| if( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) { | |
| wp_die( 'Estamos en mantenimiento. Vuelve pronto.', | |
| 'Estamos en mantenimiento. Vuelve pronto.', array( 'response' => '503') ); | |
| } | |
| } | |
| add_action( 'get_header', 'modo_mantenimiento_on' ); |
| <?php | |
| function comprobar_imagen_destacada( $post_id ) { | |
| // elegimos los tipos de entrada que queremos tratar (sólo post aquí) | |
| if( get_post_type( $post_id ) != 'post' ) | |
| return; | |
| if ( ! has_post_thumbnail( $post_id ) ) { | |
| // creamos un transient para mostrar un mensaje de error | |
| set_transient( "tiene_imagen", "no" ); | |
| // desactivamos el hook para evitar tener bucle infinito | |
| remove_action( 'save_post', 'comprobar_imagen_destacada' ); | |
| // actualiza la entrada poniendola a draft | |
| wp_update_post( array( 'ID' => $post_id, 'post_status' => 'draft' ) ); | |
| // volvemos a activar el hook | |
| add_action( 'save_post', 'comprobar_imagen_destacada' ); | |
| } else { | |
| delete_transient( "tiene_imagen" ); | |
| } | |
| } | |
| function mostrar_error_no_hay_imagen() { | |
| // Comprobamos si existe el transient y mostramos el error | |
| if ( get_transient( "tiene_imagen" ) == "no" ) { | |
| echo "<div id='message' class='error'><p><strong>Tienes que añadir una imagen destacada antes de poder publicar esto. No te preocupes, tu entrada se ha guardado bien.</strong></p></div>"; | |
| delete_transient( "tiene_imagen" ); | |
| } | |
| } | |
| add_action( 'save_post', 'comprobar_imagen_destacada' ); | |
| add_action( 'admin_notices', 'mostrar_error_no_hay_imagen' ); |
| <?php | |
| function mi_propio_logo() { | |
| echo '<style type="text/css"> | |
| #header-logo { background-image: url('.get_bloginfo('template_directory').'/images/mi-logo.gif) !important; }</style>'; | |
| } | |
| add_action( 'admin_head', 'mi_propio_logo' ); |