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:
You can just use a variable for the content and change it whenever you like.
This is what I ended up doing :
in jQuery :
In PHP