I need to ask a user to confirm the delete of his post. I managed it using the confirm function from jQuery but I wanted to create an overlay box with more customization options and after browsing the web I came up with this:
Info: CMS in question is Wordpress
So first the theme: index.php:
<li class="EditDel">
<?php u_delete_post_link('Delete', '', ''); ?>
</li>
PHP function:
function u_delete_post_link($link = 'Delete This', $before = '', $after = '') {
global $post;
$message = "Are you sure you want to delete ".get_the_title($post->ID)." ?";
$delLink = wp_nonce_url( get_bloginfo('url') . "/wp-admin/post.php?action=delete&post=" . $post->ID, 'delete-post_' . $post->ID);
$htmllink = "<a href='' onclick = \" u_ask_go('".$message."','".$delLink."' ) \"/>".$link."</a>";
echo $before . $htmllink . $after;
}
Now for the actual problem the link above fires a js function with the message and the link:
function u_ask_go(msg,link)
{
u_confirm(msg,link, function () {
window.location.href = arguments[0];
});
}
and the u_confirm function:
function u_confirm(message, link, callback) {
jQuery('#confirm').modal({
closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
position: ["20%",],
overlayId: 'confirm-overlay',
containerId: 'confirm-container',
onShow: function (dialog) {
var modal = this;
modal.link = link;
//console.log(modal.link);
jQuery('.message', dialog.data[0]).append(message);
// if the user clicks "yes"
jQuery('.yes', dialog.data[0]).click(function () {
// call the callback
if (jQuery.isFunction(callback)) {
//console.log(modal.link);
callback.apply(modal.link);
}
// close the dialog
modal.close(); // or $.modal.close();
});
}
});
}
Problem1: The callback function does not fire
Problem2: The dialog collapses and the link executes without user pressing anything
Observation: Before the modal div is appended the link variable is visible but not in the function on the dialog yes button seems not visible
Hope someone can help
EDIT (HTML buildup):
<!-- modal content -->
<div id='confirm'>
<div class='header'><span>Confirm</span></div>
<div class='message'></div>
<div class='buttons'>
<div class='no simplemodal-close'>No</div><div class='yes'>Yes</div>
</div>
</div>
<!-- preload images -->
<div style='display:none'>
<img src='img/confirm/header.gif' alt='' />
<img src='img/confirm/button.gif' alt='' />
</div>
EDIT2:
echo wp_enqueue_script('simplemodal', $blogroot.'/js/jquery.js');
echo wp_enqueue_script('simplemodal', $blogroot.'/js/jquery.simplemodal.js', array("jquery"));
echo wp_enqueue_script('confirmtest', $blogroot.'/js/confirmtest.js', array("jquery"));