0

This is a bit of a follow on from this question/answer: https://stackoverflow.com/a/4152528/348922

I'm simply not sure how to apply this to my situation (if it's at all possible).

I have a container div that when a button is clicked a file is loaded into the div via jquery:

var root = location.protocol + '//' + location.host;

$(".button-book").click(function(e) {
    e.preventDefault();
    $('#container').load(root+'/loaded-file.php');
});

Fine. BUT that file has a number of text strings that I need wrapped in php in order to hook into them for translation purposes (using WPML plugin for Wordpress):

<?php _e('Arrival Date', 'mywptheme'); ?>
<?php _e("Day", 'mywptheme'); ?>
<?php _e("Month", 'mywptheme'); ?>
<?php _e("Year", 'mywptheme'); ?>
// etc...

Obviously this doesn't work when the file is loaded dynamically. Is it at all possible or am I completely wasting my time?

4
  • 3
    Your question does not make sense to me. jQuery requests a PHP file from the server. The server sees that the request is for a PHP file, processes it with PHP, and sends the output. What's the issue...? Commented Mar 26, 2014 at 13:30
  • Its a wordpress issue i believe - see my answer Commented Mar 26, 2014 at 13:33
  • @MrJonnyWood I believe user574632 is correct. For future questions here on SO, please be clear about what "doesn't work" means. Given what looks like the answer, you were most likely seeing a PHP error somewhere--or an empty doc in the response. Giving those details would lead to the answer without as much confusion. Commented Mar 26, 2014 at 13:39
  • @JAAulde sorry for the confusion, see my comment on the answer below Commented Mar 26, 2014 at 13:48

1 Answer 1

1

Your issue is that _e(...) is a wordpress function, so when this file (loaded-file.php) is executed outside of wordress, it does not work. Its not actually anything to do with jquery - if you visit the file directly in your browser it wont work either.

Simply add the following to the top of loaded-file.php:

require($_SERVER['DOCUMENT_ROOT'].'/blog/wp-blog-header.php');

Adjust for your actual wordress location, in the above case wordpress is in domain.com/blog/

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

9 Comments

Ahh, that makes the question much more understandable! Nice job.
Thanks for this - very useful. My question was badly phrased as I didn't realise that when calling a php file with jquery it processes it before sending the output.
@MrJonnyWood No problem - if this fixed your issue, please except the answer. If not please provide more details
@user574632 For some reason, when I add the correct require line I get a 404 error for loaded-file.php yet when I visit the url directly in the browser it all works.
Odd, so how about if you simple put the text: <h1>hello</h1> into loaded-file.php (nothing else at all), does it work - trying to check if its a js or php issue
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.