4

I have renamed my post formats before, but since the Gutenberg update it doesn't work anymore.

I tried Aaron's translation-solution from the link below, but WordPress doesn't use the translations in the back end. Is there a new way to do this? I am not experienced enough to be able to dig this out of WordPress myself.

The link: Is it possible to rename a post format?

This is my attempt:

function rename_post_formats( $translation, $text, $context, $domain ) {

  $names = array(
    'Aside'  => 'Breaking News',
    'Status' => 'Notice'
  );

  if ( $context == 'Post format' ) {

    $translation = str_replace( array_keys($names), array_values($names), $text );

    var_dump( $translation );
  }
  return $translation;  
}

add_filter( 'gettext_with_context', 'rename_post_formats', 10, 4 );

When I var_dump $translation after doing the str_replace, it contains the new names, but WordPress doesn't use it. Also, weirdly, it is dumped 5 times in a row (don't know if that helps).

Help would be greatly appreciated!

EDIT: In case it was unclear, the place I want the names to change is in this dropdown: enter image description here

1

The situation with changing strings in the block editor is complicated. There's 2 problems here:

  1. The list of post formats used by the block editor (Gutenberg) is not filterable using JavaScript hooks, just as the list is not filterable in PHP.
  2. The JavaScript translation functions used by the block editor do not have equivalents of the gettext hooks that are available for the PHP translation functions eg. gettext_with_context.

So what can you do? Pascal Birchler has a very thorough overview of the state of localization in WordPress 5.0 here. In that article he notes that the gettext filters do not exist yet, but there is a PHP filter introduced in 5.0.2 called load_script_translations, which:

Filters script translations for the given file, script handle and text domain. This way you can override translations after they have been loaded from the translation file.

To use it, you need to know the handle of the script being translated, decode the JSON for the translation, then re-encode it as JSON.

In your case, the post format names are defined in the /packages/editor/src/components/post-format/index.js file in the block editor, which means that they are part of the "editor" package, which uses the handle wp-editor in WordPress (the script handles for JavaScript packages are available here).

So the way to use this filter to rename post formats would be:

add_filter(
    'load_script_translations',
    function( $translations, $file, $handle, $domain ) {
        /**
         * The post format labels used for the dropdown are defined in the
         * "wp-editor" script.
         */
        if ( 'wp-editor' === $handle ) {
            /**
             * The translations are formatted as JSON. Decode the JSON to modify
             * them.
             */
            $translations = json_decode( $translations, true );

            /**
             * The strings are inside locale_data > messages, where the original
             * string is the key. The value is an array of translations.
             *
             * Singular strings only have one value in the array, while strings
             * with singular and plural forms have a string for each in the array.
             */
            $translations['locale_data']['messages']['Aside']  = [ 'Breaking News' ];
            $translations['locale_data']['messages']['Status'] = [ 'Notice' ];

            /**
             * Re-encode the modified translations as JSON.
             */
            $translations = wp_json_encode( $translations );
        }

        return $translations;
    },
    10,
    4
);

One thing to keep in mind is that unlike the gettext filters, this filter is only filtering the translations for a specific script. So if the post format names are defined and used in another script, you will need to filter that script's translations separately. You will also need to continue using the gettext filters to translate the post format names outside of the block editor.

|improve this answer|||||
  • This looks incredibly comprehensible, but it doesn't work. I ran the script as-is, to make sure I didn't mess anything up, and the post formats are still named the same as before. I'll post an update to make the question more specific, and that might clear up if we have a misunderstanding. – Benjamin Antoni Andersen Nov 29 '19 at 15:09
  • 1
    I did test the code. It works. – Jacob Peattie Nov 29 '19 at 23:19
  • I'm grateful for you taking time to answer me, but still, when I run it, it doesn't do anything. I have the latest version of WordPress. Should it work by pasting exactly what you write into functions? Because that gives me no result. And are we on the same page when it comes to where we want the translation to show up? As I showed in my latest update to the question? – Benjamin Antoni Andersen Dec 9 '19 at 12:22
  • The code block from my answer, pasted directly into a fresh site, with a theme that supports post formats, results in i.stack.imgur.com/pVgSs.png. Only reason I can imagine it not working is that you have other code interfering in some way. – Jacob Peattie Dec 9 '19 at 14:05
  • Thanks for the clarification. I'll try again at a later time :) – Benjamin Antoni Andersen Jan 21 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.