Create posts without login from frontend

You should not allow anonymous users to publish anything on your website without authentication. If you need to store custom data that is specified by users, you should use the custom fields instead.

In your case, add_post_meta() comes in handy. After creating a post using wp_insert_post(), pass its ID to add_post_meta() and add custom fields to that specific post:

$id = wp_insert_post( $args );
if ( $id ) {
    add_post_meta( $id, $meta_key, $meta_value, $unique );
}

You should also sanitize the user input before inserting it into the database. You can use sanitize_text_field() for this very purpose.

Leave a Comment