• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

Using jquery load method to call file that includes require('wp-blog-header.php') not working... why?

This is odd and I can't figure out why it won't work.

I've got a file that uses a wordpress function to call up certain information, for this example, I'll just call up the title of my blog.

I'm doing this outside of wordpress, so I start with "require('wp-blog-header.php')" as per the documentation here: https://codex.wordpress.org/Integrating_WordPress_with_Your_Website.

So this is the script (named test.php)

<?php
require('wp-blog-header.php');
echo get_bloginfo();
?>

I can go to test.php in my browser and it shows the name of the blog. All is well.

Now, I want to place the results of this script onto another page using jquery, so I do this on a second page:

<html>
  <head>
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  </head>
 <body>
   <div id="menutest"></div>
     <script>
        $( "#menutest" ).load( "test.php" );
     </script>
 </body>
</html>


I go to the page with that html/jquery and the blog name does not appear.

If I redo the php file and just put:  

<?php
//require('wp-blog-header.php');
//echo get_bloginfo();
echo "test";
?>

I can reload the html page and the word "test" shows. So I know the jquery is working.

For some reason, it seems like the php script, maybe the "require('wp-blog-header.php');" part, isn't working when I access it through the jquery load method.... but that doesn't make sense to me, because it seems like the php file would be parsed the same no matter how it was accessed. No?

Any thoughts on why this isn't working?

 (in real life, the plan is to access a special menu and load it with jquery, this is a simple and repeatable example of the same problem.)

Appreciate any help on this -

Chris

I've attached the two files I am using if you would like to load them on your wp install for testing.
test.php
test.html
0
St_Aug_Beach_Bum
Asked:
St_Aug_Beach_Bum
  • 8
  • 7
  • 2
1 Solution
 
RobOwner (Aidellio)Commented:
place the load command within jquery's initializing function

$(function() {
// load function
$("#menutest").load("test.php");
});
0
 
F PCommented:
Try this and let me know if it works:

<?php

// require isn't a function, and it's always best to be exact
// about the location. it's faster and stop it from recursing
// the entire includes locations until it finds it.
require __DIR__ . '/wp-blog-header.php';

echo json_encode(get_bloginfo());
0
 
RobOwner (Aidellio)Commented:
json_encode will return a json encoded string, however the load function expects an html response so unless I'm missing something I don't think that will work.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
F PCommented:
is there a reason why you just don't make test.php instead of test.html (if you require the .html, just use mod_rewrite) and then make test.php something like this..

<?php

require __DIR__ . '/wp-blog-header.php';

$blog_info = json_encode(get_bloginfo());

echo <<<HTML
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
    <div id="menutest">{$blog_info}</div>
</body>
</html>
HTML;
0
 
RobOwner (Aidellio)Commented:
include/require are special constructs that can be used with parenthesis if you want to capture the result of the include/require so there's nothing wrong with using them
0
 
F PCommented:
If the function is returning HTML it will escape the string being put there, and the unescaped characters could cause problems with the javascript. I guess addslashes might work too, but that doesn't encode for javascript.
0
 
RobOwner (Aidellio)Commented:
Frank: he wants to use ajax and pull the heading in. it's not always practical to create a page each time
0
 
RobOwner (Aidellio)Commented:
the returned html is automatically urldecoded by using the load jquery function so that's not an issue
0
 
F PCommented:
Try including wp-load.php instead of wp-blog-header.php and I think that should fix the issue.
0
 
F PCommented:
Also, if you're only looking to do ajax calls using HTML and want an easy way to do it - I would have a look here:

http://intercoolerjs.org/
0
 
RobOwner (Aidellio)Commented:
Frank: this issue comes back to the fact that jquery is calling a function before it is initialized. plain and simple
0
 
St_Aug_Beach_BumAuthor Commented:
Wow - ok, didn't expect so much so fast  :)  - hang on....  testing...
0
 
RobOwner (Aidellio)Commented:
Frank cool site and definitely worth considering.  does it conflict in any way with the jquery library?
0
 
F PCommented:
The only conflicts I've found using it were when you write your own $.ajax calls and it doubles the listeners for an object, either canceling one out, or causing the page to freeze.
0
 
St_Aug_Beach_BumAuthor Commented:
Thank you all for your points, counterpoints and ideas. I very much appreciate it.

Switching from wp-blog-header.php to wp-load.php did the trick. I tried everything and tried them in difference combinations, but this change is what makes it work. I don't know why, but I'll google this up next :)

Thank you again!

Chris
0
 
RobOwner (Aidellio)Commented:
stand corrected. well done Frank
0
 
F PCommented:
Thanks! Glad it's working.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 8
  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now