How to let a user create a post by submitting a POST form?


So i have the following simple POST form in my home page:

<form action="/wp-content/plugins/myplugin/my_create_post.php" method="POST">
  <label for="title_given">Title:</label>
  <textarea id="title_given" name="title_given" rows="3" cols="100" maxlength="150"></textarea>
  <input type="submit" value="Create Post">

The “my_create_post.php” file is:

if( $_POST['title_given'] ) {
    // This echo successfully shows the title_given from the form
    echo "Title given is: " . $_POST['title_given'] . "<br />";

    // Create post object
    $my_post = array(
      'post_title'    => wp_strip_all_tags( $_POST['title_given'] ),
      'post_content'  => 'Did it work??',
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_type'     => 'post'

    // Insert the post into the database
    $return_value = wp_insert_post( $my_post, true );   
    // This echo never appears for some reason
    echo "wp_insert_post() returned: " . var_dump( $return_value ) . "<br />";


I am running all my tests as an admin. When i click the submit button i see a white page with only the first echo successfully printed and the following error in the console:

enter image description here

No post seems to ever be created while checking from the admin page or from the mysql cli. Ideally the user should be redirected to a new post that has the title he submitted.
Any insight on what i am doing wrong? or if there is a better way to accomplish my task without wp_insert_post()? Thanks!

PantelD 2 years 2021-12-22T13:57:50-05:00 0 Answers 0 views 0

Leave an answer