[英]wordpress add post validation
這很令人尷尬,但我很驚訝地發現在wordpress中添加新帖子時默認沒有驗證。 當我沒有輸入標題甚至內容,只是點擊發布時,它說該帖子已發布,當我查看前端時,沒有新帖子。
wordpress如何跳過添加帖子的簡單驗證? Atleast我期望服務器端驗證(如果不是客戶端)。 不知道為什么跳過驗證。 它是wordpress,是否將它們合並到新版本中。
但我想知道如何添加javascript(或jquery)驗證以在wordpress中添加帖子。 我知道這一點都不難。 但是對wordpress不熟悉,我想得到一些暗示。
從Firebug,我可以看到表單呈現如下:
<form id="post" method="post" action="post.php" name="post">
...
</form>
我應該在哪里放置我的javascript驗證碼?
這不是一個bug, WordPress
故意這樣做(為了保存修訂版AFAIK)。 無論如何,這可能會有效但可能比這更好(粘貼在你的functions.php
文件中)
add_action( 'admin_notices', 'custom_error_notice' );
function custom_error_notice(){
global $current_screen, $post;
if ( $current_screen->parent_base == 'edit' ){
if((!$post->post_name && !$post->post_content) && $_GET['post']) {
wp_redirect(admin_url('post-new.php?empty=1'));
}
if($_GET['empty']) echo '<div class="error"><p>Warning - Please fill up all fields correctly!</p></div>';
}
}
此外,這需要使用wp_redirect
並avoid header already sent error message
,將其粘貼到所有其他代碼頂部的functions.php
中
function callback($buffer) {
// You can modify $buffer here, and then return the updated code
return $buffer;
}
function buffer_start() { ob_start("callback"); }
function buffer_end() { ob_end_flush(); }
// Add hooks for output buffering
add_action('init', 'buffer_start');
add_action('wp_footer', 'buffer_end');
嗯,你是對的,WordPress在Post Edit Screen中沒有驗證,
為什么不嘗試Post requires Fields插件,在哪里可以輕松檢查要驗證的內容,您不需要編寫單行代碼的javascript或PHP。
這是Backend Management的截圖
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.