我就是这么做的。它不会创建新按钮,但会在“发布/更新”按钮旁边打一个复选标记。其想法是,如果在点击发布/更新之前选中该按钮。它将在保存中运行您的函数。如果未选中,系统将保存为正常。我对函数名进行了概括,以便您可以使它们更具体,以及按钮标题。
function rt_custom_save_action(){
global $post;
if (get_post_type($post) != \'post\') //Choose Post type HERE
return false;
$html = \'<div id="major-publishing-actions" style="overflow:hidden">\';
$html .= \'<div id="publishing-action">\';
$html .= \'<label><input type="checkbox" value="1" name="custom_action" />SAVE WITH FUNCTION</label>\';
$html .= \'</div>\';
$html .= \'</div>\';
echo $html;
}
add_action( \'post_submitbox_misc_actions\', \'rt_custom_save_action\' );
function rt_project_updated_save_dosomething( $post_id ) {
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
// If this is just a revision, don\'t do anything.
// if ( wp_is_post_revision( $post_id ) )
// return;
// only go if user checked action checkbox.
if(isset($_POST[\'custom_action\'])){
//PUT SOMETHING HERE THAT YOU WANT TO HAPPEN
}
}
add_action( \'save_post\', \'rt_project_updated_save_dosomething\' ); //if you change the custom post type above, you have to change it here too (save_post_CUSTOMPOSTNAME)
如果您想使用按钮,我想您可以这样做,我已经删除了函数中的复选框复选框,并添加了2个按钮选项。先尝试一种,然后再尝试另一种:
function rt_custom_save_action(){
global $post;
if (get_post_type($post) != \'post\') //Choose Post type HERE
return false;
$html = \'<div id="major-publishing-actions" style="overflow:hidden">\';
$html .= \'<div id="publishing-action">\';
// $html .= \'<input type="submit" accesskey="p" tabindex="5" value="custom_action" class="button-primary" id="custom" name="custom_action" value="submitted">\';
// try this button code first.
// $html .= \'<button onclick="rt_project_updated_save_dosomething( $post_id );" accesskey="p" tabindex="5" class="button-primary" id="custom" name="custom_action">SEND WITH FUNCTION</button>\';
// try this one next but don\'t use both.
$html .= \'</div>\';
$html .= \'</div>\';
echo $html;
}
add_action( \'post_submitbox_misc_actions\', \'rt_custom_save_action\' );
function rt_project_updated_save_dosomething( $post_id ) {
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
// If this is just a revision, don\'t do anything.
if ( wp_is_post_revision( $post_id ) )
return;
//PUT SOMETHING HERE THAT YOU WANT TO HAPPEN
}
add_action( \'save_post\', \'rt_project_updated_save_dosomething\' ); // If you change the custom post type above, you have to change it here too (save_post_CUSTOMPOSTNAME)