@Steve函数hook to save\\u post总是调用两次,Wordpress调用函数hook to save\\u post总是调用两次,因为:
第一次保存后修订第二次保存实际帖子NOTE: 如果已使用禁用发布修订define(\'WP_POST_REVISIONS\', false);
然后,当实际的帖子将被保存时,save\\u post挂钩将只被触发一次。
我刚刚测试了以下代码,它按预期工作,echo在something\\u process函数中只运行一次。
NOTE: 我上钩的方式保存了\\u post
<?php
/*
Plugin Name: Something Amazing
Plugin URI: http://www.somewhere.com/
Description: Displays something.
Author: Some guy
Version: 1.0
Author URI: http://www.somewhere.com/
*/
function call_something() {
return new something();
}
if (is_admin()) add_action(\'load-post.php\',\'call_something\');
class something {
public function __construct() {
add_action(\'add_meta_boxes\',array(&$this,\'something_add_boxes\'));
add_action(\'save_post\',array(&$this,\'something_process\'),1,2);
}
public function something_add_boxes() {
add_meta_box(\'something\',\'Some Thing\',array(&$this,\'something_form\'),\'post\',\'side\',\'default\');
}
public function something_form($post,$args) {
echo \'<p><input type="text" name="somethingnew" /></p>\';
}
public function something_process($id, $post_object) {
// don\'t run the echo if this is an auto save
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return;
// don\'t run the echo if the function is called for saving revision.
if ( $post_object->post_type == \'revision\' )
return;
echo \'hey there! I\\\'m going to cause a redirect warning, but you will see this line twice per submit!\';
//do_something_amazing();
}
}
?>