I am developing a plugin which displays a wordpress post in a lightbox outside the loop. I noticed that some plugin which modify the contents of the post (like Facebook Like button plugin) use the get_permalink() function to retrieve post urls. When the post is opened normally (single.php) the plugins work fine. When the post is opened in the lightbox (outside the loop) , the plugins dont work since get_permalink() in the plugin does not return anything. however if i can somehow apply filter and return get_permalink($post_ID) instead, I can get the job done. I dont know how to hook it properly to get_permalink. I tried this, but this just makes it enter an infinite loop :

function cg_post_permalink(){ return get_permalink($post_ID); }

here I read that post_link was the hook for the get_permalink function

PS - please do not suggest using an iframe since it has its own set of complications. :)

  • 2
    how exactly are you rendering the posts in the lightbox? – Milo Nov 17 '12 at 4:57
  • by making an Ajax request to a .php file i have created. I use colorbox (see the Outside HTML example) I loaded the wordpress environment in the php file by adding wp-load.php – Anshul Nov 17 '12 at 5:25

I made an example plugin based on Milo's answer to show how you can use ajax to get a post and display it with colorbox.


Plugin Name: example

function example_ajax_get_post() {

    $url = $_POST['url'];
    $post = get_post(url_to_postid($url));

    <h2><?php print $post->post_title; ?></h2>
    <div><?php print $post->post_content; ?></div>


}// end function

add_action('wp_ajax_get_post', 'example_ajax_get_post');

function example_init() {

    wp_register_style('colorbox-style', plugins_url() . '/example/css/colorbox.css');

    wp_enqueue_script( 'ajax', plugin_dir_url( __FILE__ ) . 'js/ajax.js', array( 'jquery' ) );
    wp_localize_script( 'ajax', 'ajaxurl', admin_url( 'admin-ajax.php' )  );


    wp_register_script('colorbox-script', plugins_url() . '/example/js/jquery.colorbox-min.js');

}// end function

add_action('init', 'example_init');

function example_footer() {
(function($) {

    $(document).ready(function() {

        $('.post .entry-title a').click(function(e) {


            var href = $(this).attr('href');

            var data = {
                action : 'get_post',
                url : href

            $.post(ajaxurl, data, function(data) {
                $.colorbox({ html : data });



}// end function

add_action('wp_footer', 'example_footer');

/* EOF */

First, I suggest reading AJAX in Plugins in Codex, including the examples linked at the bottom. The way you are doing your AJAX request has a couple of potential points of failure depending on how WordPress is configured.

Second, pass the requested post ID with your AJAX request, then query for that post in your AJAX function via WP_Query. You can then run the loop to output post content, and the issue of functions not working outside the loop becomes a non-issue.

Also see this answer I gave to an AJAX-related question that shows a full working example of loading posts via AJAX.

  • Thanks a lot. I will definitely learn more about AJAX now :). For now, I'l let the colorbox handle the request since it has a lot of featues i would like to use. And by adding THE LOOP to the file solved all my problems. Everything works fine. lesson learned :) – Anshul Nov 17 '12 at 8:19
  • you can still use colorbox to render the post, just pass the data returned from the ajax request to colorbox as html content type: $.colorbox({html:$data_from_ajax}); – Milo Nov 17 '12 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.