I have an admin plugin that has a list of articles in it with a button "Add to Posts" along side. On the click of that button, I want to redirect to "/wp-admin/post-new.php" with the form pre filled.
I can set the title in the url like wp-admin/post-new.php?post_type=post&post_title=My Titlle
. But, how can I pre fill the content?
I read some articles like this , but its not what I am looking for.
Also, the content is going to be different each time, so I don't want to set it as default.
What I am doing right now :
My jQuery on button click :
function add_to_post(id) {
var data = {
'action' : 'add_to_post',
'id' : id
};
$.ajax({
type : 'POST',
url : ajaxurl,
data : data
})
.done(function(){
var title = $(document.getElementById('title_'+id)).text();
var link = host+"/wp-admin/post-new.php?post_type=post&post_title="+title;
window.open(link,"_blank");
})
;
}
My Plugin code for Action
add_action('wp_ajax_add_to_post','add_to_post_callback');
function add_to_post_callback() {
add_filter( 'default_content', 'my_editor_content', 10 , 2 );
wp_die();
}
function my_editor_content( $content ) {
$content = "This is some custom content I'm adding to the post editor because I hate re-typing it.";
return $content;
}
But when I click on "Add to post" button, the content is still empty I would appreciate any help.
Aman.
WordPress has a filter for default content:
add_filter( 'default_content', 'set_default_content', 10, 2 );
function set_default_content( $content, $post ) {
$content = ...your content...;
return $content;
}
You can just use a variable for the content and change it whenever you like.
This is what I ended up doing :
in jQuery :
function add_to_post(feed_id) {
var title = $(document.getElementById('title_'+feed_id)).text();
var content = $(document.getElementById('summary_'+feed_id)).text();
var link = host+"/wp-admin/post-new.php";
var data = {
'post_title' : title,
'pre_content' : content,
'post_type' : 'post'
};
$.extend({
redirectPost: function(location, args) {
var form = '';
$.each( args, function( key, value ) {
form += '<input type="hidden" name="'+key+'" value="'+value+'">';
});
$('<form target = "_blank" action="'+location+'" method="POST">'+form+'</form>').appendTo('body').submit();
}
});
// sending content as post because Get request has character limit of 2048. Just taking no chances.
$.redirectPost(link,data);
}
In PHP
//To add content to your post.
add_filter( 'default_content', 'my_editor_content', 10 , 2 );
function my_editor_content( $content , $post ) {
if(isset($_POST['pre_content'])) {
$content = $_POST['pre_content'];
}
return $content;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.