Hier ist eine Lösung, die den shortcode_atts_{shortcode} -Filter nutzt, wobei caption in diesem Fall der shortcode ist.
Dies funktioniert, indem die Anhangs-ID aus $out['id'] dann der Wert der class über get_post_meta( $attachment_id, 'img_class_field', true ) .
Bei diesem Ansatz ist es nicht notwendig, die Ausgabe des Untertitel-Shortcodes im Editor zu ändern. Das Hinzufügen der benutzerdefinierten class erfolgt hinter den Kulissen. Es funktioniert auch weiterhin, wenn ein Benutzer dem Beschriftungs-Shortcode manuell ein classnattribut hinzugefügt hat.
/** * Adds class to caption shortcode output via img_class_field attachment meta. * * @param array $out The output array of shortcode attributes. * @param array $pairs The supported attributes and their defaults. * @param array $atts The user defined shortcode attributes. * @param string $shortcode The shortcode name. */ add_filter( 'shortcode_atts_caption', 'wpse_shortcode_atts_caption', 10, 4 ); function wpse_shortcode_atts_caption( $out, $pairs, $atts, $shortcode ) { // Get the attachment id. It should be available via $out['id'], but // it will be in the format 'attachment_xxxx' where xxxx is the id. // We'll try to get the id portion and we'll bail if this doesn't work. $attachment_id = isset( $out['id'] ) && ( $out['id'] ) ? $out['id'] : false; $attachment_id = (int) preg_replace( '/^attachment_/', '', $attachment_id ); if ( ! $attachment_id ) { return $out; } // Get the custom image class and add it to the existing classes $extra_image_class = get_post_meta( $attachment_id, 'img_class_field', true ); if ( $extra_image_class ) { $spacer = isset( $out['class'] ) && ( $out['class'] ) ? ' ' : ''; $out['class'] .= esc_attr( $spacer . $extra_image_class ); } return $out; }
Verwendung
Benutzerdefinierte classn wurden zu Anhangsmetadaten hinzugefügt (ich habe 2 als gutes Maß verwendet):
some-class another-class
Beispiel für nicht modifizierten Untertitel-Shortcode, der über Media-Modal hinzugefügt wurde:
[caption id="attachment_2397" align="alignnone" width="480"] This is the caption![/caption]
Beispielausgabe 1: ( Thema unterstützt HTML5 für Bildunterschriften):
This is the caption!
Beispielausgabe 2: (Das Thema unterstützt HTML5 für Untertitel nicht):
This is the caption!
Meine Idee dahinter wäre, einen benutzerdefinierten Feldaufruf innerhalb des Shortcodes selbst zu verwenden, dann einen Filter shortcode_atts _ {$ shortcode} hinzuzufügen, um das benutzerdefinierte Feld zu verarbeiten und es der class hinzuzufügen, um es zurückzugeben.
{shortcode} in diesem Fall eine caption denn das ist es, woran wir bauen.