Created
December 7, 2014 06:21
-
-
Save dcondrey/58072edd29a56cb1c275 to your computer and use it in GitHub Desktop.
Basic WP plugin, front-end editing with ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready( function($) { | |
$('.entry-title a').after( ' <button class="edit-title">EDIT</button>'); | |
$('.entry-title').on( 'click', '.edit-title', function(ev) { | |
ev.preventDefault(); | |
var postId = $(this).closest('article.post').attr('id').replace('post-',''); | |
$a = $(this).prev('a'), | |
title = $a.text(), | |
newTitle = prompt( 'New Title', title ); | |
if ( '' == newTitle || null == newTitle ) return; | |
$.post( fete.ajaxUrl, { | |
action: 'fete_edit_title', | |
post_id: postId, | |
new_title: newTitle, | |
nonce: fete.nonce | |
}, function( response ) { | |
console.log( response ); | |
if ( ! response.success ) { | |
alert( 'Error: '+ response.data ); | |
return; | |
} | |
$a.html( response.data ); | |
}, 'json' ); | |
} ); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Front end title editing | |
* Description: Tested only with Twenty Fourteen | |
* Plugin URI: http://trepmal.com/2014/01/18/stronger-than-dirt-i-mean-ajax/ | |
*/ | |
add_action( 'wp_enqueue_scripts', 'fete_wp_enqueue_scripts' ); //front end | |
function fete_wp_enqueue_scripts() { | |
// don't bother for logged out users | |
if ( ! is_user_logged_in() ) return; | |
wp_enqueue_script( 'fete-script', plugins_url( 'front-end-title-edit.js', __FILE__ ), array('jquery') ); | |
wp_localize_script( 'fete-script', 'fete', array( | |
'ajaxUrl' => admin_url('admin-ajax.php'), | |
'nonce' => wp_create_nonce( 'security' ) | |
) ); | |
?><style> | |
.edit-title { | |
height: 20px; | |
padding: 2px 4px; | |
font-size: 14px; | |
vertical-align: middle; | |
font-weight: normal; | |
} | |
</style><?php | |
} | |
add_action('wp_ajax_fete_edit_title', 'fete_edit_title'); | |
function fete_edit_title() { | |
// verify intent | |
if ( ! check_ajax_referer( 'security', 'nonce', false ) ) { | |
wp_send_json_error( 'Failed nonce check' ); | |
} | |
$new_title = $_POST['new_title']; | |
$post_id = $_POST['post_id']; | |
// verify capabilities | |
if ( ! current_user_can( 'edit_post', $post_id ) ) { | |
wp_send_json_error( 'Sorry Dave.' ); | |
} | |
$test = wp_update_post( array( | |
'ID' => $post_id, | |
'post_title' => $new_title | |
) ); | |
// check if we succeeded | |
if ( is_wp_error( $test ) ) { | |
wp_send_json_error( 'Update failed.' ); | |
} else { | |
wp_send_json_success( get_the_title( $test ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment