Wordpress - Add post status programmatically | Wordpress - Thêm Post Status bằng code

Post Reply
tthlan
Quản trị viên
Posts: 76
Joined: Tue Aug 23, 2016 8:13 am

Wordpress - Add post status programmatically | Wordpress - Thêm Post Status bằng code

Post by tthlan »

1. Register Post Status
Đăng ký post_status là Archive cho post_type là 'your_post_type' mà thôi.
Chú ý: Show_in_admin_status_list phải set thì mới có filter trên Data Table của post_type

Code: Select all

add_action( 'init', array( $this,'create_initial_post_types') );

        /**
         * Register Archived post status of Course post type
         * to display filter by post_status link on the bootom of posts list #offy
         *
         */
        public function create_initial_post_types() {

            if ( 'your_post_type' !== $_REQUEST['post_type']) return; // your_post_type

            $localized_text['archived'] = __( 'Archived');

            register_post_status(
                'archive',
                array(
                    'label'       => $localized_text['archived'],
                    'show_in_admin_status_list'      => true,
                    'label_count' => _n_noop( $localized_text['archived'] .' <span class="count">(%s)</span>', $localized_text['archived'] .' <span class="count">(%s)</span>' ),
                )
            );
        }
Image

2. Adjust Bulk options when the list data filter as Publish show Archive bulk action/filter by Archive show Publish bulk option
Khi lọc theo Publish thì hiển thị 'Archive' trong Bulk Action Options
Khi lọc theo Archive thì hiển thị 'Publish' trong Bulk Action Options

Code: Select all

add_filter( 'handle_bulk_actions-edit-course', array( $this,'my_archive_action_handler'), 10, 3);

        /**
         * Add [post_type] Archive item on Bulk Actions options
         *
         */
        public function my_archive_action_handler($bulk_actions) {

            if ( isset( $_REQUEST['post_status'] ) && in_array( $_REQUEST['post_status'], array( 'trash', 'untrash', 'draft' ) ) ) {
                return $bulk_actions;
            }

            if (isset($_REQUEST['post_status']) && $_REQUEST['post_status'] === 'publish'){
                $bulk_actions['archive'] = __( 'Archive','woothemes-sensei' );
            }
            if (isset($_REQUEST['post_status']) && $_REQUEST['post_status'] === 'archive'){
                $bulk_actions['publish'] = __( 'Publish','woothemes-sensei' );
            }

            return $bulk_actions;
        }
3. Handler action bulk action
Khi submit (apply) của bulk action muốn xử lý gì ... ở đâu xử lý my_update_post_status
Hàm này output là redirect_to về trang hiện tại kèm query arg sẽ dùng trong 'admin_notice'

Code: Select all

        
	add_filter( 'handle_bulk_actions-edit-[post_type]', array( $this,'my_archive_action_handler'), 10, 3);
        /**
         * Action Handler of selected Archive item on the Course's bulk actions #offy
         *
         * @param string  $redirect_to
         * @param string  $action_name
         * @param array   $post_ids
         *
         */
        function my_archive_action_handler( $redirect_to, $action_name, $post_ids ) {

            if ( 'archive' === $action_name || 'publish' === $action_name  ) {

                $archived_post_ids = array();

                foreach ( $post_ids as $post_id ) {
                    $course = get_post( $post_id );

                    if ($this->my_update_post_status($action_name,    get_post_status($course),$course)
                    ) {
                        $archived_post_ids[] = $post_id;
                    }
                }

                remove_query_arg('post_status');
                remove_query_arg('update_counted');
                remove_query_arg('page');

                $redirect_to = add_query_arg( 'post_status', $action_name, $redirect_to );
                $redirect_to = add_query_arg( 'update_counted',    count($archived_post_ids), $redirect_to );

                return $redirect_to;
            }
            else
                return $redirect_to;

        }
Image
4. Display admin notices when applying archive bulk action
Thông báo khi thực hiện Archive bulk action xong

Code: Select all

	add_action( 'admin_notices',  array( $this,'my_archive_admin_notice' ));
        /**
         * Custom Post type 's Bulk Action admin notices
         *
         */
        function my_archive_admin_notice() {

            if ( isset( $_REQUEST['post_status'] ) && ( isset(  $_REQUEST['update_counted'] )))
            {
                $courses_count = intval( $_REQUEST['update_counted'] );

                echo '<div id="message" class="' . ( $courses_count > 0 ? 'updated' : 'error' ) . '">';

                if ( $courses_count > 0 ) {
                    echo '<p>' . __( ucfirst($_REQUEST['post_status']) . ' success for ' ) . $courses_count .' '. _n( 'course', 'courses', $courses_count, 'wordpress' ).'!' . '</p>';
                } else {
                    if ($_REQUEST['post_status'] === 'publish')
                        echo '<p>' . __( 'No Course was published!', 'wordpress' ) . '</p>';
                    if ($_REQUEST['post_status'] === 'archive')
                        echo '<p>' . __( 'No Course was archived!', 'wordpress' ) . '</p>';
                }

                echo '</div>';
            }
Image
Post Reply