Support

Home Forums Event Espresso Premium Using Event Espresso Functions inside Ajax in WordPress

Using Event Espresso Functions inside Ajax in WordPress

Posted: October 7, 2015 at 10:39 am


Adam Curry

October 7, 2015 at 10:39 am

I’m using Ajax to load events near a person once their location has been determined. This is inside a wordpress framework, using the WordPress Ajax integration.

However, my ajax function in functions.php dies when it tries to call an event espresso function.

Here is the offending line:

<time class="time-hold" datetime="<?php espresso_event_date('j', ' '); ?>"><span><?php espresso_event_date('j', ' '); ?></span><?php espresso_event_date('F', ' '); ?></time>

If I take this line out, the rest of the function works perfectly. The code stops right when it gets to espresso_event_date();

Is there a reason why that function would not be available inside an ajax function?
If so, how can I load the espresso functions inside that function?

Please let me know if you need more info.


Josh

  • Support Staff

October 8, 2015 at 11:24 am

Hi Adam,

One thing you could check is if template_tags.php is loaded up when your AJAX call is made. A quick way to check is just copy the espresso_event_date function, including the function_exists() check, into your functions file. This will not cause a function already declared error, since the original function is wrapped in a function_exists() check.


Adam Curry

October 12, 2015 at 7:35 am

Copying that whole function into functions.php worked perfectly. What does that mean isn’t working correctly, and how can I fix it?


Josh

  • Support Staff

October 12, 2015 at 10:57 am

One possibility is your code is executing before the WordPress init hook. If all you need is the espresso_event_date() tag, then what you’ve already done is sufficient to fix it. If you need more template tags, you can add them to your function. You can also open up Event Espresso core in an IDE and step through the code where Event Espresso loads up each system when needed. What you’ll find is Event Espresso doesn’t load everything all at once, instead, it loads up its systems step by step and when needed.


Adam Curry

October 13, 2015 at 9:12 am

Hmm, ran into a problem — the problem is that that function references other EE functions … which are also not available.

So, I added this line to my functions.php to force it to load:
require_once( EE_PUBLIC . ‘template_tags.php’ );

Is that going to cause problems elsewhere with EE?


Josh

  • Support Staff

October 13, 2015 at 12:12 pm

You can prevent potential problems by checking for any variables, functions or classes defined in the include file and see if it’s already included. For example:

if ( ! function_exists( 'is_espresso_event' ) ) { require_once( EE_PUBLIC . 'template_tags.php' ); }

Then you also test your code to make sure it doesn’t cause problems elsewhere.

The support post ‘Using Event Espresso Functions inside Ajax in WordPress’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso