WordPress 后台直接复制文章

  • 一一得一
  • 技术分享
  • Jul 02, 2022

在 WordPress 后台文章列表中,如果能直接复制一篇文章或页面,然后重新编辑后发表,是不是很方便。那为什么不全选复制文章内容,然后粘贴到新建文章中呢?这是因为这样做仅复制了文章内容,并未同时把原文章的相关信息一同复制过去,比如一些自定义字段等。文章源自知更鸟-https://zmingcx.com/duplicate-post-in-wordpress.html

利用本文的一段代码,可以实现快速复制克隆一份文章,并保留原文章的所有信息。文章源自知更鸟-https://zmingcx.com/duplicate-post-in-wordpress.html

将代码添加到当前主题函数模板 functions.php 中:文章源自知更鸟-https://zmingcx.com/duplicate-post-in-wordpress.html


 
  1. // 复制文章
  2. function zm_clone_post_as_draft() {
  3. global $wpdb;
  4. if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'zm_clone_post_as_draft' == $_REQUEST['action'] ) ) ) {
  5. wp_die('No post to clone has been supplied!');
  6. }
  7.  
  8. // Nonce 验证
  9. if ( !isset( $_GET['clone_nonce'] ) || !wp_verify_nonce( $_GET['clone_nonce'], basename( __FILE__ ) ) )
  10. return;
  11.  
  12. // 获取原文章ID
  13. $post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
  14.  
  15. // 所有文章数据
  16. $post = get_post( $post_id );
  17.  
  18. // 如果不希望当前用户作为新文章作者,将下两行替换为$new_post_author = $post->post_author;
  19. $current_user = wp_get_current_user();
  20. $new_post_author = $current_user->ID;
  21.  
  22. // 如果发布数据存在,则创建发布克隆
  23. if (isset( $post ) && $post != null) {
  24.  
  25. // 创建新文章数组
  26. $args = array(
  27. 'comment_status' => $post->comment_status,
  28. 'ping_status' => $post->ping_status,
  29. 'post_author' => $new_post_author,
  30. 'post_content' => $post->post_content,
  31. 'post_excerpt' => $post->post_excerpt,
  32. 'post_name' => $post->post_name,
  33. 'post_parent' => $post->post_parent,
  34. 'post_password' => $post->post_password,
  35. 'post_status' => 'draft',
  36. 'post_title' => $post->post_title,
  37. 'post_type' => $post->post_type,
  38. 'to_ping' => $post->to_ping,
  39. 'menu_order' => $post->menu_order
  40. );
  41.  
  42. // 通过wp_insert_post() 函数添加文章
  43. $new_post_id = wp_insert_post( $args );
  44.  
  45. // 将新的文章设置为草稿
  46. $taxonomies = get_object_taxonomies($post->post_type); // 返回文章类型的分类数组,例如:array("category", "post_tag");
  47. foreach ($taxonomies as $taxonomy) {
  48. $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
  49. wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
  50. }
  51.  
  52. // 查询复制所有文章信息 post meta
  53. $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
  54. if (count($post_meta_infos)!=0) {
  55. $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
  56. foreach ($post_meta_infos as $meta_info) {
  57. $meta_key = $meta_info->meta_key;
  58. if( $meta_key == '_wp_old_slug' ) continue;
  59. $meta_value = addslashes($meta_info->meta_value);
  60. $sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
  61. }
  62. $sql_query.= implode(" UNION ALL ", $sql_query_sel);
  63. $wpdb->query($sql_query);
  64. }
  65. wp_redirect( admin_url( 'edit.php?post_type='.$post->post_type ) );
  66. exit;
  67. } else {
  68. wp_die('Post creation failed, could not find original post: ' . $post_id);
  69. }
  70. }
  71. add_action( 'admin_action_zm_clone_post_as_draft', 'zm_clone_post_as_draft' );
  72.  
  73.  
  74. // 复制文章并添加到列表
  75. function zm_clone_post_link( $actions, $post ) {
  76. if (current_user_can('edit_posts')) {
  77. $actions['clone'] = '<a href="' . wp_nonce_url('admin.php?action=zm_clone_post_as_draft&post=' . $post->ID, basename(__FILE__), 'clone_nonce' ) . '" title="Clone this '.$post->post_type.'" rel="permalink">复制</a>';
  78. }
  79. return $actions;
  80. }
  81.  
  82. add_filter( 'post_row_actions', 'zm_clone_post_link', 10, 2 );
  83. add_filter('page_row_actions', 'zm_clone_post_link', 10, 2);

之后,进入WP后台 → 文章 → 所有文章,在文章标题下面会增加“复制”按钮,点击后会复制一份完全相同、状态为草稿的文章,同时支持页面及自定义文章类型。文章源自知更鸟-https://zmingcx.com/duplicate-post-in-wordpress.html

如果不想使用代码。可以安装使用相似功能的插件:Post Duplicator 与Yoast Duplicate Post

打赏