开发者

How do I add a confirmation dialog to the Wordpress publish button? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I'm developing a wordpress plugin which creates a custom post type "knowledgebase article" with some special fields. I'd like the publish button to warn the user if they are missing fields before they commit. How do I do this? Is there an appropriate hook to inse开发者_如何学Gort this behavior?


You could use the basic client-side form validation script in wp-admin/js/common.js which is included by default on all admin pages.

First, add an action via your plugin that runs on the post editing screen:

add_action('admin_print_scripts', 'my_validation_script');
function my_validation_script() {
    global $post_type;
    if(isset($post_type) && $post_type == 'knowledgebase_article') {
        wp_enqueue_script('my-validation-script', network_site_url() . '/wp-content/plugins/your-plugin-folder/my-validation-script.js', array('jquery', 'common'));
    }
}

Next, write the little javascript form handler to actually handle the form submission:

jQuery(document).ready(function(){

    //'my_required_field' is the class name of the DIV or TD tag *containing* the required form field
    jQuery('.my_required_field').addClass('form-required'); 

    //'post' is the ID of the main form on the new post screen
    jQuery('#post').submit(function(){ 
            //the validateForm function lives in wp-admin/js/common.js
            if ( !validateForm( jQuery(this)) ) { 
                    alert('Please enter a value'); 
                    return false; //stop submitting the form
            } else {
                    //alert('all required fields have some input');
            }
    });

});

You can see how this validator behaves by visiting the tag editing page, then creating a new tag with no name. The field's container turns red alerting you to the problem.

I tested this on my end and it works. Hopefully it can help you out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜