Skip to content

Instantly share code, notes, and snippets.

@andrasguseo
Last active December 8, 2023 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrasguseo/91ea4a54a9d838638125627582272198 to your computer and use it in GitHub Desktop.
Save andrasguseo/91ea4a54a9d838638125627582272198 to your computer and use it in GitHub Desktop.
EA > Add tags to imported events - advanced
<?php
/**
* Add tags to imported events.
*
* Plugins required: The Events Calendar
* Author: Andras Guseo
* Created: November 2, 2023
*/
add_action( 'tribe_aggregator_after_insert_post', 'tec_ea_add_tag_to_imported_events', 10, 3 );
/**
* After the post has been inserted, add the defined tags.
*
* @param array $event The imported event data.
*
* @return void
*/
function tec_ea_add_tag_to_imported_events( $event, $item, $data ) {
$current_origin = $data->meta['origin'];
$current_source = $data->meta['source'];
/**
* START: valid origins
* Limit this to the defined origins.
* If the import is from a different source than defined below, then the rest will be skipped.
* Adjust the array as needed, or remove block completely if you want this to run on all types of imports.
* Accepted values:
* - csv
* - eventbrite
* - gcal
* - ical
* - ics
* - url
*/
$valid_origins = [
'ical',
'gcal',
'ics',
];
if ( ! in_array( $current_origin, $valid_origins ) ) {
return;
}
// END: valid origins
/**
* START: valid sources
* Limit to the defined source URLs.
* If the import is from a different URL, then the rest will be skipped.
* Add import source URLs as needed or remove block completely if you want this to run on all import sources.
*/
$valid_sources = [
'https://awesomeevents.com/ical-event-calendar.php',
];
if ( ! in_array( $current_source, $valid_sources ) ) {
return;
}
// END: valid sources
/**
* START: match sources and tags
* This is useful if you are importing events from more sources and you would like to add
* different tags to the events from different sources.
* Define the sources and their tags in the below array.
* - Key: source URL
* - Value: a tag, or an array of tags
*
* Remove this block if you want to use simple tagging only.
*/
$sources_and_tags = [
'https://awesomeevents.com/ical-event-calendar.php' => 'One tag',
'https://domain.com' => [ 'First tag', 'Second tag', ],
];
// Add the tags
if ( array_key_exists( $current_source, $sources_and_tags ) ) {
wp_add_post_tags( $event['ID'], $sources_and_tags[ $current_source ] );
}
// END: match sources and tags
/**
* START: simple tagging
* This is useful when:
* - you are importing from one source only
* - if you want to add the same tags to all imported events, incl. based on the limitations above
*
* Define your tags below. If you want to add more tags, use an array.
*
* Remove this block if you want to use "match sources and tags".
*/
$simple_tag = 'My simple tag';
// OR
$more_tags = [
'First tag',
'Second tag',
];
// Add the tags
wp_add_post_tags( $event['ID'], $simple_tag );
// END: simple tagging
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment