Courtesy Text
-
Is there a way I can change the “Image courtesy of” test to “Cropped Image Copyright of”.
-
You could filter the credit markup using
media_credit_at_end
or the “translation” usingngettext
.Thank you that’s great. I haven’t used thar before, what a simple trick…
I have tried various methods with no success, my current code is:
add_filter('gettext', 'change_text', 20, 3); add_filter('ngettext', 'change_text', 20, 3); function change_text( $translation, $text, $domain ) { $custom_text = array( 'Image courtesy of' => 'Image (cropped) copyright of', ); if( array_key_exists( $translation, $custom_text ) ) { $translation = $custom_text[$translation]; } return $translation; }
Can you help?
I have resolved it using Text Replace Plugin
Hi @gphoenix5727, you are adding a filter with the
gettext
arguments to thengettext
filter which expects these parameters:string $translation
Translated text.string $single
The text to be used if the number is singular.string $plural
The text to be used if the number is plural.string $number
The number to compare against to use either the singular or plural form.string $domain
Text domain. Unique identifier for retrieving translated strings.
Also, the translated strings contain positional parameters for inserting the actual image credits which need to be accounted for during the replacement.
Since you are probably running WordPress >= 5.5, it would probably be best to hook to the domain variant (
ngettext_media-credit
). Something like this should work:add_filter('ngettext_media-credit', 'my_change_text', 20, 4); function my_change_text( $translation, $single, $plural, $number ) { if ( 'Image courtesy of %2$s%1$s' === $single && 'Images courtesy of %2$s and %1$s' === $plural ) { // Modify $translation based on $number - could be plural! ... } return $translation; }
sorry for the maybe silly question … where to put this code?
@cgra You could put this into your child theme’s
functions.php
, but please note that it is not a complete implementation, just the necessary scaffolding. Since you had to ask, I’m assuming you are not a PHP developer yourself. In that case it would be to hire one to do the necessary customizations for your website (probably if overkill if this is the only feature missing, but I guess there will be others).Hi pepe, you are assuming right ?? But I have many years experience with tweeks here and there and sometimes you have to modify in functions.php, sometimes in post.php, sometimes in the php of the plugin itself. I’ll give it a try in my functions.php, thanks for the quick answer!
ok, I don’t get it to work. Could you help me with the full code to get the output “Images: ” and in german “Bilder: ” (single an plural) instead of “Image courtesy of”?
That would be great ??Can you post the code you did try, @cgra?
I tried this (sorry in advance if this is totally stupid …)
add_filter('ngettext_media-credit', 'my_change_text', 20, 4); function my_change_text( $translation, $single, $plural, $number ) { if ( 'Image courtesy of %2$s%1$s' === $single && 'Images courtesy of %2$s and %1$s' === $plural ) { // Modify $translation based on $number - could be plural! $custom_text = array( 'Image courtesy of' => 'Image:', ); if( array_key_exists( $translation, $custom_text ) ) { $translation = $custom_text[$translation]; } return $translation; } }
Yeah this can’t work. Try this snippet (not tested and really only valid if your target language is German and nothing else).
add_filter('ngettext_media-credit', 'my_change_text', 20, 4); function my_change_text( $translation, $single, $plural, $number ) { if ( 'Image courtesy of %2$s%1$s' === $single && 'Images courtesy of %2$s and %1$s' === $plural ) { // Modify $translation based on $number - could be plural! $translation = $number > 1 ? 'Bilder: %2$s und %1$s' : 'Bild: %2$s%1$s'; } return $translation; }
Hi pepe, thank you so much, it works perfectly! As we are only targeting german language with our online-magazine this is the solution!
You can take a look if you want here:
https://vanlifemagazin.eu/the-american-way-of-drive-die-geschichte-der-kult-caravans-von-airstream/
(magazine is in development)
Kind regards,
ChristianThere will be a new filter hook
media_credit_at_end_use_short_label
to enable the short form (Images:
) in version 4.2.0.
- The topic ‘Courtesy Text’ is closed to new replies.