Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Created September 29, 2014 02:58
Show Gist options
  • Save Shelob9/138e1dd9190b6884c5bf to your computer and use it in GitHub Desktop.
Save Shelob9/138e1dd9190b6884c5bf to your computer and use it in GitHub Desktop.
UPDATED VERSION FOR REST API v2: https://gist.github.com/Shelob9/e9c8ca48a37ba4c39796
(function($){
var url = JP_POST_EDITOR.root;
url += '/posts';
function getPostsByUser( defaultID ) {
url += '?filter[author]=';
url += JP_POST_EDITOR.userID;
url += '&filter[posts_per_page]=20';
$.ajax({
type:"GET",
url: url,
dataType : 'json',
success: function(response) {
var posts = {};
$.each(response, function(i, val) {
$( "#posts" ).append(new Option( val.title, val.ID ) );
});
if ( undefined != defaultID ) {
$('[name=posts]').val( defaultID )
}
}
});
}
$( document ).ready( function() {
getPostsByUser();
});
$( '#select-post' ).on( 'submit', function(e) {
e.preventDefault();
var ID = $( '#posts' ).val();
var postURL = JP_POST_EDITOR.root;
postURL += '/posts/';
postURL += ID;
$.ajax({
type:"GET",
url: postURL,
dataType : 'json',
success: function(post) {
var title = post.title;
var content = post.content;
var postID = postID;
$( '#editor #title').val( title );
$( '#editor #content').val( content );
$( '#select-post #posts').val( postID );
}
});
});
function results( val ) {
$( "#results").empty();
$( "#results" ).append( '<div class="post-title">' + val.title + '</div>' );
$( "#results" ).append( '<div class="post-content">' + val.content + '</div>' );
}
$( '#editor' ).on( 'submit', function(e) {
e.preventDefault();
var title = $( '#title' ).val();
var content = $( '#content' ).val();
console.log( content );
var JSONObj = {
"title" :title,
"content_raw" :content,
"status" :'publish'
};
var data = JSON.stringify(JSONObj);
var postID = $( '#post-id').val();
if ( undefined !== postID ) {
url += '/';
url += postID;
}
$.ajax({
type:"POST",
url: url,
dataType : 'json',
data: data,
beforeSend : function( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', JP_POST_EDITOR.nonce );
},
success: function(response) {
alert( JP_POST_EDITOR.successMessage );
getPostsByUser( response.ID );
results( response );
},
failure: function( response ) {
alert( JP_POST_EDITOR.failureMessage );
}
});
});
})(jQuery);
<?php
/*
Plugin Name: JP REST API Post Editor
*/
add_shortcode( 'JP-POST-EDITOR', 'jp_rest_post_editor_form');
function jp_rest_post_editor_form( ) {
$form = '
<form id="select-post">
<select id="posts" name="posts">
</select>
<input type="submit" value="Choose Post To Edit" id="choose-post">
</form>
<form id="editor">
<input type="text" name="title" id="title" value="Hello there">
<textarea id="content" ></textarea>
<input type="hidden" name="post-id" id="post-id" value="">
<input type="submit" value="Submit" id="submit">
</form>
<div id="results">
</div>
';
if ( is_user_logged_in() ) {
if ( user_can( get_current_user_id(), 'edit_posts' ) ) {
return $form;
}
else {
return __( 'You do not have permissions to edit posts.', 'jp-rest-post-editor' );
}
}
else {
return sprintf( '<a href="%1s" title="Login">%2s</a>', wp_login_url( get_permalink( get_queried_object_id() ) ), __( 'You must be logged in to edit posts, please click here to log in.', 'jp-rest-post-editor') );
}
}
add_action( 'wp_enqueue_scripts', 'jp_rest_api_scripts' );
function jp_rest_api_scripts() {
wp_enqueue_script( 'jp-api-post-editor', plugins_url( 'jp-api-post-editor.js', __FILE__ ), array( 'jquery' ), false, true );
wp_localize_script( 'jp-api-post-editor', 'JP_POST_EDITOR', array(
'root' => esc_url_raw( get_json_url() ),
'nonce' => wp_create_nonce( 'wp_json' ),
'successMessage' => __( 'Post Created Successfully.', 'jp-rest-post-editor' ),
'failureMessage' => __( 'An error occurred.', 'jp-rest-post-editor' ),
'userID' => get_current_user_id(),
) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment