0

I am making my theme with studying. I want front-end posting that can be written by users.

I made form. like this.

<form class="rnm_fe_write_contets">
            <ul>
                <li>
                    <div class="rnm_fe_wr_textbox">
                        <input name="fe_title" class="rnm_fe_wr_textarea" placeholder="Title" />
                    </div>
                </li>
                <li>
                    <div class="fe_wr_textbox">
                        <?php
                        $distribution = '';
                        wp_editor(
                            $distribution, 
                            'feeling', 
                            array(
                                'wpautop' => true,
                                'media_buttons' => false,
                                'textarea_rows' =>10,
                                'tabindex' => 4,
                                'tinymce' => array(
                                    'theme_advanced_buttons1' => 'bold, italic, ul, pH, temp',
                                ),
                            )
                        );
                        ?>
                    </div>
                </li>                   
            </ul>
            <input name="fe_submit" type="submit" />

        </form>

And I will submit in front-end like this.

<?php
    $post_data = array(
        'post_title' => isset($_POST['fe_title']) ? sanitize_text_field($_POST['fe_title']) : '',
        'post_content' => '', // how to get this contents from wp_editor??

        'post_status' => 'pending',
        'post_type' => 'feeling',                

    );  



    $post_id = wp_insert_post( $post_data );

    if( is_wp_error( $post_id ) || !$post_id ){
        wp_redirect( site_url('/feeling-submit/') . '?result=failure' );
        exit;
    } 
?>

But I don't know how to get the input-data from wp_editor. Can I have some help?

Thank you.

2
  • 1
    Have you tried $_POST['feeling']? - feeling is editor id. Jun 15, 2018 at 6:35
  • I found answer with googling. :: $content = $_POST['wp_editor id'];
    – JJang
    Jun 15, 2018 at 7:13

1 Answer 1

-1

If you want to add a new post without logging into the WordPress dashboard or allow your visitors a way to submit content of their own, you can do this by front end post submission.

In this post I will show you the way to submit a post from the front end. Let’s start…

You need to follow the below essential steps for making a form for user submitted posts

Step 1. Create a form by which a user can submit the post title, content, tags, category, featured image. You can put this code into your theme functions.php or any other file.

add_shortcode( 'themedomain_frontend_post', 'themedomain_frontend_post' );
    function themedomain_frontend_post() {
        themedomain_post_if_submitted(); ?>
        <form id="new_post" name="new_post" method="post"  enctype="multipart/form-data">

            <p><label for="title"><?php echo esc_html__('Title','theme-domain'); ?></label><br />
                <input type="text" id="title" value="" tabindex="1" size="20" name="title" />
            </p>

            <?php wp_editor( '', 'content' ); ?>

            <p><?php wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=category' ); ?></p>

            <p><label for="post_tags"><?php echo esc_html__('Tags','theme-domain'); ?></label>

            <input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p>

            <input type="file" name="post_image" id="post_image" aria-required="true">

            <p><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>
        
        </form>
    <?php
    }

Step 2. Capturing the input from the front-end user post submission form. You can also put this code into your theme functions.php or any other file.

function themedomain_post_if_submitted() {
    // Stop running function if form wasn't submitted
    if ( !isset($_POST['title']) ) {
        return;
    }

    // Add the content of the form to $post as an array
    $post = array(
        'post_title'    => $_POST['title'],
        'post_content'  => $_POST['content'],
        'post_category' => array($_POST['cat']), 
        'tags_input'    => $_POST['post_tags'],
        'post_status'   => 'draft',   // Could be: publish
        'post_type'     => 'post' // Could be: 'page' or your CPT
    );
    $post_id = wp_insert_post($post);
    
    // For Featured Image
    if( !function_exists('wp_generate_attachment_metadata')){
        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
        require_once(ABSPATH . "wp-admin" . '/includes/file.php');
        require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    }
    if($_FILES) {
        foreach( $_FILES as $file => $array ) {
            if($_FILES[$file]['error'] !== UPLOAD_ERR_OK){
                return "upload error : " . $_FILES[$file]['error'];
            }
            $attach_id = media_handle_upload( $file, $post_id );
        }
    }
    if($attach_id > 0) {
        update_post_meta( $post_id,'_thumbnail_id', $attach_id );
    }

    echo 'Saved your post successfully! :)';
}

Step 3. Paste the form shortcode where you want to display the form

<?php echo do_shortcode('[themedomain_frontend_post]'); ?>
1
  • This is copy-pasted from techsolutionshere.com/… Did you write that yourself originally, are you Asia Khanom? Else I don't think that's really allowed.
    – Rup
    May 10, 2022 at 9:35

Your Answer

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

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