So I have a plugin that creates some images like a gallery. I add it to the page with shortcode:
add_shortcode('foo', 'foo_add_gallery');
I would like to create an option so that the gallery can be previewed in the wordpress admin area. I have managed to make it work like so:
1.
Normally I add my gallery scripts and styles to the frontend with
add_action('wp_enqueue_scripts', 'foo_enqueue_scripts');
Now I need to add them to the backend as well if I want to view the gallery, so I need to run
add_action('admin_enqueue_scripts', 'foo_admin_enqueue_scripts');
(This is where scripts and styles for plugin admin are already added, and I need to add my frontend script and styles in here as well)
2.
I run ajax when I want to preview the gallery and in there I call the same function shortcode would call:
var postData = [
{name: 'action', value: 'foo_show_gallery_backend'},
{name: 'gallery_id', value: gallery_id}
];
$.ajax({
url: foo_data.ajax_url,
type: 'post',
data: postData,
dataType: 'json',
}).done(function(response){
console.log( response)
$('#some-div').html(response)
}).fail(function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR, textStatus, errorThrown);
});
add_action('wp_ajax_foo_show_gallery_backend', 'foo_show_gallery_backend');
function foo_show_gallery_backend(){
$data = foo_add_gallery($atts);
echo json_encode($data);
wp_die();
}
(My foo_add_gallery function creates some html and javascript and adds this to the page)
Then I add this response to the page in admin area in a div.