Merge branch 'permalink'
# Conflicts: # tonys-sportspress-enhancements.php
This commit is contained in:
76
includes/sp-event-permalink.php
Normal file
76
includes/sp-event-permalink.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
/*
|
||||
Plugin Name: Custom Event Permalinks
|
||||
Description: Adds a custom permalink structure for the sp_event post type.
|
||||
Version: 1.0
|
||||
Author: Your Name
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
// Register custom rewrite rules
|
||||
function custom_event_rewrite_rules() {
|
||||
add_rewrite_rule(
|
||||
'(?:event|game)/.*?[/]?([0-9]+)[/]?$',
|
||||
'index.php?post_type=sp_event&p=$matches[1]',
|
||||
'top'
|
||||
);
|
||||
}
|
||||
add_action('init', 'custom_event_rewrite_rules');
|
||||
|
||||
// Customize the permalink structure
|
||||
function custom_event_permalink($permalink, $post) {
|
||||
if ($post->post_type !== 'sp_event') {
|
||||
return $permalink;
|
||||
}
|
||||
|
||||
$event = new SP_Event($post->ID);
|
||||
$teams = get_post_meta($post->ID,'sp_team', false);
|
||||
$format = get_post_meta($post->ID,'sp_format', true);
|
||||
sort($teams);
|
||||
$seasons = get_the_terms($post->ID, 'sp_season', true );
|
||||
if ($seasons) {
|
||||
$seasons_slug = implode(
|
||||
"-",
|
||||
array_map(function($season){return $season->slug;},$seasons),
|
||||
);
|
||||
} else {
|
||||
$seasons_slug = "no-season";
|
||||
};
|
||||
|
||||
// Get the teams associated with the event
|
||||
$team_1 = get_post($teams[0]);
|
||||
$team_2 = get_post($teams[1]);
|
||||
|
||||
switch ($format) {
|
||||
case 'league':
|
||||
$format_string = 'game';
|
||||
break;
|
||||
case 'tournament':
|
||||
$format_string = 'game';
|
||||
break;
|
||||
case 'friendly':
|
||||
$format_string = 'event';
|
||||
break;
|
||||
default:
|
||||
$format_string = 'event';
|
||||
break;
|
||||
}
|
||||
|
||||
if ($team_1 && $team_2) {
|
||||
$permalink = home_url($format_string ."/". $seasons_slug . '/' . $team_1->post_name . '-' . $team_2->post_name . '/' . $post->ID);
|
||||
}
|
||||
|
||||
return $permalink;
|
||||
}
|
||||
add_filter('post_type_link', 'custom_event_permalink', 10, 2);
|
||||
|
||||
// Flush rewrite rules on activation and deactivation
|
||||
function custom_event_rewrite_flush() {
|
||||
custom_event_rewrite_rules();
|
||||
flush_rewrite_rules();
|
||||
}
|
||||
register_activation_hook(__FILE__, 'custom_event_rewrite_flush');
|
||||
register_deactivation_hook(__FILE__, 'flush_rewrite_rules');
|
||||
@@ -16,3 +16,4 @@
|
||||
// Include other files here
|
||||
require_once plugin_dir_path(__FILE__) . 'includes/open-graph-tags.php';
|
||||
require_once plugin_dir_path(__FILE__) . 'includes/featured-image-generator.php';
|
||||
require_once plugin_dir_path(__FILE__) . 'includes/sp-event-permalink.php';
|
||||
Reference in New Issue
Block a user