Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a way to change the text of the publish button on a custom post type to say some different? For example, Save instead of Publish. And also remove the draft button?

share|improve this question
add comment

5 Answers

up vote 9 down vote accepted

If you look into /wp-admin/edit-form-advanced.php, you will find the meta box:

add_meta_box('submitdiv', __('Publish'), 'post_submit_meta_box', $post_type, 'side', 'core');

Note the __('Publish') – the function __() leads to translate() where you get the filter 'gettext'.

There are two ways to handle your problem: 1. Address the string in a single specialized function (be sure to match the correct textdomain!) or 2. use a more generic approach.

@Rarst has just now posted version 1, so I'll add version 2. :)

<?php
/*
Plugin Name: Retranslate
Description: Adds translations.
Version:     0.1
Author:      Thomas Scholz
Author URI:  http://toscho.de
License:     GPL v2
*/

class Toscho_Retrans {
    // store the options
    protected $params;

    /**
     * Set up basic information
     * 
     * @param  array $options
     * @return void
     */
    public function __construct( array $options )
    {
        $defaults = array (
            'domain'       => 'default'
        ,   'context'      => 'backend'
        ,   'replacements' => array ()
        ,   'post_type'    => array ( 'post' )
        );

        $this->params = array_merge( $defaults, $options );

        // When to add the filter
        $hook = 'backend' == $this->params['context'] 
            ? 'admin_head' : 'template_redirect';

        add_action( $hook, array ( $this, 'register_filter' ) );
    }

    /**
     * Conatiner for add_filter()
     * @return void
     */
    public function register_filter()
    {
        add_filter( 'gettext', array ( $this, 'translate' ), 10, 3 );
    }

    /**
     * The real working code.
     * 
     * @param  string $translated
     * @param  string $original
     * @param  string $domain
     * @return string
     */
    public function translate( $translated, $original, $domain )
    {
        // exit early
        if ( 'backend' == $this->params['context'] )
        {
            global $post_type;

            if ( ! empty ( $post_type ) 
                && ! in_array( $post_type, $this->params['post_type'] ) )
            {
                return $translated;
            }
        }

        if ( $this->params['domain'] !== $domain )
        {
            return $translated;
        }

        // Finally replace
        return strtr( $original, $this->params['replacements'] );
    }
}

// Sample code
// Replace 'Publish' with 'Save' and 'Preview' with 'Lurk' on pages and posts
$Toscho_Retrans = new Toscho_Retrans(
    array (
        'replacements' => array ( 
            'Publish' => 'Save'
        ,   'Preview' => 'Lurk' 
        )
    ,   'post_type'    => array ( 'page', 'post' )
    )
);

You don't need to use the code as a plugin. Including it in your theme's functions.php will be enough.

Update

To remove the original Save button (not sure what the 'draft' button is), add the following code to your functions.php/a plugin:

add_action( 'admin_print_footer_scripts', 'remove_save_button' );
function remove_save_button()
{   
?>
<script>
jQuery(document).ready(function($){$('#save-post').remove();});
</script><?php
}

Yes, it's ugly.

share|improve this answer
1  
That is more like version 20 :) Awesome code! –  Rarst Nov 5 '10 at 8:22
    
I like it as well. Still missing to hide the other button. –  hakre Nov 6 '10 at 18:49
    
Removing the save button is really hard, because post_submit_meta_box() just dumps its content out. No filter at all. Javascript or ob_start() could solve that. Both not very elegant. –  toscho Nov 6 '10 at 21:21
    
Hm, okay, I've added some code to remove the original Save button. Was not so hard. :) –  toscho Nov 6 '10 at 21:55
    
I miss words to describe this :) - & said it before: the use of the comma to format the array deserves a PHP Formatters Award +1+ –  brasofilo May 17 '12 at 14:45
add comment

The code for hakre's suggestion to use translation filter would be something like this:

add_filter( 'gettext', 'change_publish_button', 10, 2 );

function change_publish_button( $translation, $text ) {

if ( $text == 'Publish' )
    return 'Save';

return $translation;
}
share|improve this answer
add comment

This is not a full answer but some directions: Any text displayed surpasses a translation filter and can therefore be changed in a callback function (hook). So if the hook is only registered on the page where you would like to change that, job done.

The draft button could be "removed" by hiding it via CSS. The CSS could be injected into the <head>-tag of the admin with another callback. I think the filter is called admin_head for that. It's somehow dirty as the button still is there, for example if a user switches CSS off.

share|improve this answer
add comment

Another approach would be unregister the metabox, then re-register that same box with differing values, namingly the publish text..

See the discussion i had here regarding moving a meta box, you should be able to apply something similar to rename that Publish text.

share|improve this answer
add comment

You could just find the button via jQuery and swap the text node for something else. That would be a piece of cake to do.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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