Comment ajouter un nouveau post status sur WordPress ?

Comment ajouter un nouveau post status sur WordPress ?

 

Vous pouvez ajouter un nouveau post status sur WordPress en copiant et collant cet extrait de code dans votre code source :

<?php
/**
 *  PIT - Post status
 *  - Register your "custom post status"
 */
add_action('init', '_pit_post_status_devis', 1);
function _pit_post_status_devis() {
    /**
     *  Change those variables
     */
    $post_status_slug  = 'devis';
    $post_status_label = _x('Devis', 'post');
    register_post_status(
        $post_status_slug, array(
            'label'                     => $post_status_label,
            'public'                    => false,
            'exclude_from_search'       => false,
            'show_in_admin_all_list'    => true,
            'show_in_admin_status_list' => true,
            'label_count'               => _n_noop($post_status_label . ' <span class="count">(%s)</span>', $post_status_label . ' <span class="count">(%s)</span>'),
        )
    );
}
/**
 *  PIT - Post status
 *  - Add your "custom post status" in post status admin list using JS.
 */
add_action('admin_footer-post.php', '_pit_post_status_devis_js', 1);
add_action('admin_footer-edit.php', '_pit_post_status_devis_js', 1);
function _pit_post_status_devis_js() {
    global $post;
    $selected          = '';
    $label             = '';
    $is_current_status = 0;
    /**
     *  Change those variables
     */
    $post_type        = 'precommande';
    $post_status_slug = 'devis';
    if ($post->post_type !== $post_type) {
        return;
    }
    $post_status_object = get_post_status_object($post_status_slug);
    $status_label       = $post_status_object->label;
    $label              = '<span id="post-status-display"> ' . $status_label . '</span>';
    // If it's using currently our post status
    if ($post->post_status === $post_status_slug) {
        $is_current_status = 1;
        $selected          = ' selected="selected"';
    }
    ?>
    <script>
        (function($) {
            // Post.php
            $('select#post_status').append('<option value="<?php echo $post_status_slug; ?>"<?php echo $selected; ?>><?php echo $status_label; ?></option>');
            // Edit.php
            $('.inline-edit-status select').append('<option value="<?php echo $post_status_slug; ?>"<?php echo $selected; ?>><?php echo $status_label; ?></option>');
            var isCurrentStatus = <?php echo $is_current_status; ?>;
            if (isCurrentStatus) {
                $('.misc-pub-section label').append('<?php echo $label; ?>');
                $('#post-status-display').append('<?php echo $status_label; ?>');
            }
        })(jQuery);
    </script>
    <?php
}

/**
 *  PIT - Post status
 *  - Force to use our "custom post status" instead of "publish" when updating the post.
 */
add_action('transition_post_status', '_pit_post_status_devis_save', 10, 3);
function _pit_post_status_devis_save($new_status, $old_status, $post) {

    /** Change this variable */
    $custom_post_status = 'devis';

    /** Only target post that "already" use our "custom post status". */
    if ($custom_post_status === $old_status && $new_status !== 'publish') {
        return;
    }

    $new_status = $custom_post_status;

    /** Force update the post with our "custom post status" */
    wp_update_post(
        array(
            'ID'            => $post->ID,
            'post_status'   => $new_status,
        )
    ); 
}

 

Déclinaison de logo : son importance dans la charte graphique

Site de police d’écriture : notre sélection pour le digital !

WordPress vs Symfony : lequel choisir pour mon projet ?