Workshop to Event Workflow in WordPress

Will Wonka asking you to tell him again what WordPress can't do.

I have been doing little other than semi-real web development for three weeks across a number of projects. It’s a level of sustained practice/work that I’ve probably never done before. Who knew that kind of focus could actually result in fairly dramatic improvement?

One of the projects is the reconstruction of our ALT Lab department page and I have had the chance to really do some stuff that I’ve been considering for several years. There’s a lot going on in the site but I’ll focus on one little concept/workflow for the moment.

We have two goals in this case.

  1. We want a browsable catalog of workshops we offer that can be shared with people so they can request them.1
  2. We need a calendar of events. Those events will include workshops that are presented at a particular date. We use Events Calendar Pro for this and are very happy with it.

Workshops – The Post Type

Step one was to create a custom post type called ‘workshop’ and then assign some custom fields to it with ACF. That keeps the content fairly consistent in look/feel and gives us enough standardization to do other tricks. We ended up with a custom post interface that looks like the gif2 below. You can see the ability to add learning objectives, examples, audience etc. in addition to the standard post options.

Clicking around on a custom post type called workshop. Demonstrating ACF repeater field additions.

Events

Calendar events are often going to be workshops but might also be other things like speakers or whatever. We’ll maintain the flexibility to add other events but wanted an easy way to get our stock workshop information as specific events (where we could tweak them depending on who is presenting and focusing on any particular audience). The moment a workshop gets a time and location is the moment it graduates to an event and moves to the calendar.3

Now we certainly could cut/paste our workshops into the events post type but the system ought to do that for us. What are computers for if not to do boring, repetitive work?

The other advantage here is that we can keep our workshops up to date with our newest examples while workshop event lives independently so that faculty who really liked that example from their session in 2017 have an event URL where that lives. Progress marches on while history is preserved.

Making it Work

The goal was now to make it all work seamlessly by adding a button to the Workshop editor view that would enable you to push that content into events with, of course, the click of a button.

Adding the Button

This is fairly standard WordPress stuff, but we are changing manage_posts_columns to manage_workshop_posts_columns so that it only applies to the view for the workshop custom post type .

Now we have a button! Exciting but it’s not going to do much until we tie it to a function.

//ADD COLUMN HEADER TO LIST VIEW
function make_event_columns_head($defaults) {
    $defaults['make_event'] = 'Make Event';
    return $defaults;
}
 
// ADD BUTTON TO LIST VIEW
function make_event_columns_content($column_name, $post_ID) {
    if ($column_name == 'make_event') {
       echo '<button class="button-primary workshop-to-event-button" id="workshop-to-event-'.$post_ID.'" name="make_event" data-id="'.$post_ID.'" ><span class="dashicons dashicons-calendar-alt"></span></button>';
    }
}

add_filter('manage_workshop_posts_columns', 'make_event_columns_head');
add_action('manage_workshop_posts_custom_column', 'make_event_columns_content', 10, 2);


Make the Button Do Something

To make buttons run PHP functions we need to tie them to javascript and do that whole AJAX loop. This script first looks for our buttons in the class ‘workshop-to-event-button.’ When a button in that class is clicked the button sends the ID of the post it’s associated with to the PHP function we created called ‘make_workshop_to_event.’ That script gets the content from the ID it was given by the button and creates a new event using that content. The function then returns the URL for that new event to allow us to redirect to the editor view.

Initially, I though I could get away with manually constructing that editor URL and just passing in the ID (wp-admin/post.php?post=246&action=edit). That fails for (retroactively) obvious security reasons. I then spent a fair amount of time thinking I needed to construct a nonce-based URL to make the transition to the editor view of the event but it turns out that using get_edit_post_link was the much easier path to take. I don’t understand the security implications enough to really tell anyone how/why that works.

jQuery( document ).ready(function() {
    jQuery(".workshop-to-event-button").click(function(){  
    console.log('functioning');
    var post_id = jQuery(this).data('id');
    var data = {
      action: 'make_workshop_to_event', //php function being called
      id: post_id,
    };
    jQuery.post(ajaxurl, data, function(response) {
            window.location.replace(response);//go to new URL editor view returned by function 
        })
    });
});    

function make_workshop_to_event_callback(){
  $id = $_POST['id'];//get from JS
  $post = get_post($id);//get the workshop data
  $title = $post->post_title;
  $content = $post->post_content;
  $my_post = array(
      'post_title'    => $title,
      'post_content'  => $content,
      'post_status'   => 'draft',
  );
   
  // Insert the post into the database.
  $new_event = tribe_create_event( $my_post );//make new event
  echo get_edit_post_link($new_event, 'fugazi');//builds the edit direct link redirect piece that's used by js
  exit();
}
add_action('wp_ajax_make_workshop_to_event', 'make_workshop_to_event_callback');

The New Workflow

A button is pushed. The screen changes from the Workshops List view to the editor view of events where the selected workshop content has been duplicated.

Remaining Details

I still have to map over the ACF custom fields but that should be pretty straight forward . . . which is partly why I haven’t done it. I struggle with doing well-understood work when I have more interesting problems to address.

Asides

Please think about stuff like this every time you hear a supposed web developer/ed tech person say “WordPress can’t . . . ” I’m not a skilled web developer and if I can figure out these things many others could do far better. Most supposed limitations are a failure of imagination rather than the technology or the skill.

Also take note of how many times I tried things here. Not every one was a complete failure but there were plenty of those and a number that didn’t generate events at all. This stuff doesn’t necessarily come easy to me. Try not to get discouraged. In the future you can fail at entirely new and more complicated things!
Screenshot showing 74 different creation attempts that were part of figuring out how to make this work.


1 Got four friends interested in this? We’ll run it.

2 You hear my voice in your head saying it with a hard AND a soft G at THE SAME TIME. Then realize no one should care how gif is pronounced or whether your punctuation went to Oxford.

3 It’s a special moment for everyone to see a workshop grow up.

2 thoughts on “Workshop to Event Workflow in WordPress

Comments are closed.