WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Proper way to create a 'add new page' function (8 posts)

  1. mdevaney
    Member
    Posted 3 years ago #

    The idea is for logged in users to add pages, without accessing the backend.

    This might be backwards, but thats why im posting it here. I'm wondering if anyone can help with with this code:

    createnewpage.php

    <?php
    $newTitle = $_POST['title'];
    $newContent = $_POST['content'];
    wp_insert_post($post)
    $post = array(
      'post_author' = $user_ID,
      'post_content' => $newContent,
      'post_name' =>  $newTitle,
      'post_status' => 'publish',
      'post_title' => $newTitle,
      'post_type' => 'page',
      'post_parent' => 0,
      'menu_order' => 0,
      'to_ping' =>  '',
      'pinged' => '',
    );
    ?>

    JQuery Pop-up would contain this:

    <html><body>
    <h1>Add A Page</h1>
    <form action="createnewpage.php" method="post">
    <input name="title" type="text" />
    <textarea name="content" type="text" /></textarea>
    <input type="submit" />
    </form>
    </body></html>

    But its wrong :(

  2. Don't bump. I've deleted it.

    Also this isn't multisite specific, so I'm moving it to advanced.

  3. GRAQ
    Member
    Posted 3 years ago #

    You have to assign the $post variable before calling the wp_insert_post() function.

  4. mdevaney
    Member
    Posted 3 years ago #

    @GRAQ can you provide an example?

    Can anyone give me an example of how to incorporate the wp_insert_post into this?

    [Stop bumping your posts. - Mod.]

  5. Kraignos
    Member
    Posted 3 years ago #

    You're close... What GRAQ said, is that you have to invert two lines to make it work.

    You're calling the wp_insert_post() function, trying to pass $post as a parameter, but $post is undefined at this moment, because you declare it only the line after.

    So, change your createnewpage.php to :

    <?php
    $newTitle = $_POST['title'];
    $newContent = $_POST['content'];
    $post = array(
      'post_author' = $user_ID,
      'post_content' => $newContent,
      'post_name' =>  $newTitle,
      'post_status' => 'publish',
      'post_title' => $newTitle,
      'post_type' => 'page',
      'post_parent' => 0,
      'menu_order' => 0,
      'to_ping' =>  '',
      'pinged' => '',
    );
    wp_insert_post($post);
    ?>
  6. mdevaney
    Member
    Posted 3 years ago #

    im still having issues with it, i'm not too sure what im doing wrong, i have that form, and i have that code in createnewpage.php but its causing errors.

  7. Kraignos
    Member
    Posted 3 years ago #

    Oh my .... I'm so blind. You say there's only this php code in createnewpage.php? Ok so I see where is the problem : you need to fill the $user_ID var, otherwise it'll be empty, resulting in an error.

    [Code moderated as per the Forum Rules. Please use the pastebin]

  8. mdevaney
    Member
    Posted 3 years ago #

    We figured it out lol, thanks tho.

Topic Closed

This topic has been closed to new replies.

About this Topic