How to prevent text modified using gettext filter being stomped (presumably) by updateText() js function within a CPT’s edit screen

First a teeny bit of background… I have a custom post type where I’d like to use custom post statuses. Since custom post statuses (stati/status/whatever), have not been fully implemented within the core, I’ve decided to use the gettetext filters to change the names of certain things. For example, I’ve changed the text “Publish” on the publish button to say “Save”. I went with something like Rarst’s approach here. A summary of my code is below. I’m really not going for anything drastic here as far as changing what each status does/is.

Anyway, the translation works just fine as far as PHP is concerned. However, while testing, I realized that if I click the Edit link within the misc publishing actions div, then click the cancel link that subsequently appears, my translated “Save” button will revert to a “Publish” button.

Read More

This seems to happen as a result of the javascript function updateText() found in /wp-admin/js/post.dev.js. I’m looking for a way to keep the JS from stomping my translation. I’d prefer not to just mess with it using DOM manipulation, but if that’s my only option, I’ll go for it. I’m running 3.1, btw.

<?php
class MyClass {

        function __contsruct() {
            add_action( 'admin_head', array ( &$this, 'load_gettext_filters' ) );
        }

        function load_gettext_filters() {
            add_filter( 'gettext', array ( &$this, 'change_publishing_text' ), 10, 2 );
        }

        function change_publishing_text( $translation, $text ) {
            global $post;

            if ( ! isset ( $post->post_type ) ) {
                return $translation;
            }

            if ( 'mypost' === $post->post_type ) {
                if ( ( 'Publish' === $text ) ) {
                    return 'Save';
                }
            }
            return $translation;
        }

    }

Related posts

Leave a Reply

2 comments

  1. First, you have nasty typo in __contsruct. 🙂

    Second, your hook timing is wrong. Related WP JavaScript is localized via postL10n object (you can see it echoed in page’s source), that gets put together on init hook – way earlier then admin_head and your filter is not in place yet.

    From quick test this should do it:

    add_action( 'init', array ( &$this, 'load_gettext_filters' ), 9 );
    

    Update

    Ok, scratch that. Won’t work if we need context. Let’s try this:

    add_action('admin_footer', array ( &$this, 'localize_post_script' ) );
    
        function localize_post_script() {
    
            global $wp_scripts;
    
            $wp_scripts->registered['post']->extra['l10n'][1]['publish'] = __('Publish');
        }
    
  2. You both have the right idea, but you’ll actually need both filters, one to catch the button text, another to catch the localized text, so a combination of the two.

    This works..

    class MyClass {
        function __construct() {
            add_action('admin_head-post.php',     array ( $this, 'load_gettext_filters' ), 1 );
            add_action('admin_head-post-new.php', array ( $this, 'load_gettext_filters' ), 1 );
        }
    
        function load_gettext_filters() {
            global $post_type, $wp_scripts;
    
            if( 'YOURTYPENAMEHERE' != $post_type )
                return;
    
            $wp_scripts->registered['post']->extra['l10n'][1]['publish'] = __('Save');
    
            add_filter( 'gettext', array ( $this, 'change_publishing_text' ), 10, 2 );
        }
    
        function change_publishing_text( $translation, $text ) {
            if( 'Publish' != $text )
                return $translation;
    
            return __( 'Save' );
        }
    }
    $MyClass = new MyClass;