I'm looking for a way to open every/any link to a blog post in a lightbox/modal globally and without having to any extra classes or rel attribute to the link element. I know I can access this info from within the page or post itself, but is there a way, through some sort of AJAX call I presume, to get this data before the request for the content is actually made so that when I do retrieve the response content, I can either just display it normally in the same browser tab (in the case of a Page) or trigger a lightbox and populate it with the content instead.
At first I was thinking about using regular expression matching on the link element's href and if it matched something looking like a blog post URL, open the lightbox. that seems pretty hacky tho and is definitely not bulletproof.
This is what I've got so far...
JavaScript:
$('a:not([href^="#"])[href]').click(function(e) {
e.preventDefault();
var $this = $(this);
var lnk = $this.attr('href');
var jqxhr = $.post("/wp-content/themes/mist-child/php/get-post-type.php", {url: lnk});
jqxhr.done(function(data) {
if(data === 'post') {
$.prettyPhoto.open(lnk + '?iframe=true&width=80%&height=80%');
}
});
});
get-post-type.php (/wp-content/themes/CHILD_THEME/php/...)
<?php
require_once(rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/wp-load.php');
$url = $_POST['url'];
$postid = url_to_postid($url);
echo(get_post_type($postid));
?>
The part where Posts get opened in the lightbox is working. It's a little slow - I'm guessing that loading wp-load.php adds quite a bit of overhead. is there any way to add my PHP somewhere in functions.php and call that function (with arguments) via AJAX?
I know it needs some work, but as far as a prototype goes, am I moving in the right direction? Please feel free to shoot this full of as many holes as you can find.
post_link
and/orpage_link
filters. A possible solution is to add a rewrite endpoint, which you can append to whatever post type you want, and detect on the front end via javascript, without having to make that extra request.