How to Create Customized External Post Page in WordPress

Yea, am not that a WordPress fan or a good user but few months ago I was working with it and it was really cool, mainly I love working with backend so I decided to use some features of WordPress which led me into reading bulks of their docs and finally gave me something.

php-mysql-wordpress

Well, WordPress owners will like a situation where posts are being created outside the wp-admin which sucks seeing a user there, I thought of creating an external post page which I already knew some functions to do that in WordPress and success was mine at the end. So am gonna share this to you.

Create your PHP file and name post.php

  • Now lets add some core files needed for this operation which are already created by WordPress, paste the below code in the file
//for if user is logged in and user details
require_once('wp-includes/pluggable.php');
 
//All about your blog theme and designs
require_once('wp-blog-header.php');
 
//Handle all post functions
require_once('wp-includes/post.php');
 
//Get current user ID
require_once('wp-includes/user.php');
 
//Getting Post link
require_once('wp-includes/link-template.php');
  •  We are needing categories which the user can select and which the post is assigned to, then paste the below code next 🙂
//Details for category to fetch
$cat_opt = array(
    'child_of' => 0,
    'order_by' => 'name',
    'order' => ASC,
    'hide_empty' =>0
    );
  •  Essential files included, now lets drag in your current blog theme to the external page to make it look exactly like other pages of your blog
//Getting your blog header
get_header();
  •  Ah, do you want guest to access this page? Of course not else we won’t be able to get the current user id. Lets restrict it then
//If guests are trying to access this page, redirect them to login page
if(!is_user_logged_in()) {
    header('location: wp-login.php');
}
  •  I wanna work with HTML now, you can close the PHP end tag (?>) for now
<style type="text/css">
/*Our content block CSS */
    #content {
        border:1px solid gray;
        border-radius: 5px;
        box-shadow: 0px 0px 5px 0px black;
 
    }
</style>
  •  Now lets create our post options here, the inputs of post title, selecting category and body
<div id="content">
<form action="" method="post">
<table>
<tr>
    <td><strong>Topic:</strong></td>
    <td><input type="text" name="topic" placeholder="Topic Name" /></td>
</tr>
  •  Wait! Here might seem a bit confusing to you but let me put you through. Am using the WordPress built in function to get all categories which is in an array form, now using foreach() statement  to get them into a select input. Take a look below
<tr>
    <td><strong>Select Category:</strong></td>
    <td><select name="category"><?php
    foreach(get_categories($cat_opt) as $category) {
        ?><option value="<?php echo $category->term_id?>"><?php echo $category->name?></option><?php
    } ?></select></td>
</tr>
  •  I guess you ain’t lost ye? 😀 If so please do use the comments 🙂
  • Now lets finish up with the HTML
<tr>
    <td><strong>Post Body:</strong></td>
    <td><textarea name="body" style="height:170px; width:300px" placeholder="Post Body"> </textarea></td>
</tr>
<tr>
    <td></td>
    <td><input type="submit" value="Post" /></td>
</tr>
</table>
</form></div>
  •  Done with HTML, PHP follows in validation of inputs. Just paste this one below the <form> element
<?php
//Form input validations
if(isset($_POST['topic'])) {
    $post_topic = htmlentities(trim($_POST['topic']));
    $post_category = htmlentities(trim($_POST['category']));
    $post_body = htmlentities(trim($_POST['body']));
    if(!$post_topic or !$post_body or !$post_category) {
        echo 'Sorry, all fields needs input';
    }
  •  What next after validation? Now inserting the post into the database which means post is being created
else {
        $post= array(
            'post_title' => $post_topic,
            'post_content' => $post_body,
            'post_status' => 'publish',
            'post_author' => get_current_user_id(),
            'post_category' => $post_category
            );
            $post_id = wp_insert_post($post);
            header('location: '.get_permalink($post_id));
    }
}
?>
  •  We are still not done with the outlook, lets finish up what we started 🙂 adding the footer and sidebars if you want
<?php
//Getting side bars
get_sidebar();
 
//Getting the footer
get_footer();
?>

Yes! We are done, you can customize to your desire 🙂

Download full code here

Have fun