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

I add new post status via this code in functions:function custom_post_status() {register_post_status( 'spam',

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745661733a4638876.html

相关推荐

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

    I add new post status via this code in functions:function custom_post_status() {register_post_status( 'spam',

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信