클래스에 wordpress 용 플러그인을 작성하고 있으며 클래스 생성자에서 'save_post'에 액션 훅을 추가하고 있습니다. 그러나 그것은 발사하지 않는 것 같습니다. 올바른 방법으로 사용하고 있습니까?왜 내 save_post 작업이 트리거되지 않습니까?
EDIT 25-05-2014 - 필자는 문제를 확실히 재현 한 새로운 (테스트 된) 최소 예제를 작성했습니다.
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']);
}
}
}
?>
클래스가 작동하는 것을 알고 있으므로 내 맞춤형 포스트 유형 및 입력란을 만듭니다. 그러나 save_post가 트리거되지 않습니다. 그것은 '죽지 않습니다'() '및'update_post_meta() '않습니다. 사용자 정의 필드는 POST 요청에 나타나므로 isset()은 체크 아웃합니다.
아마 뭔가 바보 같지만 작동시킬 수는 없습니다.
아하 죄송합니다. 내 게시물에 해당 내용을 적어 두지 않았습니다. 내 init 함수는 훨씬 크고 다른 파일도 포함되어있다. 또한 savecontroller도 거기에 포함되어 있습니다. 나머지 클래스는 잘 작동합니다. 저장 기능에 주사위를 넣으면 화재가 발생하지 않습니다. – iamrobin
일부 코드가 savecontroller를 포함하는 방법을 반영하도록 업데이트되었습니다. – iamrobin
코드가 작동합니다 (사소한 오류 수정 후). save_post는'die()'를 수행합니다. [** 최소, 완전하고 검증 가능한 예제를 만드는 방법 **] (http://stackoverflow.com/help/mcve)를 읽어보십시오. . . 즉 : '문제는 당신이 의심하는 부분이 아닐 수도 있지만, 또 다른 부분은 전부입니다.' – brasofilo