Hướng dẫn duplicate page wordpress - trùng lặp trang wordpress

Th6 01, 2018

Hai G.

5ít nhất Đọc

Hướng dẫn duplicate page wordpress - trùng lặp trang wordpress

Có bao giờ bạn gặp trường hợp bạn cần làm mới lại trang hiện tại với thiết kế mới, nhưng vẫn muốn giữ nguyên bài viết để sử dụng sau? Dĩ nhiên, bạn có thể copy/paste rồi lưu toàn bộ trong file draft, nhưng bạn nhận ra nó sẽ không di chuyển SEO optimization, page templates, featured images và những thông tin liên quan của bạn? Nếu bạn muốn clone toàn bộ blog/page chỉ để tinh chỉnh rồi so sánh giữa 2 phiên bản thì sao? Có rất nhiều lý do khác nhau để bạn muốn nhân bản bài viết hoặc trang WordPress phải không nào? Và bạn hoàn toàn không muốn mất thời gian trong việc copy/paste đơn thuần.

Khi đó bạn cần duplicate post WordPress hoặc clone toàn bộ trang web. Trong bài hướng dẫn này, chúng tôi sẽ chỉ cách đơn giản nhất để nhân bản bài viết và page trong WordPress cho bạn. Chỉ mất vài cú click là bạn có thể clone bất kỳ bài viết nào hoặc trang nào theo ý thích, thậm chí là clone cả toàn bộ website của bạn.

Hãy bắt đầu thôi

  • Bạn cần gì?
  • Lựa chọn 1 – Nhân bản Post/Page với Plugin Duplicate Post
  • Lựa chọn 2 – Clone Page/Post với plugin Duplicate Page and Post
  • Lựa chọn 3 – Sử dụng Post Duplicator để duplicate post WordPress
  • Lựa chọn 4 – Clone Page hoặc Post mà không cần plugin duplicate post WordPress
  • Lời kết

Bạn cần gì?

Lựa chọn 1 – Nhân bản Post/Page với Plugin Duplicate Post

  • Lựa chọn 2 – Clone Page/Post với plugin Duplicate Page and Post

Lựa chọn 3 – Sử dụng Post Duplicator để duplicate post WordPress

Lựa chọn 4 – Clone Page hoặc Post mà không cần plugin duplicate post WordPress

Lời kết

  1. Trước khi bắt đầu bài hướng dẫn này, bạn cần chuẩn bị:
    Hướng dẫn duplicate page wordpress - trùng lặp trang wordpress
  2. Truy cập vào WordPress Admin PanelPages -> All Pages.
  3. Lựa chọn 1 – Nhân bản Post/Page với Plugin Duplicate PostCloneNew Draft.
    Hướng dẫn duplicate page wordpress - trùng lặp trang wordpress
    • Bạn có thể click vào nút Clone để tạo bản sao của bài post được chọn.
    • Plugin Duplicate Post là phương pháp tiện nhất nếu bạn muốn clone bài viết hoặc trang của bạn trong WordPress. Không chỉ nó hỗ trợ bạn nhân bản bài viết, nó còn cho phép bạn điều chỉnh thêm nhiều thông số riêng. Bạn có một suffix pre-defined để thêm vào trước hoặc sau title của page hoặc post được clone. Bạn cũng có thể giới hạn chức năng của plugin dựa trên quyền người sử dụng.New Draft để tạo một bản sao của bài post được chọn nhưng nó sẽ được mở lên trong post editor mới. Bạn có thể lưu nháp hoặc xuất bản nó sau khi save lại.

Để clone một bài viết hoặc trang bằng plugin này, thực hiện các bước bên dưới:

Cài đặt và kích hoạt plugin Duplicate Post.

Tiếp theo, nhấn vào nút Pages -> All Pages.

  1. Giờ hãy di chuyển đến bài viết bạn muốn duplicate (hay clone), sau đó bạn sẽ thấy 2 option mới ở đây – Clone và New Draft.Bạn có thể click vào nút Clone để tạo bản sao của bài post được chọn.
    Hướng dẫn duplicate page wordpress - trùng lặp trang wordpress
  2. Bạn có thể click vào nút New Draft để tạo một bản sao của bài post được chọn nhưng nó sẽ được mở lên trong post editor mới. Bạn có thể lưu nháp hoặc xuất bản nó sau khi save lại.Pages -> All Pages nếu bạn muốn nhân bản page hoặc Posts -> All Posts nếu bạn muốn nhân bản bài post.
  3. Lựa chọn 2 – Clone Page/Post với plugin Duplicate Page and PostClone Me.
    Hướng dẫn duplicate page wordpress - trùng lặp trang wordpress
  4. Duplicate Page and Post là một plugin để nhân bản khác có thể được dùng để clone page và post nhanh chóng. Plugin này giúp bạn tạo ra bản sao của page và post mà toàn bộ nội dung, title và styling của chúng vấn được giữ y nguyê.Publish hoặc Save Draft.
    Hướng dẫn duplicate page wordpress - trùng lặp trang wordpress

Sử dụng plugin này cũng đơn giản. Bạn chỉ cần làm các bước sau:

Cài đặt và kích hoạt plugin Duplicate Page and Post plugin.

Tiếp theo, tới phần Pages -> All Pages nếu bạn muốn nhân bản page hoặc Posts -> All Posts nếu bạn muốn nhân bản bài post.

  1. Di chuyển tới bài viết bạn muốn nhân bản và click vào nút Clone Me.
    Hướng dẫn duplicate page wordpress - trùng lặp trang wordpress
  2. Bạn sẽ được chuyển tới bản copy của bài viết hoặc page. Sửa nó và click vào nút Publish hoặc Save Draft.Duplicate Post/Duplicate Page để tạo một bản clone.
    Hướng dẫn duplicate page wordpress - trùng lặp trang wordpress
  3. Lựa chọn 3 – Sử dụng Post Duplicator để duplicate post WordPress
    Hướng dẫn duplicate page wordpress - trùng lặp trang wordpress

Nếu 2 plugin trên không hợp với yêu cầu của bạn, bạn có thể chọn Post Duplicator. Plugin duplicate post và page WordPress này sẽ tạo bản sao chính xác của bài viết được chọn, giữ lại tất cả các trường custom và taxonomies.

Tất cả nhữn gì bạn cần làm là:

Cài đặt và kích hoạt plugin Post Duplicator

/*
 * Function for post duplication. Dups appear as drafts. User is redirected to the edit screen
 */
function rd_duplicate_post_as_draft(){
  global $wpdb;
  if (! ( isset( $_GET['post']) || isset( $_POST['post'])  || ( isset($_REQUEST['action']) && 'rd_duplicate_post_as_draft' == $_REQUEST['action'] ) ) ) {
    wp_die('No post to duplicate has been supplied!');
  }
 
  /*
   * Nonce verification
   */
  if ( !isset( $_GET['duplicate_nonce'] ) || !wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) )
    return;
 
  /*
   * get the original post id
   */
  $post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
  /*
   * and all the original post data then
   */
  $post = get_post( $post_id );
 
  /*
   * if you don't want current user to be the new post author,
   * then change next couple of lines to this: $new_post_author = $post->post_author;
   */
  $current_user = wp_get_current_user();
  $new_post_author = $current_user->ID;
 
  /*
   * if post data exists, create the post duplicate
   */
  if (isset( $post ) && $post != null) {
 
    /*
     * new post data array
     */
    $args = array(
      'comment_status' => $post->comment_status,
      'ping_status'    => $post->ping_status,
      'post_author'    => $new_post_author,
      'post_content'   => $post->post_content,
      'post_excerpt'   => $post->post_excerpt,
      'post_name'      => $post->post_name,
      'post_parent'    => $post->post_parent,
      'post_password'  => $post->post_password,
      'post_status'    => 'draft',
      'post_title'     => $post->post_title,
      'post_type'      => $post->post_type,
      'to_ping'        => $post->to_ping,
      'menu_order'     => $post->menu_order
    );
 
    /*
     * insert the post by wp_insert_post() function
     */
    $new_post_id = wp_insert_post( $args );
 
    /*
     * get all current post terms ad set them to the new post draft
     */
    $taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
    foreach ($taxonomies as $taxonomy) {
      $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
      wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
    }
 
    /*
     * duplicate all post meta just in two SQL queries
     */
    $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
    if (count($post_meta_infos)!=0) {
      $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
      foreach ($post_meta_infos as $meta_info) {
        $meta_key = $meta_info->meta_key;
        if( $meta_key == '_wp_old_slug' ) continue;
        $meta_value = addslashes($meta_info->meta_value);
        $sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
      }
      $sql_query.= implode(" UNION ALL ", $sql_query_sel);
      $wpdb->query($sql_query);
    }
 
 
    /*
     * finally, redirect to the edit post screen for the new draft
     */
    wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
    exit;
  } else {
    wp_die('Post creation failed, could not find original post: ' . $post_id);
  }
}
add_action( 'admin_action_rd_duplicate_post_as_draft', 'rd_duplicate_post_as_draft' );
 
/*
 * Add the duplicate link to action list for post_row_actions
 */
function rd_duplicate_post_link( $actions, $post ) {
  if (current_user_can('edit_posts')) {
    $actions['duplicate'] = 'Duplicate';
  }
  return $actions;
}
 
add_filter( 'post_row_actions', 'rd_duplicate_post_link', 10, 2 );

Di chuyển tới post hoặc trang bạn muốn duplicate và click vào nút Duplicate Post/Duplicate Page để tạo một bản clone.

add_filter('page_row_actions', 'rd_duplicate_post_link', 10, 2);

Plugin sẽ ngay lập tức duplicate post hoặc page.functions.php. Để làm vậy, bạn có thể dùng File Manager, FTP client hoặc dùng WordPress file editor có sẵn:

Hướng dẫn duplicate page wordpress - trùng lặp trang wordpress

Lựa chọn 4 – Clone Page hoặc Post mà không cần plugin duplicate post WordPressAll Posts hoặc All Pages bạn sẽ thấy nút Duplicate.

Hướng dẫn duplicate page wordpress - trùng lặp trang wordpress

Ghi chú: Bạn cần phải backup website trước khi chỉnh sửa file gốc của WordPress.

Lời kết

Trước khi bắt đầu bài hướng dẫn này, bạn cần chuẩn bị:

Có bất kỳ câu hỏi hoặc thủ thuật nào mới. Hãy chia sẽ với chúng tôi trong phần bình luận bên dưới nhé.

Để biết thêm bài viết mới nhất về WordPress, đừng quên truy cập vào trang Hướng Dẫn WordPress của chúng tôi, và tìm tới trang cuối cùng nha!

Hải G. là chuyên gia quản lý, vận hành các dịch vụ website. Anh có nhiều năm kinh nghiệm về VPS, Hosting, technical SEO, CMS. Đặc biệt yêu thích WordPress và đã dùng nó hơn 5 năm nay. Sở thích của anh là đọc, viết blog, đi du lịch và tư vấn cho các bạn trẻ khởi nghiệp.