2014-05-24 18 views
1

클래스에 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()은 체크 아웃합니다.

아마 뭔가 바보 같지만 작동시킬 수는 없습니다.

+0

아하 죄송합니다. 내 게시물에 해당 내용을 적어 두지 않았습니다. 내 init 함수는 훨씬 크고 다른 파일도 포함되어있다. 또한 savecontroller도 거기에 포함되어 있습니다. 나머지 클래스는 잘 작동합니다. 저장 기능에 주사위를 넣으면 화재가 발생하지 않습니다. – iamrobin

+0

일부 코드가 savecontroller를 포함하는 방법을 반영하도록 업데이트되었습니다. – iamrobin

+0

코드가 작동합니다 (사소한 오류 수정 후). save_post는'die()'를 수행합니다. [** 최소, 완전하고 검증 가능한 예제를 만드는 방법 **] (http://stackoverflow.com/help/mcve)를 읽어보십시오. . . 즉 : '문제는 당신이 의심하는 부분이 아닐 수도 있지만, 또 다른 부분은 전부입니다.' – brasofilo

답변

1

save_postadd_meta_box 콜백 안에는 후크를 추가하려고합니다. 그 위치는 아닙니다. 그것을 해결하기

public function init(){ 
    register_post_type($args); 
    $this->savecontroller = new SaveController(); 
} 

init 방법을 변경하고 save_post 작업은 두 개의 매개 변수를 사용하여 우선 순위가 기본 (10)이 될 수 있다는 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>'); 
    } 
} 

에주의를 수정합니다. 메타 박스에 대한 많은 예제와 save_post here을 찾을 수 있습니다.

+0

와우 덕분에, 아직 upvote 수는 없지만, 이것은 확실히 내 하루를 저장! – iamrobin

관련 문제