THÊM NÚT NÚT CẬP NHẬT VÀO TRÌNH SỬA BÀI VIẾT

by daiphucminh

Thêm nút “CẬP NHẬT” cho tất cả khối góc trên bên phải của phần thêm và sửa bài viết riêng trong trình soạn thảo cổ điển.

Do Gemini tạo – chạy ổn


/* ==========================================================================
   THÊM NÚT XUẤT BẢN / CẬP NHẬT RIÊNG (BẢN V5 - SỬA LỖI ĐƠ NÚT, BẤM 1 PHÁT ĂN NGAY)
   -------------------------------------------------------------------------- */
   
function dpm_add_publish_update_box_v2( $post_type, $post ) {

    if ( ! $post instanceof WP_Post ) {
        return;
    }

    if ( ! current_user_can( 'edit_post', $post->ID ) ) {
        return;
    }

    add_meta_box(
        'dpm_publish_update_box_v2',
        'Xuất bản nhanh',
        'dpm_render_publish_update_box_v2',
        $post_type,
        'side',
        'high'
    );
}
add_action( 'add_meta_boxes', 'dpm_add_publish_update_box_v2', 10, 2 );


/**
 * Hiển thị nút mới và kích hoạt lệnh gửi dữ liệu chuẩn xác không bị đơ.
 */
function dpm_render_publish_update_box_v2( $post ) {

    if ( ! $post instanceof WP_Post ) {
        return;
    }

    if ( ! current_user_can( 'edit_post', $post->ID ) ) {
        return;
    }

    $published_statuses = array(
        'publish',
        'private',
        'future'
    );

    $button_text = in_array( $post->post_status, $published_statuses, true ) ? 'Cập nhật' : 'Xuất bản';

    echo '<button type="button" id="dpm_publish_update_button_v2" class="button button-primary button-large" style="width:100%;min-height:44px;font-size:16px;font-weight:600;">' . esc_html( $button_text ) . '</button>';

    ?>
    <script type="text/javascript">
        jQuery(document).ready(function($) {
            $(document).on('click', '#dpm_publish_update_button_v2', function(e) {
                e.preventDefault();

                // 1. Ép đồng bộ toàn bộ nội dung từ khung TinyMCE vào ô văn bản gốc ngay lập tức
                if (typeof tinyMCE !== 'undefined') {
                    tinyMCE.triggerSave();
                }

                // 2. Gỡ bỏ bộ đếm cảnh báo rời trang của trình duyệt
                $(window).off('beforeunload.edit-post');

                // 3. Tìm nút gốc và biểu mẫu
                var $originalBtn = $('#publish');
                var $form = $('#post');

                if ($originalBtn.length > 0 && $form.length > 0) {
                    // Tạo một thẻ input ẩn để hệ thống nhận diện đúng hành động lưu
                    if ($('#dpm_fake_submit_param').length === 0) {
                        $form.append('<input type="hidden" id="dpm_fake_submit_param" name="' + $originalBtn.attr('name') + '" value="' + $originalBtn.attr('value') + '" />');
                    }
                    
                    // Ép biểu mẫu gửi dữ liệu đi luôn, không thông qua hàm click trung gian để tránh bị chặn lần đầu
                    $form.submit();
                }
            });
        });
    </script>
    <?php
}


/**
 * Ẩn tiêu đề và viền hộp, chỉ giữ lại nút.
 */
function dpm_publish_update_box_css_v2() {

    $screen = get_current_screen();

    if ( ! $screen || 'post' !== $screen->base ) {
        return;
    }
    ?>
    <style>
        #dpm_publish_update_box_v2 {
            border: 0 !important;
            box-shadow: none !important;
            background: transparent !important;
            margin-bottom: 0 !important;
        }

        #dpm_publish_update_box_v2 .postbox-header,
        #dpm_publish_update_box_v2 > h2,
        #dpm_publish_update_box_v2 > .handlediv {
            display: none !important;
        }

        #dpm_publish_update_box_v2 .inside {
            margin: 0 !important;
            padding: 0 !important;
        }

        #dpm_publish_update_button_v2 {
            width: 100% !important;
            min-height: 44px !important;
            margin: 0 !important;
            cursor: pointer !important;
        }
    </style>
    <?php
}
add_action( 'admin_head-post.php', 'dpm_publish_update_box_css_v2' );
add_action( 'admin_head-post-new.php', 'dpm_publish_update_box_css_v2' );

Do ChatGPT tạo thỉnh thoảng có cảnh báo



/* ==========================================================================
   THÊM NÚT XUẤT BẢN / CẬP NHẬT RIÊNG
   --------------------------------------------------------------------------
 * Thêm nút "CẬP NHẬT" cho tất cả khối góc trên bên phải của phần thêm và sửa bài viết riêng trong trình soạn thảo cổ điển. Thêm hộp nút riêng vào cột bên phải.
   ========================================================================== */
   
function dpm_add_publish_update_box_v2( $post_type, $post ) {

    if ( ! $post instanceof WP_Post ) {
        return;
    }

    if ( ! current_user_can( 'edit_post', $post->ID ) ) {
        return;
    }

    add_meta_box(
        'dpm_publish_update_box_v2',
        'Xuất bản nhanh',
        'dpm_render_publish_update_box_v2',
        $post_type,
        'side',
        'high'
    );
}
add_action( 'add_meta_boxes', 'dpm_add_publish_update_box_v2', 10, 2 );


/**
 * Hiển thị nút.
 *
 * Bài mới, bản nháp: Xuất bản.
 * Bài đã đăng: Cập nhật.
 */
function dpm_render_publish_update_box_v2( $post ) {

    if ( ! $post instanceof WP_Post ) {
        return;
    }

    if ( ! current_user_can( 'edit_post', $post->ID ) ) {
        return;
    }

    $published_statuses = array(
        'publish',
        'private',
        'future'
    );

    if ( in_array( $post->post_status, $published_statuses, true ) ) {

        submit_button(
            'Cập nhật',
            'primary large',
            'save',
            false,
            array(
                'id'    => 'dpm_publish_update_button_v2',
                'style' => 'width:100%;min-height:44px;font-size:16px;font-weight:600;'
            )
        );

    } else {

        submit_button(
            'Xuất bản',
            'primary large',
            'publish',
            false,
            array(
                'id'    => 'dpm_publish_update_button_v2',
                'style' => 'width:100%;min-height:44px;font-size:16px;font-weight:600;'
            )
        );
    }
}


/**
 * Ẩn tiêu đề và viền hộp, chỉ giữ lại nút.
 */
function dpm_publish_update_box_css_v2() {

    $screen = get_current_screen();

    if ( ! $screen || 'post' !== $screen->base ) {
        return;
    }
    ?>
    <style>
        #dpm_publish_update_box_v2 {
            border: 0 !important;
            box-shadow: none !important;
            background: transparent !important;
        }

        #dpm_publish_update_box_v2 .postbox-header,
        #dpm_publish_update_box_v2 > h2,
        #dpm_publish_update_box_v2 > .handlediv {
            display: none !important;
        }

        #dpm_publish_update_box_v2 .inside {
            margin: 0 !important;
            padding: 0 !important;
        }

        #dpm_publish_update_button_v2 {
            width: 100% !important;
            min-height: 44px !important;
            margin: 0 !important;
        }
    </style>
    <?php
}
add_action( 'admin_head-post.php', 'dpm_publish_update_box_css_v2' );
add_action( 'admin_head-post-new.php', 'dpm_publish_update_box_css_v2' );

Related Posts

Leave a Comment

-
00:00
00:00
Update Required Flash plugin
-
00:00
00:00