WordPress的添加帖子验证

sha*_*nth 5 javascript validation wordpress

这很尴尬,但是令我惊讶的是,在wordpress中添加新帖子时默认情况下没有验证。当我没有输入标题甚至内容时,只要单击“发布”,它就表示该帖子已发布,并且当我查看前端时,没有新的帖子。

wordpress如何跳过添加帖子的简单验证?Atleast我期望服务器端验证(如果不是客户端端)。不知道为什么跳过验证。是否由Wordpress负责,是否将其合并到新版本中。

但是我想知道如何在WordPress中添加javascript(或jquery)验证来添加帖子。我知道这绝对不难。但是对于wordpress还是陌生的,我想得到一些提示。

从Firebug,我可以看到表单呈现为:

<form id="post" method="post" action="post.php" name="post">
...
</form>
Run Code Online (Sandbox Code Playgroud)

我应该将JavaScript验证码放在哪里?

The*_*pha 3

这不是一个错误,是WordPress故意这样做的(据我所知是为了保存修订)。无论如何,这可能有效,但可能是比这更好的方法(粘贴到您的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>';
    }
}
Run Code Online (Sandbox Code Playgroud)

另外,这是使用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');
Run Code Online (Sandbox Code Playgroud)