Add an event programmatically

Hi !

I would like to be able to add an event programmatically in the database.
I've found many custom fields, but i'm unable to add the dates to an event.
My events aren't complicated, they only have a starting date with an hour, and an ending date with an hour too. No recurring event, no multiple dates.
Could you help me please ?

Thanks !

  • KuneStudio
    • Flash Drive

    Yep definitely :slight_smile:

    I'm want to import a full XML of events from my database. It's almost 900 events.
    I use wp all import to do that.
    Basically, this is what i do:
    - Use wp-all-import to import all the events, saving custom fields that can be saved with it too.
    - Use an action hook that convert the custom fields to the good format for event+ .
    The code:

    function custom_save_event_meta($post_id) {
    	global $wpdb;
        	$post = get_post($post_id);
    		// Setting up event status
    		update_post_meta($post_id, 'incsub_event_status', 'open');
    		// Setting up recurring event
    		$debut = get_post_meta($post_id, 'event_date_debut', true);
    		$fin = get_post_meta($post_id, 'event_date_fin', true);
    		$start_time = date("H:i", $debut);
    		$start_date = date("Y-m-d", $debut);
    		$start = strtotime(date("Y-m-d H:i", $debut));
    		$end_time = date("H:i", $fin);
    		$end_date = date("Y-m-d", $fin);
    		$end = strtotime(date("Y-m-d H:i", $fin));
    		$interval = '';
    		$time_parts = array(
    			'month' => '',
    			'day' => '',
    			'weekday' => '',
    			'time' => '',
    			'duration' => @$repeat['duration'],
    		$event = new Eab_EventModel($post);
    		$event->spawn_recurring_instances($start, $end, $interval, $time_parts); //@TODO: Improve
    		if ($end <= $start) {
    			// BAH! Wrong order
    		// Setting up everything else
    		delete_post_meta($post_id, 'incsub_event_start');
    		delete_post_meta($post_id, 'incsub_event_no_start');
    		delete_post_meta($post_id, 'incsub_event_end');
    		delete_post_meta($post_id, 'incsub_event_no_end');
    	    add_post_meta($post_id, 'incsub_event_start', $start_date . " " . $start_time);
    	    add_post_meta($post_id, 'incsub_event_end', $end_date . " " . $end_time);
    add_action('save_post', 'custom_save_event_meta');

    I can't get the dates to be saved... The original format of the date (event_date_debut) looks like this one: 2013-06-01T00:00:00.000+02:00

    Thank you !

  • Vladislav
    • Dead Eye Dev


    From what I understood from the code, you're trying to migrate your events as recurring Events+ events, correct? If so, here's some insight on how recurring events work in Events+, maybe that'll help.

    In Events+, the recurring events have one "hub" event, which is what you edit/create when you create a recurring event in the post editor interface. This is not an actual event - it is a special kind of instance, which is used as a "template" for spawning recurring instances. The "hub" event doesn't have the "incsub_event_start" and "incsub_event_end" meta values, but rather "eab_event_recurrence_starts" and "eab_event_recurrence_ends", which mark a period within which the instances should be spawned.

    The instances are individual event occurrences within the period you specified for the "hub" event. They are also children of the "hub" event, meaning their post_parent == hub event ID. Other then that, they're regular events and they have "incsub_event_start" and "incsub_event_end" meta values. You can use the "spawn_recurring_instances" method of the "Eab_EventModel" object to auto-create the recurring instances.

    However, in order for this to work, the "spawn_recurring_instances" also takes 2 important arguments beside the period start/end timestamp. The interval is one of them, and it should be one of these constants: Eab_EventModel::RECURRANCE_DAILY (for daily events), Eab_EventModel::RECURRANCE_WEEKLY (weekly events), Eab_EventModel::RECURRANCE_MONTLY (monthly events) or Eab_EventModel::RECURRANCE_YEARLY (yearly events). The other argument is time_parts, and it's an associative array whose values depend on the context set by the interval parameter (you may want to check the protected Eab_EventModel::_get_recurring_instances_timestamps(...) method in lib/class_eab_event_model.php to see which keys are inspected per context). Both of these arguments need to be set to proper values, so the recurring instance timestamps can be calculated - these will then be used to actually create each instance, according to the "hub" template.

    I hope this helps a bit, please let me know if you need more details or any other sort of help.

  • KuneStudio
    • Flash Drive

    Hi @Vladislav , thanks for your help.

    It's not what i'm trying to exactly do. It's much simpler in fact.
    I have a formatted XML (not the wordpress one), with simple event. They're not reccuring events :slight_smile:

    What I need i think is just a simple function to save every fields corresponding to the dates in the good format.

    Thanks !

  • Vladislav
    • Dead Eye Dev


    Oh great, this makes our lives a bit easier. The regular (non-recurring) events are much easier to deal with - they're just a regular posts, with just 3 important meta fields: incsub_event_status (should be "open"), incsub_event_start and incsub_event_end (both should be set to a value in the date("Y-m-d H:i:s", $unix_timestamp) format, where $unix_timestamp is your event start/end timestamp). Other then these 3 meta fields, the only other caveat is that you really want to make sure that your post has "post_type" set to "incsub_event" - that should be enough to get the post listed in the admin interface. You want to make sure the "post_status" for the post is set to "publish" too, to get it to appear on the front-end.

  • Vladislav
    • Dead Eye Dev


    The plugin makes a (historic) distinction between multiple start/end dates and recurring events. If it's the multiple start/end dates you're after, the processing should be easy - just make sure the incsub_event_start and incsub_event_end fields are arrays of matching length, with MySQL timestamps (Y-m-d H:i:s format) as their values.

  • aecnu
    • WP Unicorn

    Greetings KuneStudio,

    It appears this particular topic is now resolved/closed, if you need any further assistance please let us know.

    If it wasn't resolved, or you have any more questions related to this thread, please feel free to post them below including any new symptoms or errors and tick the 'Mark as Not Resolved (re-open)' box below the post area (or else we'll miss it!)

    Thank you for being a WPMU Dev Community Member!

    Cheers, Joe

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.