2

In my plugin, I have a custom post type with a specific behaviour.

The administrator after to publish it, he should not be able to set the post as draft again.

How can I do this ?

1

I would recommend the transition_post_status hook, which allows you to hook into when a post is transitioning (or anytime a post is saved). In the example below, we check if the post is apart of your custom post type. Then we see if the old status was published and if the new status is not published, we throw an error.

<?php 
add_action( 'transition_post_status', 'tps_no_draft_after_publish', 10, 3 );
function tps_no_draft_after_publish( $new_status, $old_status, $post ) {
    if ( ( 'publish' !== $new_status && 'publish' === $old_status ) && 'my-custom-post-type' === $post->post_type
    ) {
        wp_die('Posts that have been published cannot be set as draft.');
    }
}

Additional reading:

| improve this answer | |
1

I was looking for a solution to display the submit box wihtout draft button.

The code to display this box is generated by the function post_submit_meta_box from wp-admin/includes/meta-boxes.php.

As we can see, there is no hook to overwrite some code, so this following solution step by step is the best way I found without using a dirty solution like hide with css or js :

1 - remove the metabox "submitdiv"

public function remove_customized_metabox(){    
    remove_meta_box( 'submitdiv', $post_type_slug, 'side' ); 
}
add_action( 'add_meta_boxes', "remove_customized_metabox", 10 );

2 - add my "submitdiv" customized metabox

You must use a different id than submitdiv to show your metabox
In my case I prefixed with underscore : _submitdiv

add_meta_box( 
    "_submitdiv", 
    __( "Publish" ), 
    "my_custom_callback", 
    $post_type_slug, 
    'side', 
    'core', 
    [ 'show_draft_button' => false ] 
);

3 - write the view in the callback function

Based on post_submit_meta_box, write your callback function like this :

public function my_custom_callback(?\WP_Post $post, array $callback_args){
    global $action;

    $post_type = $post->post_type;
    $post_type_object = get_post_type_object($post_type);
    $can_publish = current_user_can($post_type_object->cap->publish_posts);
    ?>
    <div class="submitbox" id="submitpost">
    ...
    if( $callback_args['show_draft_button'] ) {
       //display the draft button
    }

    ...
}

4 - change _submitdiv by submitdiv with jquery

if you want to change the publish date, post.js use the anchor #submitdiv so you must change the id.

wp_enqueue_style( "metaboxsubmitstyle", "path/css/admin-metabox-submit.css" );
wp_enqueue_script( "metaboxsubmitscript", "path/js/admin-metabox-submit.js" );

admin-metabox-submit.css

#poststuff #_submitdiv .inside {
    margin: 0;
    padding: 0;
} 

admin-metabox-submit.js

jQuery(function ($) {
    $(document).ready(function () {
        $("#_submitdiv").attr( 'id', "submitdiv");
    });
});
| 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.