简体   繁体   English

wordpress添加帖子验证

[英]wordpress add post validation

This is embarrassing, but yet i am surprised to see that there is no validation by default while adding a new post in wordpress. 这很令人尴尬,但我很惊讶地发现在wordpress中添加新帖子时默认没有验证。 When i don't enter a title and even content, and just hit Publish, it says that the post is published, and when i view the front end, there is no new post. 当我没有输入标题甚至内容,只是点击发布时,它说该帖子已发布,当我查看前端时,没有新帖子。

How could wordpress skip the simple validation for adding a post? wordpress如何跳过添加帖子的简单验证? Atleast i expected a server side validation (if not client side). Atleast我期望服务器端验证(如果不是客户端)。 Don't know why the validation is skipped. 不知道为什么跳过验证。 It is upto wordpress, whether they incorporate it in the new versions. 它是wordpress,是否将它们合并到新版本中。

But i want to know how can i add a javascript (or jquery) validation for adding a post in wordpress. 但我想知道如何添加javascript(或jquery)验证以在wordpress中添加帖子。 I know it must not at all be difficult. 我知道这一点都不难。 But being new to wordpress, i would like to get some hint. 但是对wordpress不熟悉,我想得到一些暗示。

From Firebug, i could see the form is rendering like: 从Firebug,我可以看到表单呈现如下:

<form id="post" method="post" action="post.php" name="post">
...
</form>

Where shall i put my javascript validation code? 我应该在哪里放置我的javascript验证码?

It's not a bug, WordPress intentionally do this (to save revisions AFAIK). 这不是一个bug, WordPress故意这样做(为了保存修订版AFAIK)。 anyways, this may work but could be better ways than this (paste in your functions.php file) 无论如何,这可能会有效但可能比这更好(粘贴在你的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>';
    }
}

Also, this is required to use wp_redirect and to avoid header already sent error message , paste this in your functions.php at the top of all other code 此外,这需要使用wp_redirectavoid 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');

Well, You are right, WordPress don't have validation in Post Edit Screen, 嗯,你是对的,WordPress在Post Edit Screen中没有验证,

Why don't you try Post require Fields plugin, Where you can easily Check What to validate, You don't require to write single line of code of javascript or PHP. 为什么不尝试Post requires Fields插件,在哪里可以轻松检查要验证的内容,您不需要编写单行代码的javascript或PHP。

Here is screenshot for Backend Management 这是Backend Management的截图

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM