开发者

Wordpress: Hook the post update button or form for validation

开发者 https://www.devze.com 2023-04-01 13:57 出处:网络
I want to cancel the post update in some circunstances without the need to reload the entire page. I\'ve tried:

I want to cancel the post update in some circunstances without the need to reload the entire page.

I've tried:

// publish is the id of the Update button.
$('#publish').click(function(){
    return false;
});

// post is the id of the corresponding form.
$('#post').submit(function(){
     return false;
});

In both 开发者_高级运维codes the form it's not submitted but the Update button remain disabled and the ajax loader gif is showed.

How I can do this? Thanks in advance.


I think because WordPress already has an event handler on that element it doesn't matter what it returns, it's just firing both even handlers. Try this:

$('#publish').click(function (event) {
    event.stopPropagation();
});


Did you also try:

jQuery(document).ready(function($) {
    $('#post').submit(function(e) {
        e.preventDefault();

        $('.ajax-loading').hide();
        $('#publish').removeAttr("disabled"); 
    });
});


Finally, with the help of those who answered my question (specially to stealthyninja), I managed to bring working what I asked.

@karevn: This solution doesn't break the autosave in current WordPress version (tested up to 3.2.1).

$('#publish').click(function(e) {
    if (!condition) {

        // Fix WordPress display to user.
        $('#ajax-loading').hide();
        $('#publish').removeClass('button-primary-disabled');               

        return false;
    }     
});


Sorry, it is impossible - it breaks autosave in current WordPress version. You can submit a patch to WP or stick to another solution.

0

精彩评论

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