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

admin2025-01-08  6

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>
  <br>
  <input type="submit" value="Create Post">
</form>

The "my_create_post.php" file is:

<?php
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 />";

    exit();
}
?>

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:

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!

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>
  <br>
  <input type="submit" value="Create Post">
</form>

The "my_create_post.php" file is:

<?php
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 />";

    exit();
}
?>

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:

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!

Share Improve this question asked Dec 22, 2021 at 18:57 PantelDPantelD 398 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The echo part should be at the end of your code. Keep that in mind, any echo or print should be at the end of your piece of code.

<?php
    if( $_POST['title_given'] ) {
        // 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 "Title given is: " . $_POST['title_given'] . "<br />";
        echo "wp_insert_post() returned: " . var_dump( $return_value ) . "<br />";
   
    }
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736267304a1186.html

最新回复(0)