2011-03-26 3 views
0

현재 WPAlchemy metabox에 사용중인 날짜와 시간으로 metabob을 변환하려고합니다.WPAlchemy 대사 물을 사용하여 날짜와 시간 입력 데이터를 하나의 필드로 결합

저장 중 하나의 입력란에 시작일과 시작 시간이 결합되었습니다.

이 기존의 저장 기능입니다 : 여기

add_action ('save_post', 'save_event'); function save_event(){ global $post; // - still require nonce if (!wp_verify_nonce($_POST['event-nonce'], 'event-nonce')) { return $post->ID; } if (!current_user_can('edit_post', $post->ID)) return $post->ID; // - convert back to unix & update post if(!isset($_POST["startdate"])): return $post; endif; $updatestartd = strtotime ($_POST["startdate"] . $_POST["starttime"]); update_post_meta($post->ID, "startdate", $updatestartd); if(!isset($_POST["enddate"])): return $post; endif; $updateendd = strtotime ($_POST["enddate"] . $_POST["endtime"]); update_post_meta($post->ID, "enddate", $updateendd); 

는 참조 용으로 새로운 기능과 필드를 다음과 같습니다

$custom_event_metabox = new WPAlchemy_MetaBox(array 
(
    'id' => '_custom_event_meta', 
    'title' => 'Event Information', 
    'template' => /event_meta.php', 
    'types' => array('event'), 
    'context' => 'normal', 
    'priority' => 'high', 
    'mode' => WPALCHEMY_MODE_EXTRACT, 
    'save_filter' => 'event_save_filter', 
    'prefix' => '_my_' // defaults to NULL 
)); 

      <li><label>Start Date</label> 
      <?php $mb->the_field('startdate'); ?> 
      <input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>" class="tsadate" /> 
      </li> 

      <li><label>Start Time</label> 
      <?php $mb->the_field('starttime'); ?> 
      <input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>" class="tsatime" /> 
      <span><em>Use 24h format (7pm = 19:00)</em></span> 
      </li> 

      <li><label>End Date</label> 
      <?php $mb->the_field('enddate'); ?> 
      <input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>" class="tsadate" /> 
      </li> 

      <li><label>End Time</label> 
      <?php $mb->the_field('endtime'); ?> 
      <input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>" class="tsatime" /> 
      <span><em>Use 24h format (7pm = 19:00)</em></span> 

내가 직면하고있어 문제는 내가 완전히 확실하지 않다입니다 save_filter 또는 save_action을 사용해야하는지, 아니면 어떻게이 WPA를 처리해야하는지 WPAlchemy.

내가 지금까지 무엇을 가지고 :

function event_save_filter($meta, $post_id) 
{ 

    // the meta array which can be minipulated 
    var_dump($meta); 

    // the current post id 
    var_dump($post_id); 

    // fix: remove exit, exit here only to show you the output when saving 
    //exit; 

    // - convert back to unix & update post 
    if(!isset($_POST["startdate"])): 
return $post; 
endif; 
$updatestartd = strtotime ($_POST["startdate"] . $_POST["starttime"]); 
update_post_meta($post->ID, "startdate", $updatestartd); 

경우 (!의는 isset을 ($ _ POST [ "ENDDATE"])) : 반환 $ 포스트; endif; $ updateendd = strtotime ($ _POST [ "enddate"]. $ _POST [ "endtime"]); update_post_meta ($ post-> ID, "enddate", $ updateendd);

// filters must always continue the chain and return the data (passing it through the filter) 
    return $meta; 

} 

이 방법이 유용할까요? save_filter 또는 save_action일까요? 당신이 WPAlchemy을 사용하고 당신이 필요로하는 모든 당신의 메타 데이터에 새 값 또는 갱신 값을 추가하는 경우

는 어떤 통찰력은 ;-)

답변

2

감사. $meta 어레이에 추가 값을 추가하여이 작업을 수행 할 수 있습니다. save_filter을 사용할 때 항상 수행해야하는 것처럼 반환하면 WPAlchemy가 데이터 저장을 처리합니다.

save_actionsave_filter 사이의 주요 차이점은 필터를 사용하여, 당신은 $meta 값을 다시 통과해야하지만, 당신은 당신이 숨겨진 값을 저장할 수있는, 그렇게하기 전에 배열을 수정할 수 있다는 것입니다. WPAlchemy 중지 알려줍니다 다시 falsesave_filter에 전달

이러한 옵션은 당신이 포스트를 업데이트하는 동안 및 사용자의 입력 값에 따라 워드 프레스의 다른 측면을 조작 할 수 있다는 점이다 사용의 전원을 켭니다.

및 저장하지 마라. 둘 사이의 추가 차이점은 저장 전에 save_filter이 발생하고 이후에 save_action이 발생한다는 것입니다.

위의 코드를 조정하려는 시도가 있습니다. 분명히 작동하도록 코드를 수정해야합니다. 포함 된 설명을 읽어보십시오.

function event_save_filter($meta, $post_id) 
{ 
    // the meta array which can be minipulated 
    var_dump($meta); 

    // the current post id 
    var_dump($post_id); 

    // fix: remove exit, exit here only to show you the output when saving 
    //exit; 


    // at this time WPAlchemy does not have any field validation 
    // it is best to handle validation with JS prior to form submit 
    // If you are going to handle validation here, then you should 
    // probably handle it up front before saving anything 

    if(! isset($meta['startdate']) OR ! isset($meta['enddate'])) 
    { 
     // returning false stops WPAlchemy from saving 
     return false; 
    } 

    $updatestartd = strtotime($meta['startdate'] . $meta['starttime']); 

    // this is an example of setting an additional meta value 
    $meta['startdate_ts'] = $updatestartd; 

    // important: 
    // you may or may not need the following, at this time, 
    // WPAlchemy saves its data as an array in wp_postmeta, 
    // this is good or bad depending on the task at hand, if 
    // you need to use query_post() WP function with the "startdate" 
    // parameter, your best bet is to set the following meta value 
    // outside of the WPAlchemy context. 

    update_post_meta($post_id, "startdate", $updatestartd); 

    $updateendd = strtotime ($meta['enddate'] . $meta['endtime']); 

    // similar note applies 
    update_post_meta($post_id, "enddate", $updateendd); 

    // filters must always continue the chain and return the data (passing it through the filter) 
    return $meta; 

} 
관련 문제