Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Fiddling around with a custom post type which is quite heavy modified. Started doing some testing to see if I had broken some function, and realized that wordpress doesn't have a function that checks if a post has any content at all. Meaning, if I press "Add new" and just press publish, it won't say a word just save the information in the database.

The problem is that it's not visible in the admin area, just in the database. An entry with the title Auto Draft and post status auto-draft is created.

This isn't the worlds biggest problem, but still I want to avoid to be creating unnecessary entries in the database.

So I wonder how I would go about to create some sort of function that checks if any data has been entered in the editor, and if it's blank it'll prompt the user to enter data before saving/publishing the post.

The publish meta looks like this:

 <?php
if ( !in_array( $post->post_status, array('publish', 'future', 'private') ) || 0 == $post->ID ) {?> 
    <input name="original_publish" type="hidden" id="original_publish" value="<?php esc_attr_e('Publish') ?>" />
    <?php submit_button( __( 'Create' ), 'primary', 'publish', false, array( 'tabindex' => '5', 'accesskey' => 'p' ) );
} else { ?>
    <input name="original_publish" type="hidden" id="original_publish" value="<?php esc_attr_e('Save') ?>" />
    <input name="save" type="submit" class="button-primary" id="publish" tabindex="5" accesskey="p" value="<?php esc_attr_e('Save') ?>" />
<?php } ?>
share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

When i needed a solution to the save "problem" i create an Ajax function to validate the fields and did some minor JQuery hacks:

first we add our JavaScript to capture the submit/publish event and use it to submit our own ajax function before the actual submit

 add_action('wp_print_scripts','my_publish_admin_hook');

function my_publish_admin_hook(){
if (is_admin()){
        ?>
        <script language="javascript" type="text/javascript">
            jQuery(document).ready(function() {
                jQuery('#post').submit(function() {

                    var form_data = jQuery('#post').serializeArray();
                    form_data = jQuery.param(form_data);
                    var data = {
                        action: 'my_pre_submit_validation',
                        security: '<?php echo wp_create_nonce( 'pre_publish_validation' ); ?>',
                        form_data: form_data
                    };
                    jQuery.post(ajaxurl, data, function(response) {
                        if (response.indexOf('True') > -1 || response.indexOf('true') > -1 || response = true ||  response) {
                            jQuery('#ajax-loading').hide();
                            jQuery('#publish').removeClass('button-primary-disabled');
                            return true;
                        }else{
                            alert("please correct the following errors: " + response);
                            jQuery('#ajax-loading').hide();
                            jQuery('#publish').removeClass('button-primary-disabled');
                            return false;
                        }
                    });
                    return false;
                });
            });
        </script>
        <?php
    }
}

then we create the function to do the actual validation:

add_action('wp_ajax_my_pre_submit_validation', 'pre_submit_validation');
function pre_submit_validation(){
    //simple Security check
    check_ajax_referer( 'pre_publish_validation', 'security' );

    //do your validation here
    //all of the form fields are in $_POST['form_data'] array
    //and return true to submit: echo 'true'; die();
    //or your error message: echo 'bal bla bla'; die();
}

you can always change it up a bit to do the validation on the client side but i prefer on the server side.

share|improve this answer
    
Thanks alot! To have a serve side validation could be useful, but for this purpose I think it would be easier if it only had a check so if no information is entered, it's not possible to press the publish button. I realize that's not exactly what I was asking for, but I gave it some thought and came to this conclusion. With that being said. Do you have any ideas for a function like this? –  INT Mar 20 '11 at 18:14
    
you can disable the button on load and change back to enabled when the title and content are changed –  Bainternet Mar 20 '11 at 18:33
add comment

realized that wordpress doesn't have a function that checks if a post has any content at all. Meaning, if I press "Add new" and just press publish, it won't say a word just save the information in the database.

This is incorrect. wp_insert_post() checks if post has content, title or excerpt. If all of these are empty then it returns WP_Error with message about empty content.

The problem is that it's not visible in the admin area, just in the database. An entry with the title Auto Draft and post status auto-draft is created.

This isn't the worlds biggest problem, but still I want to avoid to be creating unnecessary entries in the database.

From quick look at code auto-drafts should be cleaned up automatically.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.