Categories
General

Adding a quick post box to your theme

Since I added a sidebar login form to my group blog, I’ve wanted to add a way for logged-in folks to make a quick post without going to the WordPress backend. I learned about the Prologue theme via a Twitter post about its successor, P2. There’s a comment at Lorelle’s blog pointing to where to get P2 via SVN, but it’s pretty complicated AJAX-y and JSON-y stuff – the original Prologue looked much easier to borrow from.

First, you’ll need the post form. You can put the form directly in your index.php, or it in a separate file (I called it post-form.php):

<?php $user = get_userdata( $current_user->ID ); ?>

<div id="postbox">
<h3><a class="togglepost" style="cursor:pointer">Make a quick post &raquo;</a></h3>
<div class="showhidepost">
<form id="new_post" name="new_post" method="post" action="<?php bloginfo( 'url' ); ?>/">
<input type="hidden" name="action" value="post" />
<?php wp_nonce_field( 'new-post' ); ?>
<textarea name="posttext" id="posttext" style="width: 100%;"></textarea><br />
<strong>Title:</strong> <input size="40" type="text" name="posttitle" id="posttitle" /> <input id="submit" type="submit" value="Post it &raquo;" />
</form>
</div>
</div>

Next, add code to detect a logged-in user with publishing permission, and either include the form or echo it out:

if( current_user_can( 'publish_posts' ) ) {
require_once dirname( __FILE__ ) . '/post-form.php';
}

Finally, add some code to your index.php to process submitted posts:

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'post' ) {
if ( ! is_user_logged_in() )
auth_redirect();
if( !current_user_can( 'publish_posts' ) ) {
wp_redirect( get_bloginfo( 'url' ) . '/' );
exit;
}
check_admin_referer( 'new-post' );
$user_id = $current_user->user_id;
$post_content = $_POST['posttext'];
$post_title = $_POST['posttitle'];
$post_id = wp_insert_post( array(
'post_author' => $user_id,
'post_title' => $post_title,
'post_content' => $post_content,
'post_status' => 'publish'
) );
wp_redirect( get_bloginfo( 'url' ) . '/' );
exit;
}

Prologue has a tags field, which I replaced with a title field – it’d be easy to add any of the fields from the backend posting form you think are important. It might also be interesting to make a template for a quick post page separate from the main blog, like a bulletin board. I think for that I’d put back Prologue’s auto-title feature, and add a hidden category field to make it easy to filter quick posts off the main blog.

Avatar photo

By isaac

I like cats. he/him