Come posso aggiungere un campo URL alla finestra degli allegati?
Per esempio...
add_action('init', 'reg_tax');
function reg_tax() {
register_taxonomy_for_object_type('category', 'attachment');
}
Aggiunge un campo di input "Categoria" al gestore media e all'editor degli allegati. Vorrei sapere se è possibile modificare questa funzione per catturare invece un URL di "destinazione del link". L'URL verrebbe eseguito quando l'immagine viene cliccata.
Ho anche bisogno di sapere come recuperare il valore per questo nuovo campo.
AGGIORNAMENTO: Grazie alla risposta di Thomas qui sotto, ecco la mia soluzione finale...
function my_image_attachment_fields_to_edit($form_fields, $post) {
$form_fields["custom1"] = array(
"label" => __("Image Links To"),
"input" => "text",
"value" => get_post_meta($post->ID, "_custom1", true)
);
return $form_fields;
}
function my_image_attachment_fields_to_save($post, $attachment) {
if( isset($attachment['custom1']) ){
update_post_meta($post['ID'], '_custom1', $attachment['custom1']);
}
return $post;
}
add_filter("attachment_fields_to_edit", "my_image_attachment_fields_to_edit", null, 2);
add_filter("attachment_fields_to_save", "my_image_attachment_fields_to_save", null, 2);

Utilizzo un plugin molto grezzo per aggiungere informazioni sull'artista e un URL ai file multimediali. Ha bisogno di qualche ritocco (e io ho bisogno di tempo), ma funziona e può dimostrare come aggiungere i campi extra e come utilizzarli nel tuo tema:
<?php
/*
Plugin Name: Media Artist Field
Description: Aggiunge due campi agli allegati – Artista e URL Artista – e aggiunge queste informazioni alle didascalie.
Version: 0.1
Author: Fuxia Scholz
Created: 19.09.2010
*/
$Media_Artist = new Media_Artist(
array (
'artist_name' => array (
'public' => 'artist_name'
, 'hidden' => '_artist_name'
, 'label' => 'Fotografo (Nome)'
)
, 'artist_url' => array (
'public' => 'artist_url'
, 'hidden' => '_artist_url'
, 'label' => 'Fotografo (URL)'
)
)
, 'Foto: '
);
/**
* Aggiunge due campi per i crediti a qualsiasi file multimediale: nome e URL.
*
* Basato sul chiaro tutorial di Andy Blackwell:
* @link http://net.tutsplus.com/?p=13076
*/
class Media_Artist
{
public
$fields = array (
'artist_name' => array (
'public' => 'artist_name'
, 'hidden' => '_artist_name'
, 'label' => 'Nome Artista'
)
, 'artist_url' => array (
'public' => 'artist_url'
, 'hidden' => '_artist_url'
, 'label' => 'URL Artista'
)
)
// Forse il suo campo?
, $caption_prefix
, $br_before = TRUE;
public function __construct(
$fields = array()
, $caption_prefix = 'Fonte: '
, $br_before = TRUE
)
{
$this->fields = array_merge($this->fields, $fields);
$this->caption_prefix = $caption_prefix;
$this->br_before = (bool) $br_before;
$this->set_filter();
}
public function set_filter()
{
add_filter(
'attachment_fields_to_edit'
, array ( $this, 'add_fields' )
, 15
, 2
);
add_filter(
'attachment_fields_to_save'
, array ( $this, 'save_fields' )
, 10
, 2
);
add_filter(
'img_caption_shortcode'
, array ( $this, 'caption_filter' )
, 1
, 3
);
}
public function add_fields($form_fields, $post)
{
foreach ( $this->fields as $field)
{
$form_fields[ $field['public'] ]['label'] = $field['label'];
$form_fields[ $field['public'] ]['input'] = 'text';
$form_fields[ $field['public'] ]['value'] = get_post_meta(
$post->ID
, $field['hidden']
, TRUE
);
}
return $form_fields;
}
public function save_fields($post, $attachment)
{
foreach ( $this->fields as $field)
{
if ( isset ( $attachment[ $field['public'] ]) )
{
update_post_meta(
$post['ID']
, $field['hidden']
, $attachment[ $field['public'] ]
);
}
}
return $post;
}
public function caption_filter($empty, $attr, $content = '')
{
/* Input tipico:
* [caption id="attachment_525" align="aligncenter"
* width="300" caption="La didascalia."]
* <a href="http://example.com/2008/images-test/albeo-screengrab/"
* rel="attachment wp-att-525"><img
* src="http://example.com/uploads/2010/08/albeo-screengrab4.jpg?w=300"
* alt="" title="albeo-screengrab" width="300" height="276"
* class="size-medium wp-image-525" /></a>[/caption]
*/
extract(
shortcode_atts(
array (
'id' => ''
, 'align' => 'alignnone'
, 'width' => ''
, 'caption' => ''
, 'nocredits' => '0'
)
, $attr
)
);
// Lascia che WordPress gestisca questi casi.
if ( empty ($id ) or 1 == $nocredits )
{
return '';
}
if ( 1 > (int) $width || empty ( $caption ) )
{
return $content;
}
if ( ! empty ( $id ) )
{
// Esempio: attachment_525
$html_id = 'id="' . esc_attr($id) . '" ';
$tmp = explode('_', $id);
$id = end($tmp);
$sub_caption = '';
$artist_name = get_post_meta($id, $this->fields['artist_name']['hidden'], TRUE);
$artist_url = get_post_meta($id, $this->fields['artist_url']['hidden'], TRUE);
// Ok, almeno un valore.
if ( '' != $artist_name . $artist_url )
{
$sub_caption .= $this->br_before ? '<br />' : '';
$sub_caption .= '<span class="media-artist">' . $this->caption_prefix;
// Nessun nome fornito. Usiamo l'URL abbreviato.
if ( '' == $artist_name )
{
$sub_caption .= '<a rel="author" href="'
. $artist_url . '">'
. $this->short_url($artist_url)
. '</a>';
} // Abbiamo solo il nome.
elseif ( '' == $artist_url )
{
$sub_caption .= $artist_name;
} // Abbiamo entrambi.
else
{
$sub_caption .= '<a rel="author" href="'
. $artist_url . '">'
. $artist_name
. '</a>';
}
$sub_caption .= '</span>';
}
$caption .= $sub_caption;
}
return '<div ' . $html_id . 'class="wp-caption ' . esc_attr($align)
. '" style="width: ' . (10 + (int) $width) . 'px">'
. do_shortcode( $content ) . '<p class="wp-caption-text">'
. $caption . '</p></div>';
}
public function short_url($url, $max_length=20)
{
$real_length = mb_strlen($url, 'UTF-8');
if ( $real_length <= $max_length )
{
return $url;
}
$keep = round( $max_length / 2 ) - 1;
return mb_substr($url, 0, $keep, 'UTF-8') . '…'
. mb_substr($url, -$keep, $real_length, 'UTF-8');
}
# @todo disinstallazione
}

Rispondendo alla domanda di Drew nei commenti, puoi personalizzare l'HTML per il campo impostando input
su una nuova stringa, e poi aggiungendo quella stessa stringa come chiave all'array $form_fields
.
Di default, WordPress accetterà solo text
e textarea
come tipo di input
. Qualsiasi altra cosa dovrà essere definita in modo personalizzato come mostrato sotto. Non ho provato a rendere persistenti i campi del modulo in questo modo, quindi per creare un altro tipo di input, come un radio button, potrebbe richiedere un po' di lavoro aggiuntivo.
add_filter( 'attachment_fields_to_edit', 'change_fields', 10, 2 );
function change_fields( $form_fields, $post ) {
$form_fields["some_new_field"] = array(
"label" => __("Digita qualcosa"), // Traduzione del testo del label
"input" => "arbitrary_value",
"value" => get_post_meta($post->ID, "some_new_field", true),
"arbitrary_value" => "hello world!" // Valore arbitrario per il campo
);
}
