問題描述
我想在”Publish” 按鈕附近的某個地方新增自定義按鈕 「傳送修正」 。此自定義按鈕必須將帖子狀態從”Pending” 更改為我自己建立的名為”On correction” 的狀態。
現在可以透過 5 次點選更改狀態 (編輯狀態 – > 下拉單擊 – > 選擇 On-correction – > Ok – > 另存為更正) 。
更新:
add_action('post_submitbox_misc_actions', 'send_for_correction_button');
function send_for_correction_button()
{
//global $post;
echo '<div id="send-for-correction" class="misc-pub-section"
style="border-top-style:solid; border-top-width:1px; border-top-color:#EEEEEE; border-bottom-width:0px;">
<input id="save-post2" class="button button-highlighted"
type="submit" value="Send for correction" name="save">
</div>';
}
add_action('save_post', 'save_status');
function save_status($post_id)
{
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
if ($_POST['save'] == 'Send for correction')
{
update_post_meta($post_id, "post_status", 'on-correction');
}
}
最佳解決方案
您可以在函式中建立自定義按鈕並將其掛接到 post_submitbox_misc_actions 中,並將其新增到釋出按鈕的正上方。
要更改狀態,請在 Ajax 函式中使用 wp_update_post 。如果遇到任何問題,請嘗試併發回您的程式碼。
更新:
add_action('post_submitbox_misc_actions', 'send_for_correction_button');
function send_for_correction_button()
{
//global $post;
echo '<div id="send-for-correction" class="misc-pub-section"
style="border-top-style:solid; border-top-width:1px; border-top-color:#EEEEEE; border-bottom-width:0px;">
<input id="save-post2" class="button button-highlighted"
type="submit" value="Send for correction" name="save">
</div>';
}
add_filter( 'wp_insert_post_data' , 'my_filter_handler' , '99', 2 );
function my_filter_handler( $data , $postarr )
{
if ($postarr['save'] == 'Send for correction')
$data['post_status'] = 'on-correction';
return $data;
}
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。