早些时候追格小编分享过一篇《WordPress主题:在网页复制文章时自动带上版权小尾巴》,今天分享WordPress后台文章列表添加一键复制文章的按钮。
当然也可以安装WordPress插件Post Duplicator来实现该功能,下面说说无需插件的方法。
// 复制文章
function zhuige_clone_post_as_draft() {
global $wpdb;
if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'zhuige_clone_post_as_draft' == $_REQUEST['action'] ) ) ) {
wp_die('No post to clone has been supplied!');
}
// Nonce 验证
if ( !isset( $_GET['clone_nonce'] ) || !wp_verify_nonce( $_GET['clone_nonce'], basename( __FILE__ ) ) )
return;
// 获取原文章ID
$post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
// 所有文章数据
$post = get_post( $post_id );
// 若不希望当前用户作为新文章作者,将下两行替换为$new_post_author = $post->post_author;
$current_user = wp_get_current_user();
$new_post_author = $current_user->ID;
// 如果发布数据存在,则创建发布克隆
if (isset( $post ) && $post != null) {
// 创建新文章数组
$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
);
// 通过wp_insert_post() 函数添加文章
$new_post_id = wp_insert_post( $args );
// 将新的文章设置为草稿
$taxonomies = get_object_taxonomies($post->post_type); // 返回文章类型的分类数组,例如: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);
}
// 查询复制所有文章信息 post meta
$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);
}
wp_redirect( admin_url( 'edit.php?post_type='.$post->post_type ) );
exit;
} else {
wp_die('Post creation failed, could not find original post: ' . $post_id);
}
}
add_action( 'admin_action_zhuige_clone_post_as_draft', 'zhuige_clone_post_as_draft' );
// 复制文章并添加到列表
function zhuige_clone_post_link( $actions, $post ) {
if (current_user_can('edit_posts')) {
$actions['clone'] = '<a href="' . wp_nonce_url('admin.php?action=zhuige_clone_post_as_draft&post=' . $post->ID, basename(__FILE__), 'clone_nonce' ) . '" title="Clone this '.$post->post_type.'" rel="permalink">复制</a>';
}
return $actions;
}
add_filter( 'post_row_actions', 'zhuige_clone_post_link', 10, 2 );
add_filter('page_row_actions', 'zhuige_clone_post_link', 10, 2);
我们在当前使用的WordPress模板 functions.php 中添加以上代码即可。
-
WPvivid插件:WordPress网站备份与还原的解决方案WPvivid插件是一款功能强大的WordPress插件,专为网站数据备份、还原和搬家设计。它支持高度自定义的备份选项,允许用户选择备份整个站点(包括数据库和文件)、仅文件或仅数据库。
-
重塑WordPress生态的思考与探索前两天的《WordPress圈子到底怎么了...》这篇文章大家都挺有感触的,群里聊得热火朝天。里头啥声音都有:有的哥们儿直接吐槽WordPress性能拖后腿,躲都躲不及;有的则感慨流量难搞,但坚持建站纯粹是出于热爱和那份情怀;还有人预言,以后建站啊,模块化、可视化才是王道;较热闹的还得数短视频和直播,这家伙一来,内容消费整个儿变了天,网站和小程序怎么赚钱都成难题了。
-
WordPress企业主题近期,国内WordPress社群似乎进入了一段相对宁静的时期,无论是活跃的用户群体还是勤勉的开发者们,都呈现出一种沉稳的态势。在这样一个略显静谧的时刻,追格团队选择利用这段时间沉潜下来,专心致志地精进自身的产品,力求在每一个细节上都追求完善,以备在不久的将来,以更加超凡脱俗的面貌回馈给广大用户与开发者。
-
Loco Translate 一款专为WordPress设计的翻译插件WordPress Loco Translate是一款专为WordPress用户设计的翻译插件,它简化了主题和插件的多语言翻译过程。
-
追格小程序V2.5.0更新发布追格小程序V2.5.0版本的更新,我们没给大家添太多新花样,主要是把“内功”练好了。我们认真检查了上一版里那些组件之间的小摩擦,一点点地调整、优化,搞定了超过40多个小地方。这样一来,小程序跑起来就更顺畅了,大家用起来也会感觉更舒服、更顺手。这次虽然没大张旗鼓,但确实是让产品站得更稳、更可靠了。
-
WordPress后台待审文章显示气泡提示的方法之前小编分享过一篇《WordPress 后台菜单添加 badge-红点提示(气泡通知)》的文章,今天我简单说说待审文章(为全部文章类型显示待审角标提示)的气泡提示如何操作。
暂无评论,抢个沙发...