Last active
November 27, 2019 17:23
-
-
Save damiencarbery/46e5d5d634f90852196922778a3e5bff to your computer and use it in GitHub Desktop.
Create Events Manager event - https://www.damiencarbery.com/2017/10/create-events-manager-events-programmatically/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en-US"> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>Test Add Event</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1" /> | |
</head> | |
<body> | |
<h1>Test Add Event</h1> | |
<?php | |
// Change the slug to event name and date. | |
function ce_change_event_slug( $result, $event_obj ) { | |
if ( $result ) { | |
if ( $event_obj->post_id ) { | |
$author_id = 1; # ID of non-anonymous user. | |
$event_slug = sanitize_title($event_obj->event_name.' '.$event_obj->event_start_date); | |
$args = array( 'ID' => $event_obj->post_id, | |
'post_name' => $event_slug, | |
'post_author' => $author_id, | |
'post_status' => 'publish' | |
); | |
wp_update_post( $args ); | |
} | |
} | |
return $result; | |
} | |
define('WP_USE_THEMES', false); | |
/** Loads the WordPress Environment and Template */ | |
require( dirname( __FILE__ ) . '/wp-blog-header.php' ); | |
$event_name = 'EM Test'; | |
$event_description = '<p>This is the description of EM Test event.</p>'; | |
$event_start_date = '2017-10-01'; | |
$event_time = '12:13:14'; // 12h 13m 14s | |
$location_id = 1; // This isn't absolutely necessary - an event can be created without a location. | |
// STOP EDITING. | |
$event_end_date = $event_start_date; | |
$event_start_time = $event_time; | |
$event_end_time = $event_start_time; | |
$EM_Event = new EM_Event(); | |
$EM_Event->event_name = $event_name; | |
$EM_Event->post_content = $event_description; | |
$EM_Event->location_id = $location_id; | |
$EM_Event->event_start_date = $event_start_date; | |
$EM_Event->event_end_date = $event_end_date; | |
$EM_Event->event_start_time = $event_start_time; | |
$EM_Event->event_end_time = $event_end_time; | |
// Combine date and time and convert to Unix timestamp to populate datetime fields. | |
$EM_Event->start = strtotime($EM_Event->event_start_date.' '.$EM_Event->event_start_time); | |
$EM_Event->end = strtotime($EM_Event->event_end_date.' '.$EM_Event->event_end_time); | |
$EM_Event->event_rsvp = false; // Set to false to fix bug introduced in Events Manager in 5.8. | |
add_filter( 'em_event_save', 'ce_change_event_slug', 10, 2 ); | |
$EM_Event->save(); | |
remove_filter( 'em_event_save', 'ce_change_event_slug', 10, 2 ); | |
echo '<p>Event post ID: ', $EM_Event->post_id, '</p>'; | |
echo '<p>Event event ID: ', $EM_Event->event_id, '</p>'; | |
global $EM_Notices; | |
echo $EM_Notices::__toString(); | |
?> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment