1

I am trying to make one specific page on my wordpress installation to have a different text on the "Upload" button.

So i tried:

function change_settingspage_publish_button( $translation, $text ) {

    if ( '567' == get_the_ID() && $text == 'Publish' ) {

        return 'Save Settings';

    } else {

        return $translation;
    }
}
add_filter( 'gettext', 'change_settingspage_publish_button', 10, 2 );

(Where '567' is the page id). but it doesn't work. any ideas?

0

You can tried this code below, it works for me.

function change_publish_button( $translation, $text ) {
if ( 'CUSTOM_POST_TYPE' == get_post_type() && ($text == 'Publish' || $text == 'Update') ) {
    return 'Save';
 } else {
    return $translation;
 }
}
|improve this answer|||||
  • That doesn't change it for a page id as i asked, but for specific post type. read the question again. – mondi Jul 20 '17 at 7:12
  • @mondi if you look into the code you can easily change it for a page id where you see ''CUSTOM_POST_TYPE'' == get_post_type() declare like this if($page = get_page_by_path( 'about' ) – Nuno Sarmento Jul 21 '17 at 9:08
0

You can use something like this if you'd like to target a specific post/page ID.

add_action('admin_notices', 'check_id_before_text_change_wpse_273920');

function check_id_before_text_change_wpse_273920() {
    global $post;
    //if post isn't set bail
    if(null == $post)
        return;

    $id = $post->ID;
    //add our filter if the id matches
    if($id == '567') {
        add_filter('gettext', 'change_publish_text_wpse_273920', 30, 2);
    }
}

function change_publish_text_wpse_273920($translation, $text) {
    //if this runs and publish isn't in the text
    //we still want to return something
    $rtn_text = $translation;

    if($text === 'Publish') {
        $rtn_text = 'Save setting';
    }

 return $rtn_text;
}
|improve this answer|||||
  • Thanks, but it doesn't seem to change nothing. – mondi Jul 20 '17 at 7:17
  • What is the ID of the page you are on? Also what is the current status? I've tested this on a site of my own so I know it works – mrmadhat Jul 20 '17 at 7:51
  • the ID is 784 (of course i changed it in the code) and the status is publish. weird. why can it be? – mondi Jul 20 '17 at 7:54
  • Where are you adding the code? – mrmadhat Jul 20 '17 at 18:32
  • Also try var_dump($post); die(); before the if(null == $post) to see if that produces a post object, if it does try var_dump($post->ID) to confirm you are using the correct ID in the if statement. – mrmadhat Jul 20 '17 at 18:39
0

I wanted to change the Publish button's text in the Block Editor and came across this question. With the answers given it wasn't changing the text. With the Gutenberg update, I realized it would have to be done with JavaScript. I found this response: Changing text within the Block Editor

Which I applied in my plugin code (you can insert this into your Themes functions.php file, too).

function change_publish_button_text() {
// Make sure the `wp-i18n` has been "done".
  if ( wp_script_is( 'wp-i18n' ) ) :
  ?>
    <script>
      wp.i18n.setLocaleData({'Publish': ['Custom Publish Text']});
    </script>

    <script>
      wp.i18n.setLocaleData({'Publish…': ['Custom Publish Text']});
    </script>
  <?php
  endif;
}

add_action( 'admin_print_footer_scripts', 'change_publish_button_text', 11 );

Which to your specific request to target only on a post ID you can do this:

function change_publish_button_text() {
  global $post;
  if (get_the_ID() == '123') {
    // Make sure the `wp-i18n` has been "done".
    if ( wp_script_is( 'wp-i18n' ) ) :
      ?>
        <script>
          wp.i18n.setLocaleData({'Publish': ['Custom Publish Text']});
        </script>

        <script>
          wp.i18n.setLocaleData({'Publish…': ['Custom Publish Text']});
        </script>
      <?php
      endif;
    }
  }

add_action( 'admin_print_footer_scripts', 'change_publish_button_text', 11 );
|improve this answer|||||

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.