当前位置: 首页 > article >正文

纯代码实现给WordPress添加文章复制功能

在给wordpress添加内容时,有时会遇到文章复制的功能,但是wordpress又没有这个功能。把下面一段代码添加到functions.php文件中,就可以实现这个功能。

/*
 * 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'] = '<a href="' . wp_nonce_url('admin.php?action=rd_duplicate_post_as_draft&post=' . $post->ID, basename(__FILE__), 'duplicate_nonce' ) . '" title="Duplicate this item" rel="permalink">Duplicate</a>';
  }
  return $actions;
}
add_filter( 'post_row_actions', 'rd_duplicate_post_link', 10, 2 );

添加完代码后,就会有后台文章管理列表中,鼠标移到标题上时,就会看到出现Duplicate管理标签。点击这个标题就会实现对该文章的复制。

文章

https://www.zhanyes.com/code/5995.html


http://www.kler.cn/a/507693.html

相关文章:

  • Json转换类型报错问题:java.lang.Integer cannot be cast to java.math.BigDecimal
  • 若依分页插件失效问题
  • Spring Boot Actuator 详细介绍
  • Pandas库的常用内容归纳
  • 基于Oracle与PyQt6的电子病历多模态大模型图形化查询系统编程构建
  • JAVA实现五子棋小游戏(附源码)
  • C#实现字符串反转的4种方法
  • openharmony/build/README_zh.md学习
  • 查找某个年龄段的用户信息TCP头格式为什么需要 TCP 协议? TCP 工作在哪一层?
  • Spring Boot 条件注解:@ConditionalOnProperty 完全解析
  • 整数的分离与合成
  • DNS介绍(3):应用场景
  • 数据结构之哈希表详解
  • 【Block总结】WTConv,小波变换(Wavelet Transform)来扩展卷积神经网络(CNN)的感受野
  • 【论文阅读笔记】基于YOLO和ResNet深度卷积神经网络的结直肠息肉检测
  • 【SQL】查询前三名
  • STM32 FreeRTOS 任务创建和删除实验(动态方法)
  • 机器学习和深度学习是人工智能(AI)领域的两个重要分支,它们都依赖于数学、统计学和计算机科学的基础知识。
  • Ubuntu系统备份与还原
  • ECCV2020 | YAILA | 又一种中间层攻击方法
  • 如何进行域名跳转与域名重定向的综合指南
  • 一个悄然崛起的开源项目,已开源
  • JavaScript 变量与数据类型
  • WEB攻防-通用漏洞_XSS跨站_MXSS_UXSS_FlashXSS_PDFXSS
  • PCL 新增自定义点类型【2025最新版】
  • 【Vim Masterclass 笔记16】S07L32 + L33:同步练习09 —— 掌握 Vim 宏操作的六个典型案例(含点评课内容)