WordPress – allow an anonymous user to submit posts?

Following this guide, I am trying to create my own anonymous user submitted post plugin.

Below is my code so far:

Read More
function suq_form_shortcode() {

    if (
        isset($_POST['suq_form_create_quote_submitted'])
        && wp_verify_nonce($_POST['suq_form_create_quote_submitted'],'suq_form_create_quote')
    ) {

        $suq_quote_title = trim($_POST['suq_quote_title']);
        $suq_quote_author = trim($_POST['suq_quote_author']);
        $suq_quote_text = trim($_POST['suq_quote_text']);

        if ($suq_quote_author != '' && $suq_quote_text != '') {

            $quote_data = array(
                'post_title' => $suq_quote_title,
                'post_content' => $suq_quote_text,
                'post_status' => 'pending',
                'post_author' => $suq_quote_author,
                'post_type' => 'post'
            );

            if($quote_id = wp_insert_post($quote_data)){

              echo '<p>Quote created and awaiting moderation!</p>';

            }

        } else { // author or text field is empty

            echo '<p>Quote NOT saved! Who said it? and Quote must not be empty.</p>';

        }
    }

    echo suq_get_create_quote_form($suq_quote_author, $suq_quote_text, $suq_quote_category);

}

function suq_get_create_quote_form ($suq_quote_author = '', $suq_quote_text = '', $suq_quote_category = 0) {

    $out .= '<form id="create_quote_form" method="post" action="">';

    $out .= wp_nonce_field('suq_form_create_quote', 'suq_form_create_quote_submitted');

    $out .= '<label for="suq_quote_author">Name</label><br/>';
    $out .= '<input type="text" id="suq_quote_author" name="suq_quote_author" value="' . $suq_quote_author . '"/><br/>';


    $out .= '<label for="suq_quote_title">Title</label><br/>';
    $out .= '<input type="text" id="suq_quote_title" name="suq_quote_title" value=""/><br/>';

    $out .= '<label for="suq_quote_text">Quote</label><br/>';
    $out .= '<textarea id="suq_quote_text" name="suq_quote_text" />' . $suq_quote_text . '</textarea><br/><br/>';
    $out .= '<input type="submit" id="suq_submit" name="suq_submit" value="Submit Quote For Publication">';

    $out .= '</form>';

    return $out;
}

It works fine as I can see the submitted posts in my admin area, but it one of info is missing which is the author name that user submitted.

enter image description here

Any ideas how I can show the author name that provided by the user themselves?

I have passed the author name to WP:

'post_author' => $suq_quote_author,

But WP does not seem to pick it up…

Related posts

Leave a Reply

1 comment

  1. post_author Take (int) The ID of the user who added the post. Default is the current user ID

    In your case “Anonymous user” can submit the post. No user related to your post save in user table so save this “Anonymous user name” in post meta table and ‘post_author’ = admin user id (1)

    $quote_data = array(
                    'post_title' => $suq_quote_title,
                    'post_content' => $suq_quote_text,
                    'post_status' => 'pending',
                    'post_author' => 1, // admin user id 
                    'post_type' => 'post',
    );
    $quote_id = wp_insert_post($quote_data);
    update_post_meta ( $quote_id,'anonymous_user',$suq_quote_author);
    

    To display this fields in post listing at backend. Add this function in function.php file

    function add_anonymous_user_column( $columns ) {
        return array_merge( $columns, 
            array( 'anonymous_user' => __( 'Anonymous user Name', 'your_text_domain' ) ) );
    }
    add_filter( 'manage_posts_columns' , 'add_anonymous_user_column' );
    

    For get the value of Anonymous user name add this function in function.php file

    function display_posts_display_posts_anonymous_user( $column, $post_id ) {
        if ($column == 'anonymous_user'){
            echo get_post_meta ($post_id,'anonymous_user',true);
        }
    }
    add_action( 'manage_posts_custom_column' , 'display_posts_display_posts_anonymous_user', 10, 2 );
    

    If you remove the default “Author” from the post listing used unset($columns['author']); in add_anonymous_user_column() function

    function add_anonymous_user_column( $columns ) {
         unset($columns['author']);
        return array_merge( $columns, 
            array( 'anonymous_user' => __( 'Anonymous user Name', 'your_text_domain' ) ) );
    }
    add_filter( 'manage_posts_columns' , 'add_anonymous_user_column' );
    

    enter image description here