最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

customization - How to add convenient buttons for new custom post statuses

matteradmin6PV0评论

I add new post status via this code in functions:

function custom_post_status() {
    register_post_status( 'spam', array(
        'label' => _x( 'Spam', 'post' ),
        'public' => false,
        'exclude_from_search' => true,
        'show_in_admin_all_list' => true,
        'show_in_admin_status_list' => true,
        'label_count' => _n_noop( 'SPAM <span class="count">(%s)</span>', 'SPAM <span class="count">(%s)</span>' ),
    ) );
}

function append_post_status_list() {
    global $post;
    $complete = '';
    $label = '';
    if( $post->post_type == 'post' ) {
        if( $post->post_status == 'spam' ) {
            $complete = ' selected="selected"';
            $label = '<span id="post-status-display">SPAM</span>';
        }

        echo '<script>
            jQuery(document).ready(function($){
                $("select#post_status").append("<option value=\"spam\" '.$complete.'>SPAM</option>");
                $(".misc-pub-section label").append("'.$label.'");
            });
            </script>';
    }
}

add_action( 'init', 'custom_post_status' );
add_action( 'admin_footer-post.php', 'append_post_status_list' );

How to add: 1. Convinient button in post editing page (like Update or Preview) in admin panel

  1. Add "Send to spam" option in post listing in admin panel

I add new post status via this code in functions:

function custom_post_status() {
    register_post_status( 'spam', array(
        'label' => _x( 'Spam', 'post' ),
        'public' => false,
        'exclude_from_search' => true,
        'show_in_admin_all_list' => true,
        'show_in_admin_status_list' => true,
        'label_count' => _n_noop( 'SPAM <span class="count">(%s)</span>', 'SPAM <span class="count">(%s)</span>' ),
    ) );
}

function append_post_status_list() {
    global $post;
    $complete = '';
    $label = '';
    if( $post->post_type == 'post' ) {
        if( $post->post_status == 'spam' ) {
            $complete = ' selected="selected"';
            $label = '<span id="post-status-display">SPAM</span>';
        }

        echo '<script>
            jQuery(document).ready(function($){
                $("select#post_status").append("<option value=\"spam\" '.$complete.'>SPAM</option>");
                $(".misc-pub-section label").append("'.$label.'");
            });
            </script>';
    }
}

add_action( 'init', 'custom_post_status' );
add_action( 'admin_footer-post.php', 'append_post_status_list' );

How to add: 1. Convinient button in post editing page (like Update or Preview) in admin panel

  1. Add "Send to spam" option in post listing in admin panel
Share Improve this question asked Mar 24, 2019 at 10:38 MartinMartin 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

There is a work tool set that works nice for this kind of add on, called CMB2. You can add all the fields and buttons you want as part of a metabox series. Metaboxes are what make up the admin section section of wordpress.

https://wordpress/plugins/cmb2/

For example..a snippet:

 function custom_post_status() {
          register_post_status( 'spam', array(
         'label' => _x( 'Spam', 'post' ),
         'public' => false,
         'exclude_from_search' => true,
         'show_in_admin_all_list' => true,
         'show_in_admin_status_list' => true,
         'label_count' => _n_noop( 'SPAM <span class="count">(%s)</span>', 
 'SPAM <span class="count">(%s)</span>' ),
     ) );
 }



 add_action( 'cmb2_admin_init', 'cmb2_sample_metaboxes' );
 /**
  * Define the metabox and field configurations.
  */
 function cmb2_sample_metaboxes() {

// Start with an underscore to hide fields from custom fields list
$prefix = '_yourprefix_';

/**
 * Initiate the metabox
 */
$cmb = new_cmb2_box( array(
    'id'            => 'test_metabox',
    'title'         => __( 'Test Metabox', 'cmb2' ),
    'object_types'  => 'spam', '  // <<<==== YOUR POST STATUS NAME
    'context'       => 'normal',
    'priority'      => 'high',
    'show_names'    => true, // Show field names on the left
    // 'cmb_styles' => false, // false to disable the CMB stylesheet
    // 'closed'     => true, // Keep the metabox closed by default
) );

// Regular text field
$cmb->add_field( array(
    'name'       => __( 'Test Text', 'cmb2' ),
    'desc'       => __( 'field description (optional)', 'cmb2' ),
    'id'         => $prefix . 'text',
    'type'       => 'text',
    'show_on_cb' => 'cmb2_hide_if_no_cats', // function should return a bool value
    // 'sanitization_cb' => 'my_custom_sanitization', // custom sanitization callback parameter
    // 'escape_cb'       => 'my_custom_escaping',  // custom escaping callback parameter
    // 'on_front'        => false, // Optionally designate a field to wp-admin only
    // 'repeatable'      => true,
) );

// URL text field
$cmb->add_field( array(
    'name' => __( 'Website URL', 'cmb2' ),
    'desc' => __( 'field description (optional)', 'cmb2' ),
    'id'   => $prefix . 'url',
    'type' => 'text_url',
    // 'protocols' => array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet'), // Array of allowed protocols
    // 'repeatable' => true,
) );

// Email text field
$cmb->add_field( array(
    'name' => __( 'Test Text Email', 'cmb2' ),
    'desc' => __( 'field description (optional)', 'cmb2' ),
    'id'   => $prefix . 'email',
    'type' => 'text_email',
    // 'repeatable' => true,
) );

// Add other metaboxes as needed

}

More can be seen here.
https://github/CMB2/CMB2/wiki/Basic-Usage#create-a-metabox

Post a comment

comment list (0)

  1. No comments so far