将“重复”添加到自定义帖子类型管理菜单

时间:2018-09-10 10:03:58

标签: php wordpress custom-post-type

我正在尝试将“重复的”发布选项添加到名为“事件”的自定义发布类型管理菜单中。我已经在线搜索过,但是关于如何将此功能添加到自定义帖子类型的文档很少。也许我在搜索时使用了错误的术语。

下面的代码添加了“ duplicate”功能,但是单击该代码实际上并不会复制帖子,而是返回白屏。您可以给我一些指示或提示吗?

function rd_duplicate_post_link( $actions, $post ) {
    if (current_user_can('edit_posts') || $post->post_type=='events') {
        $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('page_row_actions', 'rd_duplicate_post_link', 10, 2)

1 个答案:

答案 0 :(得分:1)

您需要调用admin_action_rd_duplicate_post_as_draft钩子

function rd_duplicate_post_link( $actions, $post ) {

    //print_r($actions);
    //if (current_user_can('edit_posts') || $post->post_type=='movies') {
        $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('page_row_actions', 'rd_duplicate_post_link', 10, 2);



add_action( 'admin_action_rd_duplicate_post_as_draft', 'dt_dpp_post_as_draft' ); 

  function dt_dpp_post_as_draft()
    {
          global $wpdb;

          /*sanitize_GET POST REQUEST*/
          $post_copy = sanitize_text_field( $_POST["post"] );
          $get_copy = sanitize_text_field( $_GET['post'] );
          $request_copy = sanitize_text_field( $_REQUEST['action'] );

          $opt = get_option('dpp_wpp_page_options');
          $suffix = !empty($opt['dpp_post_suffix']) ? ' -- '.$opt['dpp_post_suffix'] : '';
          $post_status = !empty($opt['dpp_post_status']) ? $opt['dpp_post_status'] : 'draft';
          $redirectit = !empty($opt['dpp_post_redirect']) ? $opt['dpp_post_redirect'] : 'to_list';

            if (! ( isset( $get_copy ) || isset( $post_copy ) || ( isset($request_copy) && 'dt_dpp_post_as_draft' == $request_copy ) ) ) {
            wp_die('No post!');
            }
            $returnpage = '';

            /* Get post id */
            $post_id = (isset($get_copy) ? $get_copy : $post_copy );

            $post = get_post( $post_id );

            $current_user = wp_get_current_user();
            $new_post_author = $current_user->ID;

            /*Create the post Copy */
            if (isset( $post ) && $post != null) {
                /* 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' => $post_status,
                'post_title' => $post->post_title.$suffix,
                'post_type' => $post->post_type,
                'to_ping' => $post->to_ping,
                'menu_order' => $post->menu_order

               );
               $new_post_id = wp_insert_post( $args );

               $taxonomies = get_object_taxonomies($post->post_type);
               if(!empty($taxonomies) && is_array($taxonomies)):
               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);}
               endif;

               $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;
                  $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);
                  }

                 /*choice redirect */
                 if($post->post_type != 'post'):$returnpage = '?post_type='.$post->post_type;  endif;
                 if(!empty($redirectit) && $redirectit == 'to_list'):wp_redirect( admin_url( 'edit.php'.$returnpage ) );
                 elseif(!empty($redirectit) && $redirectit == 'to_page'):wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
                 else:
                 wp_redirect( admin_url( 'edit.php'.$returnpage ) );
                 endif;
                 exit;
                 } else {
                 wp_die('Error! Post creation failed: ' . $post_id);
                 }
   }