• Resolved Jack Reichert

    (@jackreichert)


    I’m trying to load some php into a div after the first image of a wordpress post. I thought that with Jquery’s selectors it was natural to use Jquery for the job.

    Initially I tried:

    $j(document).ready(function() {
    $j(‘p:first’).append(‘<?php include(‘new-box.php’); ?>’)
    });

    But that didn’t seem to work.

    Then, I DID successfully add the div using:

    $j(document).ready(function() {
    $j(‘p:first’).append(‘<div id=”new-box”>Ooga Booga
    </div>’)
    });

    But I could not get anything into the div with .load. I first tried:

    $j(document).ready(function() {
    $j(‘p:first’).append(.load(“new-box.php”));
    });

    Then I tried putting in the div first then loading the content like so:

    var $j = jQuery.noConflict();

    $j(document).ready(function() {
    $j(‘p:first’).append(‘<div id=”new-box”>hooga
    </div>’)
    $j(“#new-box”).load(“new-box.php”);
    });

    The file listed is in the theme dir of WP. My thoughts are that WP is making the relative url’s wonky, but I can’t see to get php calls like template_url() working in the .load().

    Any suggestions?

    Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Your first approach isn’t going to work. PHP runs on the server before anything gets sent to the browser. Javascript, and hence jQuery, runs in the browser on the computer of whoever is viewing the site. You won’t be able to run PHP just by having jQuery insert the code. You are already too late by the time the jQuery runs. JQuery can load PHP using its Ajax functions, which is what you are doing with load. Your first attempt at that uses load incorrectly but your second one looks right to me– I say this without testing it so beware. It looks to me like your conclusion that it is the relative URL is correct. Try this:

    $j("#new-box").load("<?php bloginfo('template_url'); ?>/new-box.php");

    Again, no testing but hopefully that will get you started.

    Thread Starter Jack Reichert

    (@jackreichert)

    thank you so much for the help and the explanation!

    I’ll let you know if it works.

    Thread Starter Jack Reichert

    (@jackreichert)

    So the <?php bloginfo(‘template_url’); ?> didn’t work, I guess for the same reason that the append didn’t with but when I used a full url it worked great.

    I’m thinking that with a global variable it could work, still not sure how to mix the php with the javascript. Ideas?

    But by using bloginfo you should be generating an absolute url. That is the reason to use it. Exactly what does your code look like?

    Thread Starter Jack Reichert

    (@jackreichert)

    I found a better solution here.

    http://aext.net/2009/10/wordpress-hack-anything-can-be-added-anywhere-in-the-post-content/

    By adding the content this way I can access plugins and wp hooks which weren’t working with the above solution.

    I really appreciate your help!

    Can you explain how you made it work?

    Thread Starter Jack Reichert

    (@jackreichert)

    Sure,

    I added the php into the theme but wrapped it in a div tag that was set display:none.

    Then I used the javascript to move it to the right place.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘PHP & Jquery in WordPress’ is closed to new replies.