I’ve installed your plugin and it doesn’t work.
Feeds counter are updated correctly, but no feed are shown (and so no ‘Press this’ buttons).
After that when I install it I see several notices: are you testing your plugin with wp debugging active?
The best way, for me, is redirect users to the standard wordpress post creation page. But how pre populated it?
After a quick look on your code, I’ll give you the bones of a solution that should be improved and tailored to your plugin.
Sure there are more elegant way, but first coming into my mind is:
- create and save a post with selected feed content, and set status as auto-draft.
- redirect users to edit page for the just created posts
Something like:
Javascript: in the file ‘js/myblogthis.js’
jQuery().ready(function($) {
$('.entry-tools a').click(function(e) {
e.preventDefault();
var $entry = $(this).closest('.entry');
var $titleObj = $entry.find('.entry-title');
var _title = $titleObj.text();
var _url = $titleObj.parent().attr('href');
var _by = $entry.find('.author').text();
var _date = $entry.find('.date').text();
var _content= $entry.find('.entry-content').html();
$.ajax({
url: ajaxurl, type: 'POST', dataType: 'json',
data: {
title: _title, url: _url, by: _by, date: _date, content: _content,
action: 'blog_this', nonce: myblogthisData.nonce
}
}).done(
function( data ) {
if ( data && data.url ) { window.location.href = data.url
} else { alert( "Error!" ); }
}
);
});
});
PHP
add_action('admin_enqueue_scripts', 'enqueue_blog_this_js');
add_action('wp_ajax_blog_this', 'my_blog_this');
function enqueue_blog_this_js($page) {
if ( $page != 'toplevel_page_orbital' ) return;
wp_enqueue_script('myblogthis', plugins_url('js/myblogthis.js', __FILE__), array(), null );
$data = array( 'nonce' => wp_create_nonce('myblogthis') );
wp_localize_script('myblogthis', 'myblogthisData', $data);
}
function my_blog_this() {
error_reporting(0);
if ( ! isset($_POST['nonce']) || ! wp_verify_nonce($_POST['nonce'], 'myblogthis') )
wp_send_json( array('error' => 'Security Fail') );
$title = isset($_POST['title']) ? $_POST['title'] : '';
$url = isset($_POST['url']) ? $_POST['url'] : '';
$by = isset($_POST['by']) ? $_POST['by'] : '';
$date = isset($_POST['date']) ? $_POST['date'] : '';
$content = isset($_POST['content']) ? $_POST['content'] : '';
if ( $title && $url && $by && $date) {
$content .= sprintf(
'<p>Originally posted by: %s on %s (see: <a href="https://wordpress.stackexchange.com/questions/112508/%s">%s</a>)</p>',
esc_html($by), esc_html($date), esc_url($url), esc_html($title)
);
}
$args = array(
'post_status' => 'auto-draft',
'post_title' => '',
'post_author' => wp_get_current_user()->ID,
'post_content' => $content
);
$id = wp_insert_post($args);
if ( $id ) {
$url = add_query_arg( array('action'=> 'edit', 'post'=> $id), admin_url('post.php') );
wp_send_json( array('url'=> $url ) );
}
wp_send_json( array('error' => 'Saving Post Error') );
}
As you can see, I tried to create js according to the html outputted by ‘mainwindow.php’.
The main between the method you are using and the mine is that you let users select a text to re-blog, while I use the entire content.
But with some js improvements maybe you can modify this behaviour.