Usaremos los siguientes filtros para realizar el cambio: attach_fields_to_edit y attach_fields_to_save
Para un proyecto como este, le recomendamos que cree un sitio específico plugin y agregue el siguiente código. Sin embargo, aún puede agregar los códigos en el archivo functions.php de su tema para que funcione.
/** * Add Photographer Name and URL fields to media uploader * * @param $form_fields array, fields to include in attachment form * @param $post object, attachment record in database * @return $form_fields, modified form fields */ function be_attachment_field_credit( $form_fields, $post ) { $form_fields['be-photographer-name'] = array( 'label' => 'Photographer Name', 'input' => 'text', 'value' => get_post_meta( $post->ID, 'be_photographer_name', true ), 'helps' => 'If provided, photo credit will be displayed', ); $form_fields['be-photographer-url'] = array( 'label' => 'Photographer URL', 'input' => 'text', 'value' => get_post_meta( $post->ID, 'be_photographer_url', true ), 'helps' => 'Add Photographer URL', ); return $form_fields; } add_filter( 'attachment_fields_to_edit', 'be_attachment_field_credit', 10, 2 ); /** * Save values of Photographer Name and URL in media uploader * * @param $post array, the post data for database * @param $attachment array, attachment fields from $_POST form * @return $post array, modified post data */ function be_attachment_field_credit_save( $post, $attachment ) { if( isset( $attachment['be-photographer-name'] ) ) update_post_meta( $post['ID'], 'be_photographer_name', $attachment['be-photographer-name'] ); if( isset( $attachment['be-photographer-url'] ) ) update_post_meta( $post['ID'], 'be_photographer_url', esc_url( $attachment['be-photographer-url'] ) ); return $post; } add_filter( 'attachment_fields_to_save', 'be_attachment_field_credit_save', 10, 2 ); ?>
El código anterior agregará dos campos de texto al cargador de medios llamados Nombre del fotógrafo y URL del fotógrafo. Puede ver eso en la captura de pantalla a continuación:
Explicación del código: En la primera función, simplemente estamos usando una matriz para especificar la etiqueta del campo, el tipo de entrada, el valor y el texto de ayuda. La segunda función es verificar si se ha establecido un valor para esos campos. SI se establece el valor, se actualizan los metadatos de la publicación.
Si desea mostrar los campos en su plantilla de archivos adjuntos, simplemente pegue los siguientes códigos dentro del ciclo:
echo get_post_meta($post->ID, 'be_photographer_url', true);
Si desea mostrar los campos para su imagen destacada en su plantilla de archivo o cualquier otra plantilla, simplemente use:
echo get_post_meta(get_post_thumbnail_id(), 'be_photographer_url', true);
Esperamos que haya disfrutado de este artículo. Para aquellos que no saben cómo crear la plantilla de un archivo adjunto, no se preocupen. En el próximo artículo, cubriremos cómo crear una plantilla de archivo adjunto en WordPress.
Punta de sombrero para Bill Erickson por mostrarnos cómo hacer esto.
.
Fuente: wpbeginner