title: Posts form with AJAX request - WordPress plugin development
body: <p>Trying to create a form with AJAX call on the Frontend where the users can submit a post.</p>
  <p>There is one text field, one textarea, and one file field.</p>
  <p>Here is the form:</p>
  <pre><code>public function pp_html_template() {
      if ( is_user_logged_in() ) {
          return '&lt;h2&gt;' . __( 'Publish your post', 'post-publisher' ) . '&lt;/h2&gt;
          &lt;form id=&quot;pp-form-submit&quot; class=&quot;pp-form-submit&quot; enctype=&quot;multipart/form-data&quot;&gt;' .
         wp_nonce_field( 'pp_publisher_save', 'pp_publisher_name' )
         . '&lt;div class=&quot;pp-row&quot;&gt;
                  &lt;label for=&quot;pp_title&quot;&gt;' . esc_attr__( 'Title', 'post-publisher' ) . '&lt;/label&gt;
                  &lt;input type=&quot;text&quot; id=&quot;pp_title&quot; name=&quot;pp_title&quot;&gt;
              &lt;/div&gt;
  
              &lt;div class=&quot;pp-row&quot;&gt;
                  &lt;label for=&quot;pp_content&quot;&gt;' . esc_attr__( 'Content', 'post-publisher' ) . '&lt;/label&gt;
                  &lt;textarea name=&quot;pp_content&quot; id=&quot;pp_content&quot; name=&quot;pp_content&quot; cols=&quot;30&quot; rows=&quot;10&quot;&gt;&lt;/textarea&gt;
              &lt;/div&gt;
              
              &lt;div class=&quot;pp-row&quot;&gt;
                  &lt;label for=&quot;pp_featured_image&quot;&gt;' . esc_attr__( 'Featured Image', 'post-publisher' ) . '&lt;/label&gt;
                  &lt;input type=&quot;file&quot; id=&quot;pp_featured_image&quot; name=&quot;pp_featured_image&quot;&gt;
              &lt;/div&gt;
              &lt;input type=&quot;hidden&quot; name=&quot;action&quot; value=&quot;pp_html_process&quot;/&gt;
              &lt;div class=&quot;pp-row&quot;&gt;
                  &lt;input type=&quot;submit&quot; name=&quot;pp_submit&quot; id=&quot;pp_submit&quot;&gt;
              &lt;/div&gt;
          &lt;/form&gt;';
      }
  }
  </code></pre>
  <p>Here is the processing:</p>
  <pre><code>public function pp_html_process() {
  
      // Process the form
      if ( isset( $_POST['pp_submit'] ) ) {
          if ( ! isset( $_POST['pp_publisher_name'] ) || ! wp_verify_nonce( $_POST['pp_publisher_name'], 'pp_publisher_save' ) ) {
              esc_attr__( 'Sorry, this action is not allowed.', 'post-publisher' );
              exit;
          } else {
              global $current_user;
  
              $user_login   = $current_user-&gt;user_login;
              $user_id      = $current_user-&gt;ID;
              $post_title   = sanitize_text_field( $_POST['pp_title'] );
              $post_content = sanitize_textarea_field( $_POST['pp_content'] );
  
              $new_post = array(
                  'post_title'   =&gt; $post_title,
                  'post_content' =&gt; $post_content,
                  'post_type'    =&gt; 'post',
                  'post_status'  =&gt; 'draft',
                  'post_name'    =&gt; str_replace( ' ', '-', $post_title ),
              );
  
              $post_id = wp_insert_post( $new_post, true );
  
              if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
                  require_once( ABSPATH . &quot;wp-admin&quot; . '/includes/image.php' );
                  require_once( ABSPATH . &quot;wp-admin&quot; . '/includes/file.php' );
                  require_once( ABSPATH . &quot;wp-admin&quot; . '/includes/media.php' );
              }
  
  
              $featured_image = media_handle_upload( 'pp_featured_image', $post_id );
              if ( $featured_image &gt; 0 ) {
                  update_post_meta( $post_id, '_thumbnail_id', $featured_image );
              }
          }
      }
  }
  </code></pre>
  <p>And <code>construct()</code></p>
  <pre><code>public function __construct() {
      if ( ! is_admin() ) {
          add_shortcode( 'pp_html_template', array( $this, 'pp_html_template' ) );
          add_action( 'init', array( $this, 'pp_html_process' ) );
      }
  
      add_action( 'wp_ajax_pp_html_process', array( $this, 'pp_html_process' ) );
      add_action( 'wp_ajax_nopriv_pp_html_process', array( $this, 'pp_html_process' ) );
  }
  </code></pre>
  <p>AJAX:</p>
  <pre><code>jQuery(document).ready(function ($) {
      $('#pp-form-submit').submit(ajaxSubmit);
  
      function ajaxSubmit() {
          var newCustomerForm = $(this).serialize();
  
          jQuery.ajax({
              type: &quot;POST&quot;,
              url: &quot;/codeable/wp-admin/admin-ajax.php&quot;,
              data: $(&quot;#pp-form-submit&quot;).serialize(),
              success: function (response) {
                  console.log(response);
              }
          });
          return false;
      }
  });
  </code></pre>
  <p>Here is the CURl request:</p>
  <pre><code>curl 'http://localhost:8888/cdbl/wp-content/plugins/post-publisher/public/assets/js/pp-public-jq.js' \
    -H 'Connection: keep-alive' \
    -H 'Pragma: no-cache' \
    -H 'Cache-Control: no-cache' \
    -H 'sec-ch-ua: &quot;Google Chrome&quot;;v=&quot;93&quot;, &quot; Not;A Brand&quot;;v=&quot;99&quot;, &quot;Chromium&quot;;v=&quot;93&quot;' \
    -H 'sec-ch-ua-mobile: ?0' \
    -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36' \
    -H 'sec-ch-ua-platform: &quot;macOS&quot;' \
    -H 'Accept: */*' \
    -H 'Sec-Fetch-Site: same-origin' \
    -H 'Sec-Fetch-Mode: no-cors' \
    -H 'Sec-Fetch-Dest: script' \
    -H 'Referer: http://localhost:8888/cdbl/' \
    -H 'Accept-Language: sr-RS,sr;q=0.9,en-US;q=0.8,en;q=0.7,hr;q=0.6,sl;q=0.5,bs;q=0.4,sv;q=0.3,da;q=0.2,pt;q=0.1,fr;q=0.1,he;q=0.1' \
    -H 'Cookie: wordpress_71d96747b9bfc3c631d78d4d4d334cf6=marko%7C1632206615%7CtnKvrmSveEtJFS7cJBUwaJgzn0pY2MbblA1yqLiaSQO%7Ccde31e8b50b7181062a7bb780b2cba008918b4f68b644f8ed2de71406ed4fe18; wordpress_test_cookie=WP%20Cookie%20check; wp-settings-time-1=1631803323; wp-settings-1=libraryContent%3Dbrowse; wordpress_logged_in_71d96747b9bfc3c631d78d4d4d334cf6=marko%7C1632206615%7CtnKvrmSveEtJFS7cJBUwaJgzn0pY2MbblA1yqLiaSQO%7C81060441911318c4457ead826c5a3e2aa1cf7fbe6bc8d078b3203eff43cb3178; mp_a36067b00a263cce0299cfd960e26ecf_mixpanel=%7B%22distinct_id%22%3A%20%2217bf571eeb2d11-0dbb8fc5091344-113f6757-60e640-17bf571eeb36fe%22%2C%22%24device_id%22%3A%20%2217bf571eeb2d11-0dbb8fc5091344-113f6757-60e640-17bf571eeb36fe%22%2C%22%24initial_referrer%22%3A%20%22http%3A%2F%2Flocalhost%3A8888%2Fpimodules%2Fwp-login.php%22%2C%22%24initial_referring_domain%22%3A%20%22localhost%3A8888%22%7D' \
    --compressed
  </code></pre>
  <p>I'm not getting any errors in the console. Post without AJAX is working perfectly fine, but with AJAX, it's returning 0.</p>
  <p>Any advice will be helpful.</p>