最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论