17/08/2023

how to replace wordpress event plugins with efficient code based solutions

Ouch :) This article was published 1 year ago - might be out of date, check out stuff or leave comment

I have used wordpress event plugins for years, and if i had to continue using one of them I would recommend the very extensive - and not so expensive- tribe calendar plugin, that has a free version of the wordpress plugin directory. I like it because it is full of nice features, and may be too much. This is why after 20 years of trying to play around with modules with too much code and maintenance cost, I decided to go back to the roots using the common plugins I use for every site I build on wordpress.

If you want to spend some time investigating common wordpress techniques like custom fields, then this article if for you.

we'll be using basic techniques used in wordpress starter themes like bootscore or underscore, combined with custom fields facilitated by a very common plugin , ACF

using ACF, you can easily create custom fields. The most important custom fields for an event based content manager are obviously the start date and end date of the event. You could obviously use the post date wordpress field to specify the start date, I used this technique for a while and combined the agenda with a few query hacks that I summarized in the future post calendar plugin. In this plugin, I decided to use the default wordpress calendar display to accept posts in the future, as opposed to default behaviour which considers future posts as scheduled. That was a nice and simple option, but the ACF technique of adding specific fields for event start and end date is a better solution for many reasons

  • keep the post date for its initial wordpress behaviour which controls the publishing of the content
  • use the custom fields start date and end date to better control the agenda display , including specific custom field (date start) order, as Documented here on ACF website

The whole process involves a few techniques

Create custom fields in ACF admin page

Display custom field value in custom post admin page list


/*
 * Add columns to exhibition post list
 */
function add_acf_columns ( $columns ) {
    return array_merge ( $columns, array (
        'wave_start_date' => __ ( 'Start Date' ),
        'wave_end_date'   => __ ( 'End Date' )
    ) );
}
add_filter ( 'manage_wave_event_posts_columns', 'add_acf_columns' );
/*
* Add columns to exhibition post list
*/
function exhibition_custom_column ( $column, $post_id ) {
    switch ( $column ) {
        case 'wave_start_date':
            echo get_post_meta ( $post_id, 'wave_start_date', true );
            break;
        case 'wave_end_date':
            echo   get_post_meta ( $post_id, 'wave_end_date', true );
            break;
    }
}
add_action ( 'manage_wave_event_posts_custom_column', 'exhibition_custom_column', 10, 2 );

Use Gravity forms extension to create custom post entry from form

first you have to make a form and then map it to your custom post type with the free Gravity Forms + Custom Post Types plugin. Note the plugin page says it hasn't been tested with latest wordpress version, don't worry I have tested it it works perfectly.

3 different technique to associated entry fields with post content

First technique is for the title. The Gravity Forms + Custom Post Types plugin works by adding a new configuration setting in the field setting : Below the « Description » field setting, you will find the « Post Type » setting. I have looked into the plugin code to find out how it works. It uses gform_field_standard_settings and gform_field_advanced_settings actions to add post type setting, or taxonomy or parent post setting. I don't know about the details of how it works though. But it works, I use it for the post title and post taxonomy.

Second technique is for the post content (description). It is documented on gravity form web site and occurs within the gform_after_submission action hook.

Third technique is for custom fields, and it is documented on ACF forums. Like the post content above , this update is performed with the update_field wordpress method for custom fields and also occurs within the gform_after_submission action hook.

Last Step : display events on page template

you'll create a specific custom page template to display events. For that purpose the most important piece of code here is the query : you should query on the wave_event custom post type, and query only events that occur in the future (start date is in the future), or optionally before the end date has occured. This is the following query in wordpress query language :

$wave_query = new WP_Query( array( 'post_type' => 'wave_event',

        'posts_per_page'    => -1,
        'meta_key'          => 'date_de_debut',
        'orderby'           => 'meta_value',
        'order'             => 'asc',
'meta_query'=>
[        'relation' => 'OR',

    array(
    'key' => 'date_de_debut',
    'value' =>  date("Ymd"),
    'compare' => '>='
),     array(
    'key' => 'date_de_fin',
    'value' =>  date("Ymd"),
    'compare' => '>='
)
]
    )
    )  

display the results is now easy (we say a piece of cake). You can optionnally group them by month using php code within the loop :

    $month=date("M Y",strtotime(get_field("date_start"))  ); if ($month!=$oldmonth)
        {
            $oldmonth=$month
?>
            <div class="row m-2 " >
<div class="col-3  wavedatemonth bg-blue text-white   display-6 d-flex  " style="height:100px">
<div class="align-self-center">
<?php echo $month?>
</div></h3></div></div>
<?php }?>
To top