Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active February 21, 2024 21:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save igorbenic/7b8f817ea1eb6be92d23 to your computer and use it in GitHub Desktop.
Save igorbenic/7b8f817ea1eb6be92d23 to your computer and use it in GitHub Desktop.
Show a WordPress Post in a Modal - Article on Ibenic.com
<?php
/**
* Plugin Name: IBenic Bootstrap Modal
* Plugin URI: http://www.ibenic.com/show-a-wordpress-post-in-a-modal/
* Description: Show an article in a modal on the same page.
* Version: 1.0
* Author: Igor benić
* Author URI: http://www.ibenic.com
* License: GPL2
*
*
*
* Copyright 2014 Igor Benić (email : i.benic@hotmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
function ibenic_add_style_and_script() {
wp_enqueue_style( 'ibenic-bootstrap-css', "https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" );
wp_enqueue_script( 'ibenic-bootstrap-js', "https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js", array('jquery'), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'ibenic_add_style_and_script' );
<?php
function ibenic_add_bootstrap_modal(){
?>
<!-- Modal -->
<div class="modal fade" id="ibenicModal" tabindex="-1" role="dialog" aria-labelledby="ibenicLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="modalTitle">Modal title</h4>
</div>
<div class="modal-body" id="modalBody">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php
}
add_action('wp_footer','ibenic_add_bootstrap_modal');
<?php
function ibenic_show_post_in_modal( $atts ) {
$attributes = shortcode_atts( array(
'id' => 0,
'text' => "Open post in modal",
'class' => "btn btn-primary",
'style' => ''
), $atts );
$ajax_nonce = wp_create_nonce( "ibenic-bootstrap" );
?>
<script>
function ibenic_show_post_js(id){
//Here we will create the JavaScript code
}
</script>
<a style="<?php echo $attributes["style"]; ?>" class="<?php echo $attributes["class"]; ?>" onClick='ibenic_show_post_js(<?php echo $attributes["id"]; ?>)'><?php echo $attributes["text"]; ?></a>
<?php
}
add_shortcode( 'ibenic_post', 'ibenic_show_post_in_modal' );
<?php
function ibenic_show_post_js(id){
jQuery.ajax({
url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",
data: { id: id, action: 'ibenic_show_post', security: '<?php echo $ajax_nonce; ?>'},
success: function(response){
if(response['error'] == '1'){
jQuery('#modalTitle').html("Error");
jQuery('#modalBody').html("No post found! Sorry :(");
} else {
jQuery('#modalTitle').html(response['post_title']);
jQuery('#modalBody').html(response['post_content']);
}
jQuery('#ibenicModal').modal('show');
}
});
}
<?php
function ibenic_show_post_js(id){
jQuery.ajax({
url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",
data: { id: id, action: 'ibenic_show_post', security: '<?php echo $ajax_nonce; ?>'},
success: function(response){
if(response['error'] == '1'){
jQuery('#modalTitle').html("Error");
jQuery('#modalBody').html("No post found! Sorry :(");
} else {
jQuery('#modalTitle').html(response['post_title']);
jQuery('#modalBody').html(response['post_content']);
}
jQuery('#ibenicModal').modal('show');
}
});
}
<?php
add_action('wp_ajax_nopriv_ibenic_show_post', 'ibenic_show_post');
add_action('wp_ajax_ibenic_show_post', 'ibenic_show_post');
function ibenic_show_post(){
check_ajax_referer( 'ibenic-bootstrap', 'security' );
$id = $_GET['id'];
$post = get_post($id);
if($post){
wp_send_json(array('post_title' => $post->post_title, 'post_content' => $post->post_content));
} else {
wp_send_json(array('error' => '1'));
}
wp_die();
}
<?php
add_filter('widget_text', 'do_shortcode');
Only with an ID
[ibenic_post id=3692]
ID of the article with some other Bootstrap 3 classes
[ibenic_post id=3692 class="btn btn-warning"]
ID of the article and additional styles
[ibenic_post id=3692 style="margin:0 auto;width:100%"]
Button with a different text
[ibenic_post id=3692 text="Click to show the article"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment