Wow, you know, after I posted my last post, I began to see some serious problems with my approach with the $_SESSION superglobal. It seems that the superglobal just didn’t act the way I expected. It took me some time to find the right approach that would resolve all the issues and consistently give me the popups and the “Access Denied” pages as I originally intended, so I will post my solution here for all the world to see just in case there is anyone else out there looking for a similar solution.
My final solution involved using a form, a link and some javascript. I had to use a fake submit button in order to invoke the onsubmit event handler. The solution is as follows:
In the popup template:
<?php
/**
* @package WordPress
* @subpackage Your_Theme
*/
/*
Template Name: Popup
*/
if ($_POST['popup'] != "true") {
header( 'Location: https://your.url/?page_id=yourpageid');
// Place your "Access Denied" page in the your.url info above.
} else {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
<!-- The rest of your popup template goes here. Don't forget to close out the PHP if statement.
In the javascript file (don’t forget to include your reference to this file in the header.php file):
function get_popup(popup_window, form_name, height, width) {
window.open(popup_window, 'popup_window', 'width=' + width + ', height=' + height + ', resizable=yes, scrollbars=yes, toolbar=no, menubar=no, left=0, top=0').focus();
eval('document.' + form_name + '.target = "popup_window"');
return true;
}
function submitform(form_name) {
eval('document.' + form_name + '.fakeSubmitButton.click()');
}
And finally at the link:
<form name="yourformname" action="yourtemplateurl" method="post" onSubmit="return get_popup('yourtemplateurl', 'yourformname', yourwindowheight, yourwindowwidth)"><input type="hidden" name ="popup" value="true">
<div class="yourlink"><a href="javascript:submitform('yourformname');">Your Link</a></div><input type="submit" name="fakeSubmitButton" style="display: none"/>
</form>
This code works like a charm for me. I hope it helps someone else.
Have a great day!