Support » Plugin: Events Manager - Calendar, Bookings, Tickets, and more! » Global $EM_Event null on single-event.php in multisite.

  • Resolved Head Goldfish

    (@shoelaced)


    It sounds like others have had this issue but I haven’t seen an answer. I’m using single-event.php to create a custom template for single events. At the top of the template I call global $EM_Event, but it’s returning NULL. It works fine on all of my sites except one, and the only difference is that it’s a multisite.

    I’m running PHP 7.0, latest WP and EM. Is there any fix for this?

Viewing 11 replies - 1 through 11 (of 11 total)
  • 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.

    Thread Starter Head Goldfish

    (@shoelaced)

    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?

    Thread Starter Head Goldfish

    (@shoelaced)

    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.

    Thread Starter Head Goldfish

    (@shoelaced)

    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. 😉

    Thread Starter Head Goldfish

    (@shoelaced)

    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.

    Thread Starter Head Goldfish

    (@shoelaced)

    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.

    Thread Starter Head Goldfish

    (@shoelaced)

    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.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Global $EM_Event null on single-event.php in multisite.’ is closed to new replies.