Create posts on publishing an event to My Calendar

My Calendar, particularly as of the newly released version 2.2.8, is filled with actions and filters to add your own custom actions or alter data. These hooks are one of the things that make WordPress so much fun to program for — a few simple lines of code can make otherwise difficult things vastly easier.

I’ve been asked a few times about adding an option to My Calendar to create a blog post when you publish an event. Well, I haven’t added that as core functionality, because I really feel it’s more of an add-on type of thing.

But, really, it’s pretty darned easy.

So how do you do it?

Step One: The Hook

The relevant hook for this is ‘mc_save_event’. This function runs right after the event is saved in My Calendar, and has three passed arguments: $action, $data, and $event_id.

$action: what My Calendar is doing during this save. Possible values are ‘add’, ‘edit’, or ‘copy’.

$data: the array of event data that was just handled.

$event_id: Obviously, the ID of the event just saved.

Step Two: Create your function

Your theme’s functions.php file is the most common place to add custom functions. So, in your preferred editor, pop open your functions.php file and set up the basic function:


add_action( 'mc_save_event', 'my_custom_function', 10, 3 );
function my_custom_function( $action, $data, $event_id ) {
    // we'll get here shortly...
}

Adding an action is straightforward: this function will run at the time that the action is declared in My Calendar. It’ll run at priority 10, so you could add other functions to run before or after this one by altering that value. It’ll take the three arguments you’re handing it.

Step 3: Make it do what you want.

Inside the function, you’ll create your post. Now, I don’t know what information you might want in that post — so I’ll keep this basic.


function my_custom_function( $action, $data, $event_id ) {
    // only do this when you're adding a new event
    if ( $action == 'add' ) {
        $post = array( 
            'post_author'=>$data['event_author'], 
            'post_content'=>$data['event_description'],
            'post_title'=>$data['event_title'],
            'post_status'=>'draft' );
         $post_id = wp_insert_post( $post );
         wp_set_post_terms( $post_id, array( 'event', 'tag' ), 'post_tag' );
         add_post_meta( $post_id, 'event_date', $data['event_begin'] );
         wp_publish_post( $post_id );
    }
}

So, what does this function do?

  • Defines a post array, assigning the post to the author of the event and creating content. You may not want the post to actually use the same copy as the description assigned to the event, but that’s the easy example.
  • Inserts the post into the database to get an ID. It’s inserted as a draft so that we can add terms and custom meta data before publishing the post — that way, plug-ins like WP to Twitter can take advantage of that data when the post is published.
  • Adds some tags to the post.
  • Sets the event’s date as a custom field.
  • Publishes the post!

And that’s all it takes. Have fun!

Function References:

Have something to contribute?




« Read my Comment Policy

Start the conversation!

Return to Top