我尝试在Wordpress管理帖子列表中添加发布/草稿按钮,但在特定情况下效果不佳。
Does Not Work
新建帖子->另存为草稿->使用Adomin帖子列表中的新建按钮发布
Does Work
新建帖子->发布->草稿->使用Adomin帖子列表中的新建按钮发布
提前感谢大家的帮助或任何其他方式:D
参考号:Publish pending article from front end with a button? (这不起作用)
下面是函数。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 );