Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Cambia el nombre del archivo a una versión segura y sanitizada.
- *
- * @param string $filename Nombre del archivo una vez pasados los primeros filtros de WP.
- * @param string $filename_raw Nombre del archivo "en crudo" al subirse.
- *
- * @return string
- *
- * @since 1.2.0
- */
- function cl_nombre_archivo( $filename, $filename_raw ) {
- $info = pathinfo( $filename_raw );
- $nombre_archivo = $info['filename'];
- if ( ! empty( $info['extension'] ) ) {
- $ext = $info['extension'];
- } else {
- $ext = '';
- }
- $nombre_archivo = remove_accents( $nombre_archivo );
- $nombre_archivo = str_replace( '_', '-', $nombre_archivo );
- $nombre_archivo = str_replace( '%20', '-', $nombre_archivo );
- $nombre_archivo = sanitize_title( $nombre_archivo );
- $nombre_archivo = $nombre_archivo . '.' . $ext;
- return $nombre_archivo;
- }
- add_filter( 'sanitize_file_name', 'cl_nombre_archivo', 10, 2 );
- /**
- * Añade al ALT de las imágenes el título de la foto.
- *
- * @param int $meta_id Id del metadato.
- * @param int $post_id Id del post.
- * @param string $meta_key Clave del metadato.
- * @param mixed $meta_value Valor del metadato.
- *
- * @return void
- *
- * @since 1.2.0
- */
- function cl_alt_after_post_meta( $meta_id, $post_id, $meta_key, $meta_value ) {
- if ( '_wp_attachment_metadata' === $meta_key ) {
- $titulo = get_the_title( $post_id ); // Obtenemos el título del archivo.
- // Actualizamos el Texto del ALT.
- update_post_meta( $post_id, '_wp_attachment_image_alt', $titulo );
- }
- }
- add_action( 'added_post_meta', 'cl_alt_after_post_meta', 10, 4 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement