/**
* PostStatusExtender
*
* @author Hyyan Abo Fakher<[email protected]>
*/
class PostStatusExtender
{
/**
* Extend
*
* Extend the current status list for the given post type
*
* @global \WP_POST $post
*
* @param string $postType the post type name , ex: product
* @param array $states array of states where key is the id(state id) and value
* is the state array
*/
public static function extend($postType, $states)
{
foreach ($states as $id => $state) {
register_post_status($id, $state);
}
add_action('admin_footer-post.php', function() use($postType, $states) {
global $post;
if (!$post || $post->post_type !== $postType) {
return false;
}
foreach ($states as $id => $state) {
printf(
'<script>'
. 'jQuery(document).ready(function($){'
. ' $("select#post_status").append("<option value=\"%s\" %s>%s</option>");'
. ' $("a.save-post-status").on("click",function(e){'
. ' e.preventDefault();'
. ' var value = $("select#post_status").val();'
. ' $("select#post_status").value = value;'
. ' $("select#post_status option").removeAttr("selected", true);'
. ' $("select#post_status option[value=\'"+value+"\']").attr("selected", true)'
. ' });'
. '});'
. '</script>'
, $id
, $post->post_status !== $id ? '' : 'selected=\"selected\"'
, $state['label']
);
if ($post->post_status === $id) {
printf(
'<script>'
. 'jQuery(document).ready(function($){'
. ' $(".misc-pub-section #post-status-display").text("%s");'
. '});'
. '</script>'
, $state['label']
);
}
}
});
add_action('admin_footer-edit.php', function() use($states, $postType) {
global $post;
if (!$post || $post->post_type !== $postType) {
return false;
}
foreach ($states as $id => $state) {
printf(
'<script>'
. 'jQuery(document).ready(function($){'
. " $('select[name=\"_status\"]').append('<option value=\"%s\">%s</option>');"
. '});'
. '</script>'
, $id
, $state['label']
);
}
});
add_filter('display_post_states', function($states, $post) use($states, $postType) {
foreach ($states as $id => $state) {
if ($post->post_type == $postType && $post->post_status === $id) {
return array($state['label']);
} else {
if (array_key_exists($id, $states)) {
unset($states[$id]);
}
}
}
return $states;
}, 10, 2);
}
}
Và đây làm thế nào để sử dụng nó
add_action('init', function() {
PostStatusExtender::extend(self::NAME, array(
'sold' => array(
'label' => __('Sold', 'viasit'),
'public' => true,
'exclude_from_search' => true,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('Sold <span class="count">(%s)</span>', 'Sold <span class="count">(%s)</span>'),
)
));
});
Edit: typo cố định trong mã.
Nguồn
2016-11-04 10:19:00
Các trạng thái này có ý nghĩa sâu sắc về cách các bài đăng được xử lý và hiển thị, phải không? Tôi không tưởng tượng bạn chỉ có thể thêm một cái mới vào một số danh sách. Bạn muốn thêm trạng thái mới nào? –
Tôi chỉ muốn thêm trạng thái mới vào các loại bài đăng tùy chỉnh của mình và với những bài đăng đó đã quản lý hiển thị bài đăng thông qua các truy vấn tùy chỉnh. Với wordpress bạn có thể truy vấn để hiển thị các bài viết tùy thuộc vào trạng thái của nó. Vì vậy, thêm một số trạng thái như Bán và Gỡ bỏ không nên là một vấn đề lớn đối với hệ thống? – Brady
@Brady Tôi hiểu. Tôi không biết liệu điều này có dễ dàng không. Điều gì về việc sử dụng hệ thống gắn thẻ/danh mục cho mục đích này? –