How to Change Publish Metabox Text for Custom Post Type in WordPress

Home » Snippets » How to Change Publish Metabox Text for Custom Post Type in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you using custom post types in WordPress and want to change the text in the publish metabox? The publish metabox is where you can schedule or immediately publish a post, but the default text may not always be suitable for your specific content. In this article, we will guide you through the process of customizing the publish metabox text for your custom post types, allowing you to provide clearer instructions or more relevant options to your users.

					function wpturbo_change_publish_meta_box() {
    global $post;
    $post_type = $post->post_type;
    if ($post_type == 'your_custom_post_type') {
        $labels = get_post_type_labels(get_post_type_object($post_type));
        $publish_label = $labels->publish_item;
        $new_label = 'Custom Publish Text';
        if ($publish_label) {
            $new_label = str_replace('Publish', $new_label, $publish_label);
        }
        add_meta_box(
            'submitdiv',
            $new_label,
            'post_submit_meta_box',
            null,
            'side',
            'high'
        );
    }
}
add_action('admin_menu', 'wpturbo_change_publish_meta_box');
				

The goal of this code snippet is to change the text displayed on the Publish metabox for a custom post type in WordPress. This can be useful if you want to customize the label according to your specific requirements.

First, we define a function called wpturbo_change_publish_meta_box(). This function will be responsible for changing the text on the Publish metabox.

Inside the function, we use the global $post variable to get the current post object. We then retrieve the post type of the current post and store it in the $post_type variable.

Next, we check if the $post_type is equal to the custom post type for which we want to change the publish text. In this case, it is ‘your_custom_post_type’. You should replace this with the actual name of your custom post type.

We then use the get_post_type_labels() function to retrieve the labels for the specified post type. This function returns an object that contains various labels for the post type, such as "publish_item".

We assign the value of $labels->publish_item to the $publish_label variable. This represents the current text displayed on the Publish metabox.

Next, we define a new label by assigning the value ‘Custom Publish Text’ to the $new_label variable. This is the text that we want to replace the original publish text with. You can replace this with your desired custom text.

To ensure that we don’t change the label if the original label does not contain the word "Publish", we perform a check using an if statement. If the $publish_label variable exists, we use the str_replace() function to replace any occurrence of "Publish" in the $publish_label with the $new_label.

Finally, we use the add_meta_box() function to add a new metabox with the updated label. The add_meta_box() function takes several parameters including the id of the metabox, the new label we want to display, the callback function to render the metabox content (‘post_submit_meta_box’), and other optional parameters such as the location and priority.

In this case, we replace the existing Publish metabox with our updated label by using the same id (‘submitdiv’), the new label ($new_label), and the default callback function (‘post_submit_meta_box’). We also set the location to ‘side’ and the priority to ‘high’.

To ensure that the function is executed at the appropriate time, we hook it to the ‘admin_menu’ action using the add_action() function. This ensures that the function is executed when the admin menu is being created.

Remember to replace ‘wpturbo_change_publish_meta_box’ with your own unique function name to avoid conflicts.

That’s it! With this code snippet, you can easily change the text displayed on the Publish metabox for your custom post type in WordPress.

Register an account to save your snippets or go Pro to get more features.