Support » Developing with WordPress » Post metabox with custom submit button issue on submit

  • Hi,

    I am developing a plugin, that adds a metabox to my custom post type.
    Inside this metabox is a submit button for the post.
    This button saves the post and some other code is executed, when the button is set in the $_POST request.
    Everything works fine, however I get the browser alert asking if I want to leave the site. When I confirm that, everything works fine, I just don’t understand why I get the alert.

    My metabox:

    <?php
    if($post->post_status !== 'publish'):
    ?>
    <div class="wpt-verify-post-submit">
        <input type="submit" name="wpt-verify-post" id="wpt-verify-post" class="button" value="<?php _e('Some name', 'my_plugin_domain') ?>" />
        <div style="clear: both"></div>
    </div>
    <div class="wpt-verify-post-status">
        Status: <span id="wpt-verify-status-display">@status</span>
    </div>
    <?php
    else:
    ?>
    <span>Some text</span>
    <?php
    endif;

    Do I need to add some additional information for WordPress or something?

    • This topic was modified 1 year, 8 months ago by cyfy0.
    • This topic was modified 1 year, 8 months ago by cyfy0.
    • This topic was modified 1 year, 8 months ago by cyfy0.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    Your meta box in part of the overall post edit form, but your button is buried down below the level of the other elements like the usual update button, so the browser sees the resulting submit as a navigation away from a form that was altered, causing the warning to be presented.

    Why wouldn’t the normal update button suffice? If you must have such an alternative button, it should trigger an Ajax request and whatever needs being done be handled by an Ajax handler. This is similar to how buttons for custom fields are handled.

    Okay, I understand, but why does the default save button does not cause such a warning?

    I want to add another button, to run some additional code, while preserving the option for only saving the post with the save button.

    I thought about an AJAX handler too, but wanted to see, if it works this way too somehow. Thank you for your help, guess I’ll go for the AJAX option.

    Moderator bcworkz

    (@bcworkz)

    I’m not sure. I think it has to do with hierarchy of DOM elements

    <form>
      <input>
      <input>
      <div>
        <div>
          <h3>Metabox</h3>
          browser sees this as just another input field
          <submit>
        </div>
      </div>
      The primary submit
      <submit>
    </form>

    If anyone knows for sure, here’s your chance for 15 minutes of fame 🙂

    Okay, so I have tested burying a submit button under some DOM elements inside a form in a simple php file for testing and it wasn’t causing any issues.

    I looked at some wordpress plugin that adds an additional save button. It adds it to the publish metabox with the post_submitbox_start action hook.
    I tried that on my installation as well and it worked perfectly, also with the post_submitbox_misc_actions action hook, no issues.

    So I conclude that wordpress does something with the post submitbox in the background to make it work with their request handling.
    I just don’t know what, and I don’t feel like it would be worth the time and energy to look into that for me.

    Thank you for your help, I appreciate it a lot.

    Moderator bcworkz

    (@bcworkz)

    You’re welcome, and thanks for digging into the mystery further.

    Hi,
    I tried this code to save a CPT as Draft, and for me worked:

    if ( $post->post_status !== 'publish' ) {
      ?>
      <div class="inside">
          <div class="submitbox" id="submitpost">
    	<div id="minor-publishing">
    	  <div style="display:none;">
    		<p class="submit"><input type="submit" name="save" id="save" class="button" value="<?php _e('Save as Draft', 'language' ) ?>"></p>
    	  </div>
    	</div>
    	<div id="minor-publishing-actions">
    	  <div id="save-action">
    		<input type="submit" name="save" id="save-post" value="<?php _e('Save as Draft', 'language' ) ?>" class="button">
    		<span class="spinner"></span>
    	  </div>
            </div>
    	<div class="clear"></div>
        </div><!-- #minor-publishing-actions -->
      </div>
      <?php
    }
    • This reply was modified 1 year, 3 months ago by Fernando Garcia. Reason: Change some code
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Post metabox with custom submit button issue on submit’ is closed to new replies.