如何为新的自定义帖子状态添加方便的按钮

时间:2019-03-24 作者:Martin

我在函数中通过此代码添加新的post状态:

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\' );
如何添加:1。管理面板中编辑后页面(如更新或预览)中的便利按钮enter image description here

在管理面板的帖子列表中添加“发送到垃圾邮件”选项enter image description here

1 个回复
SO网友:Debbie Kurth

有一个工作工具集可以很好地用于这种附加组件,称为CMB2。您可以将所需的所有字段和按钮添加为metabox系列的一部分。元盒是wordpress管理部分的组成部分。

https://wordpress.org/plugins/cmb2/

例如片段:

 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
}

这里可以看到更多
https://github.com/CMB2/CMB2/wiki/Basic-Usage#create-a-metabox

相关推荐

admin-ajax.php mixed content

无法从https访问admin ajax。在我的js中,我写道$.ajax({ url: \'https://\'+window.location.host+\'/admin/admin-ajax.php\', type:\'post\', data:\'action=bid_now_live_me&_pid=\'+ mypid , success: function (data) { 所以我把每个url都改成了http