WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] PHP & Jquery in WordPress (8 posts)

  1. Jack Reichert
    Member
    Posted 4 years ago #

    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!

  2. s_ha_dum
    Member
    Posted 4 years ago #

    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.

  3. Jack Reichert
    Member
    Posted 4 years ago #

    thank you so much for the help and the explanation!

    I'll let you know if it works.

  4. Jack Reichert
    Member
    Posted 4 years ago #

    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?

  5. s_ha_dum
    Member
    Posted 4 years ago #

    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?

  6. Jack Reichert
    Member
    Posted 4 years ago #

    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!

  7. seborgarsen
    Member
    Posted 4 years ago #

    Can you explain how you made it work?

  8. Jack Reichert
    Member
    Posted 4 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags