Hi, EM user here too.
The problem isn’t multi site. I’ve been using the global variable for over five years on multiple multi sites.
You are probably calling it too soon, when you’re not yet in the object. Hard to say without a link or source code.
I’m calling it from within the WP Loop, how much later does it need to be called? And if it was being called too early, why would it correctly work on non-multisites?
The code in single-event.php
is basically:
if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/content', 'event' );
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif;
And then in the Template Part at the very top:
<?php
global $EM_Event;
?>
<div class="event-wrapper">
etc...
</div>
If I add var_dump( $EM_Event )
right after the global call, it returns NULL
.
-
This reply was modified 5 years, 3 months ago by Head Goldfish. Reason: To clarify that PHP is correct
Just to make sure… 😉
You are calling the template from a blog with that theme and em activated and registered events, right?
Or are you trying to call the global from another subsite?
It’s a subsite, but yes, EM is active on that subsite and I’ve put in a few test events. I also have the events widget in the sidebar and it’s displaying a list of events correctly, even on the single page.
In fact the multisite is a staging site and I’m building the theme on my local server which is not a multisite. The theme, uploads, and active plugins have been synced so it’s literally the exact same code, yet it works on my local server and not on the multisite. I copied the template from another site on the same staging server, which is not a multisite, but the code is working there just fine. It’s working on all the single sites, including ones on the same server as the multisite, but not on the multisite.
Aha… EM uses different tables for multisite and single installations. Also, it depends if you have set “Global Table Mode” on or off. That will make the data save in different tables and different references.
For example:
Event Categories are always stored globally. Additional meta to retrieve those is saved only on MultiSite if is not main blog and global tables is yet -> wp_em_meta. Took me days to figure that out 😉 So I ended up with a lot of (if (is_multisite && !is_mainblog() || EM_MS_GLOBAL) statements.
Copying from single install to multisite (and vice versa) will almost always cause wrong references as meta data is not automatically updated.
But try this dirty trick instead of global $EM_Event.
global $post;
$event_id = $post->event_id;
$EM_Event = new EM_Event( $event_id );
echo '<pre>'; print_r($EM_Event); '</pre>';
That will manually call the EM_Event Object if not available.
To explain further:
On Multisite, blog 2:
Events meta can be saved in wp_2_em_events or globally in wp_em_events, etc. 😉
Mmkay, so I’ve managed to fix it:
global $EM_Event;
$event = em_get_event( get_the_id() );
and then:
$event->output( '#_EVENTGCALURL' )
, etc.
I still have no idea why just calling the global wasn’t working, especially why it does work on the single installation version and doesn’t work on the multisite/subsite version of the exact same site, but for whatever reason it wasn’t, and even in order for the function above to work I had to use get_the_id()
, so it’s like for some reason it doesn’t know that it’s inside a WP loop…
In any case that’s how I got it working, if anyone has the same trouble.
The even weirder part was that var_dump( $EM_Event )
was returning NULL
, yet lower down the page $EM_Event->output( '#_EVENTGCALURL' )
was working. What a bizarre issue.
Ha, sorry we must have been writing at the same time. Sounds like either solution works for the same reason, but good to know that there is, in fact, a reason!
Thanks for your help, Patrick.
HAHA, I guess we were.
the em_get_event() always works anywhere as does not require to be in the correct place in the loop.