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.

I'm looking for a solution to a bug that occured in an answer to an earlier question.

The general idea is that I want to invoke submit on the post form and have the page published. What happens now is that the page reloads and the message "Page updated" appears, however the page is not published and not even drafted.

I've created a small demo to drop into your functions.php to test it out. In the demo I've created a secondary publish button that you can use to experience the problem, this is merely for illustration purposes, in the real case submit is invoked via ajax.

add_action('add_meta_boxes', 'add_metaboxes_pages', 10, 2);
function add_metaboxes_pages($post_type, $post)
{
    add_meta_box('pagesubmitdiv', 'WPSE_DEMO_META_BOX', 'page_submit_meta_box', 'page', 'advanced', 'low');
}

function page_submit_meta_box($post) 
{?>
    <a id="secondary-publish" class="button-primary">Secondary Publish</a><?php
}

add_action( 'admin_head-post.php', 'wpse_publish_admin_hook' );
add_action( 'admin_head-post-new.php', 'wpse_publish_admin_hook' );
function wpse_publish_admin_hook()
{
    global $current_screen;
    if( 'page' != $current_screen->post_type )
        return;

    ?>
    <script language="javascript" type="text/javascript">
        jQuery(document).ready(function() {
            jQuery('#secondary-publish').click(function() 
            {
                jQuery('#post').submit();
            });
        });
    </script>
    <?php
}
share|improve this question

1 Answer 1

up vote 1 down vote accepted

Trigger the click event of the publish button instead, and let WP do the work.

<script>
    jQuery(document).ready(function( $ ) {
        $( "#secondary-publish" ).click(function () {
             $( "#publish" ).click()
        });
    });
</script>

Don't use language (it was deprecated in HTML 4.01!) - you can drop type too (WP now has an HTML5 doctype).

share|improve this answer
    
Thanks for your reply (and your corrections). It works as it should standalone, but when I implent it in where it's gonna be used it produces a loop. Hm, do you have an idea what I could do? pastebin.com/RBZq842m –  INT Nov 8 '12 at 11:47
    
How did I miss that! See update. –  TheDeadMedic Nov 8 '12 at 11:48
    
The problem is that I only have #publish in the real example, #secondary-publish was just something I used in this demo for illustration purposes (so it wouldn't collide with wp original publish meta box). In the real example you actually push #publish and there's a check via ajax and then it's supposed to published, so #publish is gonna get pressed indefinitely. It's all there in the pastebin in my last comment. –  INT Nov 8 '12 at 12:04
    
Solved it by having a secondary button like my example and having the original publish button hidden. Thanks for your help! –  INT Nov 11 '12 at 23:03

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.