最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - wordpress add post validation - Stack Overflow

programmeradmin2浏览0评论

This is embarrassing, but yet i am surprised to see that there is no validation by default while adding a new post in 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? Atleast i expected a server side validation (if not client side). Don't know why the validation is skipped. It is upto wordpress, whether they incorporate it in the new versions.

But i want to know how can i add a javascript (or jquery) validation for adding a post in wordpress. I know it must not at all be difficult. But being new to wordpress, i would like to get some hint.

From Firebug, i could see the form is rendering like:

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

Where shall i put my javascript validation code?

This is embarrassing, but yet i am surprised to see that there is no validation by default while adding a new post in 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? Atleast i expected a server side validation (if not client side). Don't know why the validation is skipped. It is upto wordpress, whether they incorporate it in the new versions.

But i want to know how can i add a javascript (or jquery) validation for adding a post in wordpress. I know it must not at all be difficult. But being new to wordpress, i would like to get some hint.

From Firebug, i could see the form is rendering like:

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

Where shall i put my javascript validation code?

Share Improve this question asked Jun 27, 2013 at 4:18 shasi kanthshasi kanth 7,10226 gold badges111 silver badges164 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

It's not a bug, WordPress intentionally do this (to save revisions AFAIK). anyways, this may work but could be better ways than this (paste in your functions.php file)

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

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,

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.

Here is screenshot for Backend Management

发布评论

评论列表(0)

  1. 暂无评论