Skip to content

Instantly share code, notes, and snippets.

@pipiscrew
Last active March 11, 2017 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pipiscrew/89576983967179fd1c23203fcf46ec40 to your computer and use it in GitHub Desktop.
Save pipiscrew/89576983967179fd1c23203fcf46ec40 to your computer and use it in GitHub Desktop.
WordPress - Insert Post to wordpress from your PHP custom application. Login form supported
<?php
@session_start();
require_once('../wp-load.php');
//when form submited
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
//server validation - if called directly HTTP_REFERER is not set
if(!isset($_SERVER["HTTP_REFERER"])){
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
exit;
} else {
$ref = $_SERVER['HTTP_REFERER'];
$ref_details = parse_url($ref);
//array(3) { ["scheme"]=> string(4) "http" ["host"]=> string(10) "domain.com" ["path"]=> string(11) "/thefolder/" }
if (!array_key_exists('host', $ref_details)){
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
exit;
}
if (strtolower($ref_details["host"]) != "domain.com"){
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
exit;
}
}
if(!isset($_POST["login"]) || !isset($_POST["password2"]) || !isset($_POST["btn_login"])){
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
exit;
}
//save to session wrong or right!
$_SESSION["password1"] = $_POST["login"];
$_SESSION["password2"] = $_POST["password2"];
}
if (!isset($_SESSION["password1"]) || !isset($_SESSION["password2"]) ){ ?>
<form method="post">
<input name='login'>
<input name='password2'>
<button name='btn_login'>save</button>
</form>
<?php
exit;
}
if($_SESSION["password1"]=="password1" && $_SESSION["password2"] == "password2"){
//good boy
} else {
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
exit;
}
$pk_categories = get_categories(array('hierarchical'=> false));
$cats = array();
foreach($pk_categories as $pk_category) {
$cats[] = array('cat_id' => $pk_category -> term_id , 'cat_name' => $pk_category -> cat_name);
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<script type='text/javascript' src='jquery-3.1.1.min.js'></script>
<script src="bootstrap-selector.js"></script>
<script>
// content
</script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-3">
<div id="cats" class="list-group centre" ></div>
</div>
<div class="col-md-9">
<form method="post" action="submit.php">
<div class="form-group">
<label>Title :</label>
<input name="title" class="form-control" placeholder="title" required>
</div>
<div class="form-group">
<label>Detail :</label>
<textarea name="txt" class="form-control" rows="8" style="resize: none;" required></textarea>
</div>
<input id="categories" name="categories" type="hidden">
<button class="btn btn-success" style="float:right" name="btn" type="submit">publish</button>
</form>
</div>
</div>
</div>
</body>
</html>
<?php
@session_start();
require_once('../wp-load.php');
//server validation - if called directly HTTP_REFERER is not set
if(!isset($_SERVER["HTTP_REFERER"])){
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
exit;
} else {
$ref = $_SERVER['HTTP_REFERER'];
$ref_details = parse_url($ref);
//array(3) { ["scheme"]=> string(4) "http" ["host"]=> string(10) "domain.com" ["path"]=> string(11) "/thefolder/" }
if (!array_key_exists('host', $ref_details)){
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
exit;
}
if (strtolower($ref_details["host"]) != "domain.com"){
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
exit;
}
}
//form validation
if ($_SERVER["REQUEST_METHOD"] != "POST" || !isset($_POST['title']) || !isset($_POST['txt']) || !isset($_POST['categories']) || !isset($_POST['btn'])){
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
exit;
}
//login validation
if($_SESSION["password1"]=="password1" && $_SESSION["password2"] == "password2"){
//good boy
} else {
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
exit;
}
//turn categories to array
$cats = explode(',', $_POST["categories"]);
$post_title = wp_strip_all_tags($_POST['title']);
$post_content = wp_strip_all_tags($_POST['txt']);
// Create post object
$my_post = array(
'post_title' => $post_title,
'post_content' => $post_content,
'post_status' => 'publish',
'post_author' => '1',
'post_category' => $cats
);
if (wp_insert_post( $my_post ))
echo "saved";
else
echo "error";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment