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?

Community
  • 1
  • 1
Mr Jonny Wood
  • 3,806
  • 5
  • 30
  • 42
  • 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...? – JAAulde Mar 26 '14 at 13:30
  • Its a wordpress issue i believe - see my answer – Steve Mar 26 '14 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. – JAAulde Mar 26 '14 at 13:39
  • @JAAulde sorry for the confusion, see my comment on the answer below – Mr Jonny Wood Mar 26 '14 at 13:48

1 Answers1

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/

Steve
  • 20,703
  • 5
  • 41
  • 67
  • Ahh, that makes the question much more understandable! Nice job. – JAAulde Mar 26 '14 at 13:35
  • 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. – Mr Jonny Wood Mar 26 '14 at 13:46
  • @MrJonnyWood No problem - if this fixed your issue, please except the answer. If not please provide more details – Steve Mar 26 '14 at 13:49
  • @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. – Mr Jonny Wood Mar 26 '14 at 13:51
  • Odd, so how about if you simple put the text:

    hello

    into loaded-file.php (nothing else at all), does it work - trying to check if its a js or php issue
    – Steve Mar 26 '14 at 13:53
  • Still not found but working directly. I've treble-checked the path's too. – Mr Jonny Wood Mar 26 '14 at 13:59
  • Seem to have fixed it (but don't know why). I moved `require` from the very top of `loaded-file.php` inside the first line of code (which is `
    `). All works fine.
    – Mr Jonny Wood Mar 26 '14 at 14:05
  • OK, perhaps you forgot an opening – Steve Mar 26 '14 at 14:07
  • It did have the opening php tag. Anyway, thanks for your answer! – Mr Jonny Wood Mar 26 '14 at 14:10