• Resolved mdevaney

    (@mdevaney)


    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 ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Don’t bump. I’ve deleted it.

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

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

    Thread Starter mdevaney

    (@mdevaney)

    @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.]

    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);
    ?>
    Thread Starter mdevaney

    (@mdevaney)

    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.

    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]

    Thread Starter mdevaney

    (@mdevaney)

    We figured it out lol, thanks tho.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Proper way to create a 'add new page' function’ is closed to new replies.