Warning: Trying to access array offset on value of type bool in /www/wwwroot/weimahe.com/wp-content/advanced-cache.php on line 472

Warning: Trying to access array offset on value of type bool in /www/wwwroot/weimahe.com/wp-content/advanced-cache.php on line 472
通过代码给WordPress文章/页面添加一键复制功能 - 微码盒
!
也想出现在这里? 联系我们
广告位

通过代码给WordPress文章/页面添加一键复制功能

WordPress 默认没有复制这个功能的,比如两篇文章和页面内容相似,在原有基础上稍作修改就可以,通过复制就方便多了。当我们需要大部分相同的格式、标签、分类、自定义字段、SEO 数据等时,我们需要复制页面或文章功能。g a F S H在本教程中,我们将学习如何在 WordPress 中复制页面或文章。我们可以使用插件或不使用插件来做到这一点。在此方法中,我们将创建复制 WordPress 页面或文章的功能。我们将在 functions.php 文件中添加一个代码片段。

添加功t Y 1 G = Z

  1. /*
  2. * Function creates post duplicate as a draft and redirects thenO # s H K B to the edit post screen
  3. */
  4. f\ 5 6 /unction duplicatw 1 V & k w 1 . Qe_post_or_page(){
  5. global $wpdb;
  6. if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($G , } t 6 c : i_REQUEST['action']) && 'duplicate_post_o3 B U ) 2 e 8 q mr_page' == $_REQUEST['action'] ) ) ) &d ( g $#123;
  7. wp_die('No post to duplicate has been supplied!');
  8. }
  9. /*
  10. * Nonce verification
  11. */
  12. if ( !isset( $_GET['duplica= e .te_nonce'] ) || !wp_verify_nonce( $_GET['duplicate_nonce'], basename(4 3 o u __FILE__ ) ) )
  13. return;Y K ? }
  14. /*
  15. * get the original post id
  16. */
  17. $post_id = (isset($_GET 4 D d * @ A1;'post']) ? absint( $; ~ ) A g S N_GET['post'] ) : absintv L G R ; z I s _( $_POST ^ y r 9 y P ?1;'post'] ) );
  18. /*
  19. * and all the original postw ; Y 2 t U data then
  20. */
  21. $post = get_post( $post_id l z %41;;
  22. /*
  23. * if you don't want current user to be the new post ah J Y v } |uthor,
  24. * then change next couple of lines tY * \ & S \ k [o tc & %his: $new_post_author = $posk r p # % Qt->post_author;
  25. */
  26. $current_user = wp_get_current { at_user();
  27. $post_author = $curr ; o x Y 3 v ?rentH % 2 $ . x W_user->ID;
  28. /*
  29. * if post data exists, create the p~ \ . iost duplicate
  30. */
  31. if (isset( $post )Z t A 4; && $post !n i V C= null) {
  32. /*
  33. * new post data array
  34. */
  35. $args = arraym 4 6 !(
  36. 'comment_str C = 8atus' => $post->comment_status,
  37. 'pingJ G B_status' => $post->ping_status,$ i o I 1 ( { ( 4
  38. 'post_author' => $post_author,
  39. 'O * [ x K \ 3 -post_content' => $post->post_content,
  40. 'post_excerpt' => $post-w H l 6 C * [ ? ;>post_excerpt,
  41. 'post_name' => $poW k + ^st->post_name,
  42. 'C E K g A 2post_parent' => $post->post_parent,
  43. '~ v 9 }post_password' => $post->post_password,
  44. 'post_status' => 'draf$ K a $ J E wt',
  45. 'post_title' => $post->post_title,
  46. 'post_type' =&I T Xgt; $post->post_type,
  47. 'ta Z l Y M - ho_ping' => $post-&W O : 2 z m }gt;to_ping,
  48. 'menu_ord| u = Ker' => $post->menu_order
  49. );
  50. /*
  51. * insert tg | Y rhe post by wp_insert_post()\ = [ G Y Z function
  52. */
  53. $new_post_id = wp_insert_postK ! %( $args t g G ^ q _ H41;;
  54. /*
  55. * get all current post terms ad set them to the new post dg F nraft
  56. */
  57. $taxonomies = get_object_taxonomies($post->post_type! 3 V % / P 0 v);
  58. foreach ($taxonomies as $taxonomy) {
  59. $post_t\ Y K d p G ? Ierms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
  60. wp_set_object_terms($nZ B L D \ew_post_id, $pos! V ;t_terml B K $ = + Q ` 7s, $taxonomy, false);
  61. }k % G 5;
  62. /*
  63. * duplicate all posta # y U k x } - meta just in two SQL queries
  64. */
  65. $post_meta = $wpdb->get_results. ` q ] f40;"SELECT meta_key, meth B Q ~ a Ja_value FROM $wpdb->postmeta WHERE post_id=$post_id&qu@ 6 9 a Lot;);
  66. if (count($post_d ( b F Kmeta)!=0) {
  67. $sql_query = "INSERT INTO $wpdb->pos\ _ T 3tmeto ~ Pa (post_id, mf N C 3 g Y g Seta_key, meta_value)? ! J ; L ";
  68. foreach ($post_meta as $meta_info) {
  69. $meta_key = $meta_info->meta_key;
  70. if( $meta_key == '_wp_old_slug' ) con` i R Ftinue;
  71. $meta_value = addslashes($meta_info-&gta W } m j P v ? F;meta_value);
  72. $sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
  73. &1 W | I J .#125;
  74. $sql_quE p h p |ery.= implode(" UNION ALL ", $sql_quf P 1 N | ` i Q #ery_sel);
  75. $wpdb->query&9 V p & v J E ? )#40;$sql_query);
  76. D . G L5;
  77. /*
  78. * finally, redirectc @ . to the edit post screen for the new draft
  79. */
  80. wp_redirect( admin_url( 'post.php?action=edit&post] x [ _ { Q o V J=' . $new_post_id )i ? u );
  81. exit;
  82. } else {
  83. wp_di5 U z O . ) me('Post creation failed, could not find originalV 5 w 5 s q G post: ' . $post_id);
  84. }
  85. }
  86. add_action( 'admin_action_duplicate_post_or_page', 'duplicate_post_or_page' );

上面的代码将创建一个具有相同内容(如标签、分类、自定义字段等)的文章。因此Q c N C \ X,将此代码添加到主题的 functions.php 文件中并保存。A r J g

添加链接

上面我们制作了复制 WordPress 文章或页面的功能,并将新文章作为具有相同数据的草稿。而下面的代码将在列表页面上一个新的动作,运行上面的功能。因此,在主题的 functions.php 文件中添加以下代码并保存。

  1. /*
  2. * Add the duplicated a W b h \ & link to the acti* \ 2 f p 5 n _on list fo| y Q d D t Jr post_row_actions
  3. */
  4. function duplicate_post_1 G $ S s d Alink( $actions, $post ) {
  5. if (current_user_can('edit_posts')I e M g \ /) {
  6. $actioR J kns['duplicate'] = '<a href="'%20.%20wp_nonce_url('admin.php?acl R `tion=duplicate_post_or_page&post='%20.%20%24post->ID,%20baset ~ mname(__FILE__),%20'duplicate_nonce'%20)%20.%20'" tib ) _ E 8 c s :tle="Duplicate this item" rel="permalink">Duplicate</a>';
  7. / [ :125;
  8. return $actions;
  9. }
  10. add_filter( 'pos+ D G ? P N #t_row_f { 0 ^ U ; % ^actions', 'duplicate_poh f | ` I ? r Zst_link', 10, 2 );

在主题3 a V u z g O 0 $的 functions.php 文件中添加两个代? ; m & ! a k码块后,您将在帖子列表页面上看到一个新的操作链接。标签将是 Duplicate,您可以更改为克隆或复制。

现在您可以通过单击复制操作链接来克隆您的 WordPress 文章。它将创建具有相同内容的新文章,并将您重\ ; _ t定向到编辑帖子页面。您可以在此处发布或起草。

但是,如果您还想复制 WordPress 页面怎么办?

别担心!您不需要为此创建额外的功能。我们将使用与上述相同的功能,并将仅添加一个过滤器挂钩线来运行上述页面并显示页面的重复链接。
在页面列表添加复制链接; ` ; H M

在添加了上面【 在文章列表添加复制链接 】的代码基础上,只需再将0 ( 0 - A v ~ # M下面的代码添加到主题的 functions.php 文件中,就可以为页面显示相同的复制链接。

  1. az J add_filterO D U X \ : \ d S40;'page_row_actions', 'duplicate_post_link', 10, 2);

效果如下所示:

在本教程中,3 m x 3我们介绍了使用插件+ O ! ) s p x ` Y或代码实现文章/页* D 5 ,面一键复制的功能,希望对大家有所帮. n ) 9助。如果您有任何问题,请在下面评论。

给TA打赏
共{{data.count}}人
人已打赏
WordPress教程

WordPress 5.7 引入函数来检查文章是否可以公开查看

2022-9-2 15:37:55

WordPress教程

WordPress 获取指定ID或当前文章别名的方法代码

2022-9-2 15:39:06

下载说明

  • 1、微码盒所提供的压缩包若无特别说明,解压密码均为weimahe.com
  • 2、下载后文件若为压缩包格式,请安装7Z软件或者其它压缩软件进行解压;
  • 3、文件比较大的时候,建议使用下载工具进行下载,浏览器下载有时候会自动中断,导致下载错误;
  • 4、资源可能会由于内容问题被和谐,导致下载链接不可用,遇到此问题,请到文章页面进行反馈,以便微码盒及时进行更新;
  • 5、其他下载问题请自行搜索教程,这里不一一讲解。

站长声明

本站大部分下载资源收集于网络,只做学习和交流使用,版权归原作者所有;若为付费资源,请在下载后24小时之内自觉删除;若作商业用途,请到原网站购买;由于未及时购买和付费发生的侵权行为,与本站无关。本站发布的内容若侵犯到您的权益,请联系本站删除,我们将及时处理!
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索