Skip to content

Instantly share code, notes, and snippets.

@kadimi
Last active March 18, 2024 01:58
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 kadimi/639e4d996c4acd41fda4e6a4277d248f to your computer and use it in GitHub Desktop.
Save kadimi/639e4d996c4acd41fda4e6a4277d248f to your computer and use it in GitHub Desktop.
SportsPress: Regenerate Event Titles
<?php
/**
* Add bulk action to re-assign SportsPress events titles.
*/
add_filter('bulk_actions-edit-sp_event', fn ($actions) => $actions + ['sp_regenerate_title' => 'Regenerate Title']);
add_filter('handle_bulk_actions-edit-sp_event', function ($redirect_to, $action, $post_ids) {
if ($action !== 'sp_regenerate_title') {
return $redirect_to;
}
$reverse_teams = get_option('sportspress_event_reverse_teams', 'no') === 'yes';
foreach ($post_ids as $post_id) {
$teams_ids = sp_get_teams($post_id);
if ($reverse_teams) {
$teams_ids = array_reverse($teams_ids);
}
$teams_shortnames = array_map(fn ($team_id) => sp_team_short_name($team_id), $teams_ids);
$title = implode(get_option('sportspress_event_teams_delimiter', 'vs'), $teams_shortnames);
wp_update_post([
'ID' => $post_id,
'post_title' => $title,
]);
}
return add_query_arg('bulk_action_done', count($post_ids), $redirect_to);
}, 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment