2012-08-05 82 views

回答

3

由於'save_post'操作是在發佈和更新後運行的,因此沒有辦法驗證自定義密鑰而沒有黑客選擇。但是,我認爲你可以通過Viral建議的方式使用'save_post'來模仿你想要的功能,但是不要因爲驗證錯誤而中斷或取消保存過程,你可以完全刪除這個帖子:

add_action('save_post', 'validate_meta'); 
function validate_meta($post_id) 
{ 
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
     return $post_id; 
    /*USE THIS ONLY IF YOU ARE UTILIZING NONCE FIELDS IN A CUSTOM META BOX 
    if (!wp_verify_nonce($_POST['metabox_nonce'], basename(__FILE__))) 
     return $post_id;*/ 
    /*Use plugin_basename(__FILE__) if this is an actual plugin, rather than 
    a part of your theme*/ 

    if ('page' == $_POST['post_type']) 
    { 
     if (!current_user_can('edit_page', $post_id)) 
      return $post_id; 
    } 
    else 
    { 
     if (!current_user_can('edit_post', $post_id)) 
      return $post_id; 
    } 

    /*VALIDATE YOUR METADATA HERE HOWEVER YOU LIKE 
    if(is_valid($_POST['metadata'])) 
     $validated = true; 
    else 
     $validated = false; 
    */ 

    if(!$validated) 
     wp_delete_post($post_id, true); 
    else 
     return $post_id; 
} 

要注意這種方法唯一的一點是,它將同時運行發佈和更新。您可能需要考慮添加檢查以確保僅針對新發布的帖子刪除帖子,並將更新的帖子回滾到以前的版本,並刪除無效的修訂。

+0

是的,這似乎是唯一可用的選項。感謝您的回答 – 2012-08-14 04:48:17

0

直從WP法典@http://codex.wordpress.org/Function_Reference/add_meta_box,你叫save_post掛鉤,並指定將運行驗證功能/保存您的數據:

/* Do something with the data entered */ 
add_action('save_post', 'myplugin_save_postdata'); 

然後定義功能,它會自動傳遞帖子ID。此外,您可以訪問$ _POST陣列來獲得你的metaboxes值:

/* When the post is saved, saves our custom data */ 
function myplugin_save_postdata($post_id) { 
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything 
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
     return $post_id; 

    // verify this came from the our screen and with proper authorization, 
    // because save_post can be triggered at other times 

    if (!wp_verify_nonce($_POST['myplugin_noncename'], plugin_basename(__FILE__))) 
     return $post_id; 


    // Check permissions 
    if ('page' == $_POST['post_type']) 
    { 
    if (!current_user_can('edit_page', $post_id)) 
     return $post_id; 
    } 
    else 
    { 
    if (!current_user_can('edit_post', $post_id)) 
     return $post_id; 
    } 

    // OK, we're authenticated: we need to find and save the data 

    $mydata = $_POST['myplugin_new_field']; 

    // Do something with $mydata 
    // probably using add_post_meta(), update_post_meta(), or 
    // a custom table (see Further Reading section below) 

    return $mydata; 
} 

你的所有程序的有效數據將在這個函數中完成。最後,您可能會使用類似的方式保存數據: update_post_meta('meta_key', 'meta_value');

+0

但是,這不會阻止帖子被保存,以防自定義選項沒有正確驗證。 – 2012-08-08 09:52:41

1

wp_insert_post_data過濾器是你在找什麼。像這樣的應該做的伎倆:

add_filter('wp_insert_post_data', 'my_validation_function'); 

function my_validation_function($data) { 
    // Don't want to do this on autosave 
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
     return $data; 
    if ($data['some_key'] != 'some_value' || 
     $_POST['some_meta_key'] != 'some_meta_value') { 
     $data['post_status'] = 'draft'; // or whatever status to revert to 
     add_filter('redirect_post_location', 'remove_message'); // remove the publish success message 
    } 
    return $data; 
} 

function remove_message($location) { 
    return remove_query_arg('message', $location); 
}