Site icon Hip-Hop Website Design and Development

Posts type with AJAX request – Plugin development

Making an attempt to create a type with AJAX name on the Frontend the place the customers can submit a publish.

There may be one textual content area, one textarea, and one file area.

Right here is the shape:

public perform pp_html_template() {
    if ( is_user_logged_in() ) {
        return '<h2>' . __( 'Publish your publish', 'post-publisher' ) . '</h2>
        <type id="pp-form-submit" class="pp-form-submit" enctype="multipart/form-data">' .
       wp_nonce_field( 'pp_publisher_save', 'pp_publisher_name' )
       . '<div class="pp-row">
                <label for="pp_title">' . esc_attr__( 'Title', 'post-publisher' ) . '</label>
                <enter kind="text" id="pp_title" title="pp_title">
            </div>

            <div class="pp-row">
                <label for="pp_content">' . esc_attr__( 'Content material', 'post-publisher' ) . '</label>
                <textarea title="pp_content" id="pp_content" title="pp_content" cols="30" rows="10"></textarea>
            </div>
            
            <div class="pp-row">
                <label for="pp_featured_image">' . esc_attr__( 'Featured Picture', 'post-publisher' ) . '</label>
                <enter kind="file" id="pp_featured_image" title="pp_featured_image">
            </div>
            <enter kind="hidden" title="action" worth="pp_html_process"/>
            <div class="pp-row">
                <enter kind="submit" title="pp_submit" id="pp_submit">
            </div>
        </type>';
    }
}

Right here is the processing:

public perform pp_html_process() {

    // Course of the shape
    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 motion is just not allowed.', 'post-publisher' );
            exit;
        } else {
            international $current_user;

            $user_login   = $current_user->user_login;
            $user_id      = $current_user->ID;
            $post_title   = sanitize_text_field( $_POST['pp_title'] );
            $post_content = sanitize_textarea_field( $_POST['pp_content'] );

            $new_post = array(
                'post_title'   => $post_title,
                'post_content' => $post_content,
                'post_type'    => 'publish',
                'post_status'  => 'draft',
                'post_name'    => str_replace( ' ', '-', $post_title ),
            );

            $post_id = wp_insert_post( $new_post, true );

            if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
                require_once( ABSPATH . "wp-admin" . '/consists of/picture.php' );
                require_once( ABSPATH . "wp-admin" . '/consists of/file.php' );
                require_once( ABSPATH . "wp-admin" . '/consists of/media.php' );
            }


            $featured_image = media_handle_upload( 'pp_featured_image', $post_id );
            if ( $featured_image > 0 ) {
                update_post_meta( $post_id, '_thumbnail_id', $featured_image );
            }
        }
    }
}

And __construct()

public perform __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' ) );
}

jQuery:

jQuery(doc).prepared(perform ($) {
    $('#pp-form-submit').submit(ajaxSubmit);

    perform ajaxSubmit() {
        var newCustomerForm = $(this).serialize();

        jQuery.ajax({
            kind: "POST",
            url: "/codeable/wp-admin/admin-ajax.php",
            information: $("#pp-form-submit").serialize(),
            success: perform (response) {
                console.log(response);
            }
        });
        return false;
    }
});

I am not getting any errors within the console. Put up with out AJAX is working completely wonderful, however with AJAX, it is returning 0.

Any recommendation will probably be useful.