You only need 4 pieces of information to create an Events Manager event in PHP. This is the core of the code I use in a nightly event import script on a client site.
On Cork Entertainment, a client site that lists events in Cork in Ireland, I run a script each night to scrape data from the website of one venue (with the venue ownerโs permission). The client site uses the Events Manager plugin so it needs to create events dynamically.
Events Manager uses the event title and content to create a post of type ‘event.’ It then stores the event details (date, time, location etc) as post meta.
Only 4 items of information to create an event – name, post content, start date and start time.
The date and time formats are simple: yyyy-mm-dd and hh:mm:ss.
The script below, a standalone one, goes a step further – after the event is created it changes the default slug, which is based on the event name, to one based on a concatenation of the event name and date. This was useful on the Cork Entertainment site because bands play more than once and slugs with numerical suffixes e.g. -2 or -3 etc., don’t look good. The code uses ‘em_event_save‘ filter – it changes the slug, author (from the anonymous one that would leave the event status as ‘Pending’) and the status to ‘Publish.’ Removing the filter is not necessary in this script but included for completeness. The same filter is used by the plugin when saving other post types so one needs to be careful when using it.
This code works perfectly as the core code for the nightly script and imports events as necessary.
Update 10 January 2018: Early in December the nightly script stopped adding events. The events appeared in the dashboard but not on the front end. It worked with Events Manager 5.7.3 but not in version 5.8.x. The required row was not being created in the event table.
I reported the issue to the developers and I did a lot of debugging, stepping through the code. Eventually I saw an error message related to the ‘event_rsvp‘ database column – it was not being initialised by Event Manager. My fix was to set it in my code: $EM_Event->event_rsvp = false;
. I posted this info to the support thread.
hi thanks for that!
What I additionally need, is to add a category programmatically but I don’t get it working ๐
And another question, maybe You can help: how can I delete an event programmatically, so that also wp_post and wp_post_meta entries will be removed?
Thanx!
I don’t know how to add event categories.
Can you post your code? (e.g. https://pastebin.com/)
Have you enabled categories? (it is enabled by default)
To delete the event you can use the EM_Event::delete() function. It will call wp_delete_post() which will delete post_meta and it then calls EM_Event::delete_meta() which deletes data from the EM_EVENTS_TABLE.
my problem is that I got it working with categories but since some time it’s not working anymore. You can put a main category in events manager’s preference which will always be added to an event if You didn’t select any category. So, to all my events which I added programmatically, this main category was added. But this isn’t happening anymore although I didn’t change the code. So, I suppose that it’s not working anymore since an update of events manager….
But as I made this code by much trial & error I think I made some mistakes, first without consequences but now with problems…
I used following code, other than yours:
$EM_Event = new EM_Event();
$EM_Event->event_owner = 1;
$EM_Event->location_id = $loc_id;
$EM_Event->event_slug = $name;
$EM_Event->event_name = $name;
$EM_Event->post_content = $description;
$EM_Event->post_excerpt=” ;
$EM_Event->event_start_date = $date_start;
$EM_Event->event_end_date = $date_start;
$EM_Event->event_start_time = $time_start;
$EM_Event->event_end_time = $time_end;
$EM_Event->save();
$EM_Event->save_meta();
thanks
furthermore, I noticed that your code is not working, too. The new event is stored in wp_posts and wp_postmeta but not in em_events. Though it’s visible in the events list in wp backend. If I edit and save this event, then it is also saved in em_events but before this it’s not in this database. I think, maybe, it’s the same with your code and my code, that also was working before. Maybe events manager changed something in the code…
About a month ago I found that my code in my nightly script wasn’t working. Something had changed between Events Manager 5.7.3 and 5.8. I saw the same thing that you saw – wp_posts and wp_postmeta was okay but not em_events.
https://wordpress.org/support/topic/em_event-save-not-working-in-5-8-x-okay-in-5-7-3/
I eventually found that I had to add:
$EM_Event->event_rsvp = false;
Please try this
YES, that’s it! Thanks!
First I inserted the code after $EM_Event->save, that didn’t work, but inserting it before save works.
Your code is different from mine and as I said, I found my code by trial & error and not really understanding what I did ๐
so, your function ce_change_event_slug is for writing the data to _posts and _postmeta? And I us $EM_Event->save_meta() which You did not use. Maybe, save_meta() does the same thing as ?
And maybe You can help me with deleting an event with EM_Event::delete()? I don’t know how to use it in the script. Actually, I catch the post_id from em_events from the event that I want to delete and then do 3 database calls:
“DELETE FROM wp_posts WHERE ID= ‘$post_id'”;
“DELETE FROM wp_postmeta WHERE post_id= ‘$post_id'”;
“DELETE FROM wp_em_events WHERE event_name=’$name'”;
I think this would all be done by EM_Event::delete() but how do I insert the post_id?
I tried
$EM_Event = em_get_event($post_id);
$EM_Event->delete();
but that’s wrong in any way I think….
My ce_change_event_slug() function is only to change the slug of the event.
I updated the post to mention the support thread and the ‘event_rsvp’ fix.
I would expect em_get_event() and delete() to work. I will try it and report back.
Olaf – You need the event ID, not the post ID, to delete an event.
Use the ‘event_id’ from the wp_em_events table when you call ’em_get_event($event_id)’
I have blogged about this.
Thanks for this! Any suggestions on saving a recurring event rather than single? Ideally as a draft without publishing individual instances. I’m guessing that something other than $EM_Event->save() is needed, but I haven’t been able to find documentation so far. I also tried $EM_Event->recur = 1 , but no luck.
Thanks,
David