有什麼方法可以在不使用javascript的情況下驗證自定義元框字段。如果它不驗證我想停止保存到數據庫的帖子。Wordpress自定義元框驗證
4
A
回答
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
直從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);
}
相關問題
- 1. WordPress自定義評論字段 - 驗證
- 2. 驗證碼WordPress的自定義表單
- 3. 自定義CSS與WordPress元框的
- 4. WordPress自定義元框不會保存
- 5. Wordpress自定義元框更新模板
- 6. 自定義驗證
- 7. 自定義驗證
- 8. 選擇框自定義驗證
- 9. 驗證對話框自定義
- 10. zend框架自定義驗證類
- 11. Wix自定義對話框驗證
- 12. 文本框自定義驗證
- 13. 重新驗證JList - 自定義元素
- 14. 何時使用Bean驗證與自定義驗證框架?
- 15. ASP.NET - 驗證自定義驗證器
- 16. jquery驗證:自定義驗證規則
- 17. 自定義驗證問題
- 18. 自定義指令驗證
- 19. 郵差自定義驗證
- 20. RSpec的自定義驗證
- 21. 自定義驗證消息
- 22. 自定義bean驗證
- 23. ASP.NET MVC自定義驗證
- 24. 關於自定義驗證
- 25. WPF自定義驗證MVVM
- 26. 自定義Grails驗證
- 27. jquery自定義驗證器
- 28. Appengine Django自定義驗證
- 29. Spring自定義驗證
- 30. 自定義驗證摘要
是的,這似乎是唯一可用的選項。感謝您的回答 – 2012-08-14 04:48:17