javascript - wordpress add post validation - Stack Overflow

This is embarrassing, but yet i am surprised to see that there is no validation by default while adding

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745247970a4618501.html

相关推荐

  • javascript - wordpress add post validation - Stack Overflow

    This is embarrassing, but yet i am surprised to see that there is no validation by default while adding

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信