【问题标题】:add "duplicate" to custom post type admin menu将“重复”添加到自定义帖子类型管理菜单
【发布时间】:2019-02-14 17:54:42
【问题描述】:

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

下面的代码添加了“复制”功能,但是当点击它时,它实际上并没有复制帖子,而是返回一个白屏。有什么可以给我的指点或提示吗?

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)

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案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);
                     }
       }
    

    【讨论】:

    • 哇,谢谢。我刚刚搜索了admin_action_rd_duplicate_post_as_draft 并找到了一些包含更多信息的文章。谢谢你的回答。
    • 很高兴为您提供帮助
    猜你喜欢
    • 1970-01-01
    • 2015-01-07
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    相关资源
    最近更新 更多