Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 919
shasi kanth Avatar asked Jun 27 '13 04:06

shasi kanth


2 Answers

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');
like image 166
The Alpha Avatar answered Oct 02 '22 06:10

The Alpha


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

enter image description here

like image 32
Denish Avatar answered Oct 02 '22 07:10

Denish