Question

Hi I'm looking for a very simple quick (no-plugins though) way to add a form inside a page template that will allow a logged-in user to post from outside WP-Admin. So basically the post will be under the logged-in author.

I have been looking around the web for some tutorials etc but haven't had too much luck and many seem to want to opt for plugins etc.

Can anyone help thanks.

Was it helpful?

Solution

You can use the wp_insert_post() function.

take a look at this. you can paste it in category.php for example and visit a category page and check it. but make sure to put the top code above the get_header() function.

 <?php 
    if(isset($_POST['new_post']) == '1') {
        $post_title = $_POST['post_title'];
        $post_category = $_POST['cat'];
        $post_content = $_POST['post_content'];

        $new_post = array(
              'ID' => '',
              'post_author' => $user->ID, 
              'post_category' => array($post_category),
              'post_content' => $post_content, 
              'post_title' => $post_title,
              'post_status' => 'publish'
            );

        $post_id = wp_insert_post($new_post);

        // This will redirect you to the newly created post
        $post = get_post($post_id);
        wp_redirect($post->guid);
    }      
    ?>

--

<!-- this form shows only if user is logged in -->
 <?php if ( is_user_logged_in() ) { ?>

<form method="post" action=""> 
    <input type="text" name="post_title" size="45" id="input-title"/>
    <?php wp_dropdown_categories('orderby=name&hide_empty=0&exclude=1&hierarchical=1'); ?>
    <textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea> 
    <input type="hidden" name="new_post" value="1"/> 
    <input class="subput round" type="submit" name="submit" value="Post"/>
</form>

<?php } ?>

for more info you should take a look here wp_insert_post with a form

good luck

OTHER TIPS

If you don't mind changing your theme, you might want to try P2: http://wordpress.org/extend/themes/p2 There's also a video of it in action here: http://p2theme.com/

P2 allows people to post straight from the home page, which seems like what your looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top