admin管理员组

文章数量:1410737

Recently I want to use wp_editor on a custom plugin where i add it inside a form and want to save its data in wp_posts table.

So, I have created a new post type car and adds a field with wp_editor for content of the car. Now i want to save this content inside wp_content column of wp_posts table. Here is my code

<form id="cartype" method="POST" action="" class="hcforms">
<div id="cw-post">
    <div class="cw-block cw-head">
        <p>Classwork Title</p>
        <input type="text" name="cw-title" placeholder="Enter Car Title">
    </div>
    <div class="cw-block cw-content wp-editor">
        <p>Classwork Content</p>
        <?php
            $content = '';
            $editor_id = 'cw-posts-content';
            wp_editor(
                $content, 
                $editor_id,
                array(
                  'media_buttons' => false,
                  //'textarea_name' => 'cw_content',
                  'textarea_rows' => 2,
                  'tabindex' => 2,
                  'quicktags' => false,
                  'tinymce' => array(
                    'theme_advanced_buttons1' => 'bold, italic, ul',
                  ),
                )
            );
        ?>
    </div>
    <div class="cw-block cw-submit">
        <input type="hidden" name="action" value="postCar"/>
        <input type="submit" name="aw-whats-new-submit" value="POST" onclick="return postCar()"/>
        <button type="button" class="cancel-form" onclick="closeForms()">cancel</button>
    </div>
</div>
<?php wp_nonce_field( 'post_update', '_wpnonce_post_update' ); ?>

I am saving this post with the help of ajax. Here is my ajax code.

function postCar()
{
    var form_datacw = jQuery("#cartype").serialize();
    console.log(form_datacw);


    jQuery.ajax({
        action: "postCar",
        type:    "POST",
        dataType: "html",
        url:     ajaxurl,
        data:    form_datacw,
        success: function(data) {
            jQuery("#message-submit").html(data);
            setTimeout( function(){
                jQuery('#messageposts').hide();
                jQuery('#messageposts').remove();
                }, 
            9000);

        }
    });
    return false;
}

and calling this function inside functions.php to save data

function postCar()
{
    if( !empty($_POST['cw-title']) && !empty($_POST['cwcourse']) && !empty($_POST['cw-lesson-sel']) )
    {
        //IF ALL IS FILLED
        $user_id = get_current_user_id();
        $post_cw = array(
        'post_title'    => $_POST['cw-title'],
        'post_content'  => '',
        'post_status'   => 'publish',
        'post_type'     => 'car',
        'post_author'   => $user_id,
        );


        $cw_id = wp_insert_post( $post_cw );
        if ($cw_id) 
        {   

            $cw_post = array(
                  'ID'           => $cw_id,
                  'post_content'   => wp_kses_post($_POST['cw-posts-content']),
            );

            wp_update_post( $cw_post );

            $success = '';
            $success .= '<div id="messageposts" class="bp-messages bp-feedback success">
                    <span class="bp-icon" aria-hidden="true"></span><p>Success! Post submitted successfully.</p>
                </div>';
            echo $success;
        }
        else
        {
            $error = '';
            $error .= '<div id="messageposts" class="bp-messages bp-feedback error">
                        <span class="bp-icon" aria-hidden="true"></span><p>Something went wrong!</p>
                    </div>';
            echo $error;
        }
    }
    else
    {
        $error = '';
        $error .= '<div id="messageposts" class="bp-messages bp-feedback error">
                    <span class="bp-icon" aria-hidden="true"></span><p>Please enter all fields content to post.</p>
                </div>';
        echo $error;
    }
    wp_die();
}
add_action('wp_ajax_postCar', 'postCar');
add_action('wp_ajax_nopriv_postCar', 'postCar'); // not really needed

Everything is working fine but post_content is blank. I tried to get $_REQUEST and $_POST both are blank too. Please help. I need to use wp_editor to save user data in post_content column.

Thanks in advance! John

本文标签: pluginsHow to use wpeditor and save its data in wpposts table