1

I'm trying to load a php file into a div#main when a link is clicked. The html content loads but the actual php code included in the file does not. Here's what I've got:

Link to trigger everything:

<a id="nav" href="http://domain.com/inc/file.php">

jQuery:

jQuery(document).ready(function($) {

    $("#nav").on('click', function (e) {
        e.preventDefault();
        $("#main").empty();

        $.ajax({
            url: $("#nav").attr('href')
        }).done(function(data) { 
            $('#main').html(data); // display data
        });
    }); 

});

Code from file.php (mostly WordPress functions):

<div id="hostels" class="section cf">

    <?php get_a_post(31); ?>

    <article id="swiss" <?php post_class('swiss bg col span-1-3'); ?> role="article">
        <h3>
            <a href="<?php the_permalink(); ?>">
                <span class="logo-pl-small"><?php include(TEMPLATEPATH . '/library/svg/logo.svg'); ?></span>
                <span class="">Swiss Cottage &ndash; London UK</span>
            </a>
        </h3>
        <?php if ( has_post_thumbnail()) { the_post_thumbnail(); } ?>
        <div class="entry-content">
            <?php the_excerpt(); ?>
            <?php edit_post_link( __( 'Edit', 'bonestheme' ) ); ?>
            <?php if ( get_post_meta($post->ID, 'vidlink', true) ) { 
                echo apply_filters('the_content', get_post_meta($post->ID, 'vidlink', true)); 
            } ?>
         </div>
    </article>

</div>

OUTPUT:

<div id="main">
    <div id="hostels" class="section cf">
    </div>
</div>

The file begins to load but as soon as <?php get_a_post(31); ?> is reached, the file does not continue to load.

I could be wrong, but I thought that by using $.ajax the php code would execute on the server and code it outputs would load into my #main div.

7
  • Check for script error. I don't know wordpress but maybe you cannot call this method like this Commented Jan 7, 2014 at 14:37
  • Try to add <?php ini_set('display_errors', 1); ?> in the beginning of file.php Commented Jan 7, 2014 at 14:39
  • 1
    The code works fine when replicated. So the error is in your PHP. Run http://domain.com/inc/file.php in your browser and see what happends / the output is. Commented Jan 7, 2014 at 14:42
  • @Zub Thanks for the tip. I'm getting Fatal error: Call to undefined function get_a_post(). I'm going to try some generic php to see if that works, then may need to use a different method. Commented Jan 7, 2014 at 14:49
  • seems you need to activate / install / withdraw a plugin -> wordpress.org/support/topic/…, you propably not have all included in your (guessing) selfmade file.php? Commented Jan 7, 2014 at 15:02

2 Answers 2

3

So I've got this somewhat working. I found this tutorial for Ajax Powered Loops with jQuery and WordPress from which I discovered that (as I had guessed) the files I created need to understand WordPress functions. For this I need to include:

require_once('../../../../wp-load.php');

In my case that's the relevant path from file.php.

There's still some issues such as shortcodes generated from plugins not working fully, but that's beyond the scope of my original question.

There's a lot of useful info relating to ajax and Wordpress in the tutorial above though, if anyone is having similar issues I'd recommend having a read.

Sign up to request clarification or add additional context in comments.

2 Comments

+1, you could accept your own answer, maybe helping other people in the future. Regarding ../../../../wp-load.php, there must be an easy-accessible path-variable in wordpress. But cannot say/remember, gave it up years ago, dont think WP expose good programming practise or a transparent model, imho.
@davidkonrad You might be able to use home_url( 'wp-load.php' );. Can't accept my own answer for a couple of days. Will come back to do so.
1

Instead of

$.ajax({
  url: $("#nav").attr('href')
}).done(function(data) { 
  $('#main').html(data); // display data
});

why not the more simple

$("#main").load('http://domain.com/inc/file.php');

??

2 Comments

doesn't explain why OP code doesn't work but ya load() would be more accurate here
In this case, .load() wasn't loading the content into #main but linking to the full page.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.