Php 为什么我的save_post操作没有触发?

Php 为什么我的save_post操作没有触发?,php,wordpress,plugins,save,Php,Wordpress,Plugins,Save,我正在为类中的wordpress编写一个插件,并在类的构造函数中为“save_post”添加一个操作钩子。然而,它似乎没有开火。我用对了吗 编辑2014年5月25日-我写了一个新的(经过测试的)最小示例,它明确地再现了我的问题 如果我以程序化的方式使用save_post(就像直接在index.php中使用),它确实可以工作,但是当我在类中构造所有内容时,这显然是没有帮助的 /* File index.php This file handles the installation and boots

我正在为类中的wordpress编写一个插件,并在类的构造函数中为“save_post”添加一个操作钩子。然而,它似乎没有开火。我用对了吗

编辑2014年5月25日-我写了一个新的(经过测试的)最小示例,它明确地再现了我的问题

如果我以程序化的方式使用save_post(就像直接在index.php中使用),它确实可以工作,但是当我在类中构造所有内容时,这显然是没有帮助的

/*
File index.php
This file handles the installation and bootstrapping of the plugin
*/
define("XX_POST_TYPE", 'testposttype');

if( !class_exists('MyPlugin') ):

class MyPlugin {
   var $savecontroller;

   public function __construct(){
      add_action('init', array($this, 'init'), 1);

      //include stuff before activation of theme        
      $this->include_before_theme();
   }
   //Include these before loading theme
   private function include_before_theme(){
      include_once("controllers/savecontroller.php");
   }

   public function init(){
      register_post_type( XX_POST_TYPE, 
        array(
            'labels' => array(
                'name' => __('Tests'),
                'singular_name' => __('Test'),
                'add_new' => __('Add new test'),
                'add_new_item' => __('Add new test')
            ),
            'public' => true,
            'has_archive' => true,
            'hierarchical' => true
        )
      );
      add_action('add_meta_boxes', function(){
        $this->savecontroller = new SaveController();
      });
   }
}
function startup(){
   global $myPlugin;

   if( !isset($myPlugin) ){
      $myPlugin = new MyPlugin();
   }
   return $myPlugin;
}
//Initialize
startup();
endif;
?>
保存操作发生在不同的类和文件中

<?php
// file savecontroller.php
class SaveController{
   public function __construct(){
      add_meta_box('xx_field_box', 'Field', array($this, 'setup_field'), XX_POST_TYPE);
   }
   public function setup_field( $post ){
      ?>
      <input type="text" name="xx_custom_field" id="xx_custom_field" value="">
      <?php 
      add_action('save_post', array($this, 'save_my_post'), 1, 1); 
   }
   public function save_my_post($post_id){
      if(isset($_POST['xx_custom_field'])){
         update_post_meta($post_id, 'xx_custom_field', $_POST['xx_custom_field']);
      }
   }
}
?>

它确实创建了我的自定义posttype和字段,因此我知道这些类正在工作。但是save_post不会被触发。它不会“死()”,也不会执行“更新后元数据()”。自定义字段确实出现在POST请求中,因此isset()将签出


这可能是一件愚蠢的事情,但我无法让它发挥作用

您试图在
add\u meta\u框
回调中添加钩子
save\u post

要解决此问题,请将
init
方法更改为

public function init(){
    register_post_type( $args );
    $this->savecontroller = new SaveController();
}
并将
SaveController
修改为

class SaveController{
    public function __construct(){
        add_action( 'add_meta_boxes', array( $this, 'meta_box' ) );
        add_action( 'save_post', array( $this, 'save_my_post'), 10, 2 ); 
    }
    public function meta_box(){
        add_meta_box( 'xx_field_box', 'Field', array($this, 'setup_field'), XX_POST_TYPE );
    }
    public function setup_field( $post ){
        ?>
        <input type="text" name="xx_custom_field" id="xx_custom_field" value="">
        <?php 
    }
    public function save_my_post( $post_id, $post_object ){
        wp_die( '<pre>'. print_r( $post_object, true) . '</pre>' );
    }
}
类存储控制器{
公共函数构造(){
add_操作('add_meta_box',数组($this,'meta_box'));
添加操作('save_post',数组($this,'save_my_post'),10,2);
}
公共函数元盒(){
添加元框('xx_字段框','field',数组($this,'setup_字段'),xx_POST_类型);
}
公共功能设置\u字段($post){
?>

啊,很抱歉,我没有在我的帖子中写这篇文章。我的init函数大得多,还包括其他文件。还有savecontroller。其余的类工作得很好。如果我在save函数中放了一个骰子,它就不会启动。更新了一些代码以反映我是如何包括savecontroller的。您的代码对我来说是如何工作的(调整小错误后,
save_post
不执行
die()
。请阅读…即:
问题可能不在您怀疑的部分,而是完全在另一部分。
好的,对不起。我只是不认为复制整个类可以让问题清楚,所以我只取了部分。您调整了哪些小错误?这可能是它对我不起作用的原因。好吧,a这篇文章解释说,你应该测试一下你的例子是否再现了你的问题。只要用代码制作一个空白插件,你就会看到一些致命的错误,我怀疑它们是否发生在原来的版本中。哇,谢谢,还不能升级,但这确实救了我一天!