我尝试在Wordpress Admin帖子列表中添加“发布/草稿”按钮,但是在某些情况下效果不佳。
新帖子->另存为草稿->使用Adomin帖子列表中的新按钮发布
新帖子->发布->草稿->使用Adomin帖子列表中的新按钮发布
在此先感谢大家的帮助或:D
参考: https://wordpress.stackexchange.com/questions/17794/publish-pending-article-from-front-end-with-a-button (这不起作用)
这是functions.php代码
function show_publish_button($post_id){
$pub_form = <<<EOF
<form name="make_it_publish" method="post" action="">
<input type="hidden" name="pid" id="pid" value="{$post_id}">
<input type="hidden" name="mi_publish" id="mi_publish" value="mi_publish">
<input type="submit" name="submit" id="submit" value="publish">
</form>
EOF;
echo $pub_form;
}
function show_draft_button($post_id){
$pri_form = <<<EOF
<form name="make_it_draft" method="post" action="">
<input type="hidden" name="pid" id="pid" value="{$post_id}">
<input type="hidden" name="mi_draft" id="mi_draft" value="mi_draft">
<input type="submit" name="submit" id="submit" value="draft">
</form>
EOF;
echo $pri_form;
}
function change_post_status($post_id,$status){
$time = current_time('mysql');
wp_update_post(
array(
'ID' => $post_id,
'post_status' => $status,
'post_name' => $post_id,
'post_date_gmt' => get_gmt_from_date( $time )
)
);
}
add_action( 'admin_init', function() {
if (isset($_POST['mi_publish']) && $_POST['mi_publish'] == 'mi_publish'){
if (isset($_POST['pid']) && !empty($_POST['pid'])){
change_post_status((int)$_POST['pid'],'publish');
}
}
if (isset($_POST['mi_draft']) && $_POST['mi_draft'] == 'mi_draft'){
if (isset($_POST['pid']) && !empty($_POST['pid'])){
change_post_status((int)$_POST['pid'],'draft');
}
}
});
function manage_posts_columns($columns) {
$columns['publish'] = "publish";
$columns['draft'] = "draft";
return $columns;
}
function add_column($column_name, $post_id) {
if($column_name == 'publish'){
$pub = "publish";
}
if($column_name == 'draft'){
$pri = "draft";
}
if ( isset($pub) && $pub ){
show_publish_button($post_id);
} else if ( isset($pri) && $pri ){
show_draft_button($post_id);
} else {
echo __('None');
}
}
add_filter( 'manage_posts_columns', 'manage_posts_columns' );
add_action( 'manage_posts_custom_column', 'add_column', 10, 2 );
答案 0 :(得分:0)
原因是嵌套表单标签。
Admin Post Lists是巨大的表单,我添加了form标记。这就是为什么它不起作用。
简单的方法是使用ajax和wp_update_post。