I'm adding a custom form within a shortcode, but whenever I paste the shortcode in the page editor the form posts automatically and redirects to /wp-admin/admin-post.php with a blank screen.

I'm using the admin_post hook to submit the form.

But when I add the form using the_content hook it works perfect. The only problem with this method is that I don't have full control of where to add the form in the page.

What am I doing wrong?

add_shortcode('get_job_card_form', 'vision_get_job_card_form');
function vision_get_job_card_form()
    $formHtml = '<form action="' . admin_url('admin-post.php') . '" method="POST">';
    $formHtml .= wp_nonce_field('get_job_card', 'get_job_card_nonce');
    $formHtml .= ' <input type="hidden" name="action" value="get_job_card" />';
    $formHtml .= '<button name="getJobCard" type="submit" class="et_pb_button et_pb_button_2 et_pb_bg_layout_light">Get Job Card</button>';
    $formHtml .= '</form>';
    return $formHtml;

add_action('admin_post_nopriv_get_job_card', 'get_job_card');
add_action('admin_post_get_job_card', 'get_job_card');
function get_job_card()
    include get_stylesheet_directory() . '/vision/data/vision-get-job-card.php';

1 Answer 1


whenever I paste the shortcode in the page editor the form posts automatically and redirects to /wp-admin/admin-post.php with a blank screen

I don't know what editor is that (🤔), and I'm not sure if this will fix the above issue, but I noticed your function is using wp_nonce_field() which by default displays/echo the output, so because you're doing $formHtml .= wp_nonce_field(...), then you need to disable the echo because otherwise you'd run into issues like invalid REST API's JSON response which then causes the block/Gutenberg editor to fail in saving the post.

// set the 4th parameter to false
$formHtml .= wp_nonce_field('get_job_card', 'get_job_card_nonce', true, false);
  • That is a very good point and I have also spotted it. I am using the Divi editor :( and it might be the editor. I've found a workaround though. I am using the_content hook and making use of a string replace to position the form where I want it. Jun 20, 2022 at 7:06
  • Well, that's not a good option.. And after you corrected your wp_nonce_field() code, you could try clearing your caches and see if doing so helps. Else, you could try deactivating plugins. If that still doesn't help, then maybe the issue is caused by the active theme.
    – Sally CJ
    Jun 20, 2022 at 7:16
  • Thanks for the advice. I will try this and let you know. Jun 20, 2022 at 7:56
  • 1
    Thank you @SallyCJ, setting the 4th parameter to false worked! Jun 20, 2022 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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