I created a competition listing plugin for WordPress to allow our club to list the upcoming competitions and if the event was sponsored link it to the predefined sponsor.
To use this plugin you will need to add the Sponsor Plugin

The plugin will only list competitions in the future and if you have a sponsor you can link it to the sponsor as shown below

To add competitions and link a sponsor – goto your WordPress admin page. Select Competitions and populate the data. The Sponsor requires the sponsors to be already added to the site.

Php Plugin Code – Competition Listing Plugin
<?php
/*
Plugin Name: Sponsor Directory
Description: Manage and display sponsors dynamically in WordPress posts using shortcodes.
Version: 1.98
Author: Anything Access
Author URI: https://anythingaccess.com
License: GPL2
*/
// Register Custom Post Type for Sponsors
function create_sponsor_post_type() {
register_post_type('sponsors', array(
'labels' => array(
'name' => __('Sponsors'),
'singular_name' => __('Sponsor')
),
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-awards',
'supports' => array('title', 'editor', 'thumbnail'),
'show_in_rest' => true,
));
}
add_action('init', 'create_sponsor_post_type');
// Add Custom Meta Fields
function add_sponsor_meta_boxes() {
add_meta_box('sponsor_details', 'Sponsor Details', 'sponsor_meta_callback', 'sponsors', 'normal', 'high');
}
add_action('add_meta_boxes', 'add_sponsor_meta_boxes');
function sponsor_meta_callback($post) {
// Retrieve existing meta values
$website = get_post_meta($post->ID, 'sponsor_website', true);
$phone = get_post_meta($post->ID, 'sponsor_phone', true);
$facebook = get_post_meta($post->ID, 'sponsor_facebook', true);
$twitter = get_post_meta($post->ID, 'sponsor_twitter', true);
$instagram = get_post_meta($post->ID, 'sponsor_instagram', true);
$linkedin = get_post_meta($post->ID, 'sponsor_linkedin', true);
$last_event_date = get_post_meta($post->ID, 'sponsor_last_event_date', true); // New field
$businessname = get_post_meta($post->ID, 'sponsor_business_name', true);
$tiktok = get_post_meta($post->ID, 'sponsor_tiktok', true);
$email = get_post_meta($post->ID, 'sponsor_email', true);
echo '<label>Website:</label><br/>
<input type="text" name="sponsor_website" value="' . esc_attr($website) . '" size="50" /><br/>
<label>Phone:</label><br/>
<input type="text" name="sponsor_phone" value="' . esc_attr($phone) . '" size="50" /><br/>
<label>Facebook:</label><br/>
<input type="text" name="sponsor_facebook" value="' . esc_attr($facebook) . '" size="50" /><br/>
<label>Twitter:</label><br/>
<input type="text" name="sponsor_twitter" value="' . esc_attr($twitter) . '" size="50" /><br/>
<label>Instagram:</label><br/>
<input type="text" name="sponsor_instagram" value="' . esc_attr($instagram) . '" size="50" /><br/>
<label>Tiktok:</label><br/>
<input type="text" name="sponsor_tiktok" value="' . esc_attr($tiktok) . '" size="50" /><br/>
<label>Business Name:</label><br/>
<input type="text" name="sponsor_business_name" value="' . esc_attr($businessname) . '" size="50" /><br/>
<label>LinkedIn:</label><br/>
<input type="text" name="sponsor_linkedin" value="' . esc_attr($linkedin) . '" size="50" /><br/>
<label>Email:</label><br/>
<input type="text" name="sponsor_email" value="' . esc_attr($email) . '" size="50" /><br/>
<label><strong>Last Sponsored Event Date:</strong></label><br/>
<input type="date" name="sponsor_last_event_date" value="' . esc_attr($last_event_date) . '" /><br/>';
}
// Save Meta Data
function save_sponsor_meta($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (isset($_POST['sponsor_website'])) update_post_meta($post_id, 'sponsor_website', sanitize_text_field($_POST['sponsor_website']));
if (isset($_POST['sponsor_phone'])) update_post_meta($post_id, 'sponsor_phone', sanitize_text_field($_POST['sponsor_phone']));
if (isset($_POST['sponsor_facebook'])) update_post_meta($post_id, 'sponsor_facebook', sanitize_text_field($_POST['sponsor_facebook']));
if (isset($_POST['sponsor_twitter'])) update_post_meta($post_id, 'sponsor_twitter', sanitize_text_field($_POST['sponsor_twitter']));
if (isset($_POST['sponsor_instagram'])) update_post_meta($post_id, 'sponsor_instagram', sanitize_text_field($_POST['sponsor_instagram']));
if (isset($_POST['sponsor_linkedin'])) update_post_meta($post_id, 'sponsor_linkedin', sanitize_text_field($_POST['sponsor_linkedin']));
if (isset($_POST['sponsor_business_name'])) update_post_meta($post_id, 'sponsor_business_name', sanitize_text_field($_POST['sponsor_business_name']));
if (isset($_POST['sponsor_last_event_date'])) update_post_meta($post_id, 'sponsor_last_event_date', sanitize_text_field($_POST['sponsor_last_event_date']));
if (isset($_POST['sponsor_tiktok'])) update_post_meta($post_id, 'sponsor_tiktok', sanitize_text_field($_POST['sponsor_tiktok']));
if (isset($_POST['sponsor_email'])) update_post_meta($post_id, 'sponsor_email', sanitize_text_field($_POST['sponsor_email']));
}
add_action('save_post', 'save_sponsor_meta');
// Main Function to Fetch and Display Sponsor Info
function display_sponsor_info($atts) {
$atts = shortcode_atts(array('name' => ''), $atts);
$sponsor_name = sanitize_text_field($atts['name']);
$args = array(
'post_type' => 'sponsors',
'posts_per_page' => 1,
'title' => $sponsor_name
);
$query = new WP_Query($args);
if ($query->have_posts()) {
ob_start();
while ($query->have_posts()) {
$query->the_post();
$last_event_date = get_post_meta(get_the_ID(), 'sponsor_last_event_date', true);
$one_year_ago = date('Y-m-d', strtotime('-1 year'));
$businessname = get_post_meta(get_the_ID(), 'sponsor_business_name', true);
// If last sponsored event is over a year old, return only "Sponsored by: Sponsor Name"
if ($last_event_date && $last_event_date < $one_year_ago) {
echo "<p>Sponsored by: " . $businessname . "</p>";
} else {
// Otherwise, return the full description
$desc = apply_filters('the_content', get_the_content());
if (!empty($desc)) {
echo "<p>" . $desc . "</p>";
}
}
}
wp_reset_postdata();
return ob_get_clean();
}
return "<p>Sponsor not found.</p>";
}
// Shortcode Buffer Fix - Ensures Content Doesn't Flash
function sponsor_shortcode_buffer($atts) {
return display_sponsor_info($atts); // Show sponsor directly in the content
}
// Register the shortcode
add_shortcode('sponsor', 'sponsor_shortcode_buffer');
// count sponsors function
function count_posts_with_sponsor($sponsor_name) {
global $wpdb;
// Search for sponsor name in post content
$query = $wpdb->prepare("
SELECT COUNT(ID) FROM $wpdb->posts
WHERE post_status = 'publish'
AND post_type = 'post'
AND post_content LIKE %s",
'%' . $wpdb->esc_like($sponsor_name) . '%'
);
return $wpdb->get_var($query);
}
//Export the sponsors
function export_sponsors_csv() {
if (!current_user_can('manage_options')) {
wp_die(__('You do not have permission to access this page.'));
}
// Set headers to force CSV download
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="sponsors.csv"');
header('Pragma: no-cache');
header('Expires: 0');
// Open output stream
$output = fopen('php://output', 'w');
// Add column headers
fputcsv($output, ['Business Name','Shortcode','Last Sponsor Date','No of Ads','Website', 'Phone', 'Facebook', 'Twitter', 'Instagram', 'Last Update']);
// Query sponsors
$args = [
'post_type' => 'sponsors',
'posts_per_page' => -1
];
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$sponsor_id = get_the_ID();
$sponsor_name = get_the_title($sponsor_id); // Use sponsor name
// Use your existing function to count post mentions
$usage_count = count_posts_with_sponsor($sponsor_name);
// Get last updated date
$last_updated = get_the_modified_date('Y-m-d H:i:s', $sponsor_id);
fputcsv($output, [
get_post_meta($sponsor_id, 'sponsor_business_name', true),
$sponsor_name,
get_post_meta($sponsor_id, 'sponsor_last_event_date', true),
$usage_count,
get_post_meta($sponsor_id, 'sponsor_website', true),
get_post_meta($sponsor_id, 'sponsor_phone', true),
get_post_meta($sponsor_id, 'sponsor_facebook', true),
get_post_meta($sponsor_id, 'sponsor_twitter', true),
get_post_meta($sponsor_id, 'sponsor_instagram', true),
$last_updated
]);
}
}
fclose($output);
exit;
}
add_action('admin_post_export_sponsors_csv', 'export_sponsors_csv');
// Make sure this is registered properly
add_action('admin_post_export_sponsors_csv', 'export_sponsors_csv');
//add sponsor export option to menu
function add_sponsor_post_count_column($columns) {
$columns['sponsor_post_count'] = 'Posts Mentioning';
return $columns;
}
add_filter('manage_sponsors_posts_columns', 'add_sponsor_post_count_column');
function show_sponsor_post_count_column($column, $post_id) {
if ($column == 'sponsor_post_count') {
$sponsor_name = get_the_title($post_id);
echo count_posts_with_sponsor($sponsor_name);
}
}
add_action('manage_sponsors_posts_custom_column', 'show_sponsor_post_count_column', 10, 2);
function export_sponsors_page() {
?>
<div class="wrap">
<h1>Export Sponsors</h1>
<p>Click the button below to download a CSV of all sponsors.</p>
<a href="<?php echo esc_url(admin_url('admin-post.php?action=export_sponsors_csv')); ?>" class="button button-primary">
Download Sponsors CSV
</a>
</div>
<?php
}
function add_sponsors_export_menu() {
add_submenu_page(
'edit.php?post_type=sponsors', // Adds under "Sponsors" menu
'Export Sponsors',
'Export Sponsors',
'manage_options',
'export_sponsors',
'export_sponsors_page'
);
}
add_action('admin_menu', 'add_sponsors_export_menu');
//1.95
function list_recent_sponsors() {
$one_year_ago = date('Y-m-d', strtotime('-1 year'));
$args = [
'post_type' => 'sponsors',
'posts_per_page' => -1,
'meta_query' => [
[
'key' => 'sponsor_last_event_date',
'value' => $one_year_ago,
'compare' => '>=',
'type' => 'DATE'
]
],
'orderby' => 'title', // Sort by sponsor name (post title)
'order' => 'ASC' // Ascending order (A-Z)
];
$query = new WP_Query($args);
if ($query->have_posts()) {
$output = '<ul class="recent-sponsors">';
while ($query->have_posts()) {
$query->the_post();
$sponsor_name = get_the_title();
$sponsor_website = get_post_meta(get_the_ID(), 'sponsor_website', true);
$last_event_date = get_post_meta(get_the_ID(), 'sponsor_last_event_date', true);
$business_name = get_post_meta(get_the_ID(), 'sponsor_business_name', true);
$sponsor_tiktok = get_post_meta(get_the_ID(), 'sponsor_tiktok', true);
$sponsor_instagram = get_post_meta(get_the_ID(), 'sponsor_instagram', true);
$sponsor_facebook = get_post_meta(get_the_ID(), 'sponsor_facebook', true);
$sponsor_email = get_post_meta(get_the_ID(), 'sponsor_email', true);
$sponsor_phone = get_post_meta(get_the_ID(), 'sponsor_phone', true);
$sponsor_linkedin = get_post_meta(get_the_ID(), 'sponsor_linkedin', true);
$sponsor_twitter = get_post_meta(get_the_ID(), 'sponsor_twitter', true);
$output .= '<li>';
$output .= '<a href="' . esc_url(home_url('/sponsor/' . sanitize_title($sponsor_name))) . '" target="_blank"><strong>' . esc_html($business_name) . '</strong></a>';
// Adding an SVG for a web
if ($sponsor_website) {
ob_start(); // Start output buffering
?>
<a href="<?php echo esc_url($sponsor_website); ?>" target="_blank" style="display:inline-block;">
<div class="icon-bg custom-social-icon" style="display:inline-block; vertical-align:middle;">
<svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false">
<path d="M12.02,10.18v3.72v0.01h5.51c-0.26,1.57-1.67,4.22-5.5,4.22c-3.31,0-6.01-2.75-6.01-6.12s2.7-6.12,6.01-6.12 c1.87,0,3.13,0.8,3.85,1.48l2.84-2.76C16.99,2.99,14.73,2,12.03,2c-5.52,0-10,4.48-10,10s4.48,10,10,10c5.77,0,9.6-4.06,9.6-9.77 c0-0.83-0.11-1.42-0.25-2.05H12.02z"></path>
</svg>
</div>
</a>
<?php
$output .= ob_get_clean(); // Capture the output and add it to $output
}
//tiktok
if ($sponsor_tiktok) {
ob_start(); // Start output buffering
?>
<a href="<?php echo esc_url($sponsor_tiktok); ?>" target="_blank" style="display:inline-block;">
<div class="icon-bg custom-social-icon" style="display:inline-block; vertical-align:middle;">
<svg width="24" height="24" viewBox="0 0 24 24" fill="black" xmlns="http://www.w3.org/2000/svg">
<path d="M12 2a10 10 0 100 20 10 10 0 000-20zm3.75 5.125c.513 1.393 1.69 2.417 3.125 2.75v2.375a5.995 5.995 0 01-3.125-.875v4.625c0 2.205-1.79 4-4 4s-4-1.795-4-4 1.79-4 4-4c.183 0 .363.015.539.038v2.416a2.01 2.01 0 00-.539-.064 2.001 2.001 0 00-2 2c0 1.104.896 2 2 2s2-.896 2-2V7.75h2.5z"/>
</svg>
</div>
</a>
<?php
$output .= ob_get_clean(); // Capture the output and add it to $output
}
//instagram
if ($sponsor_instagram) {
ob_start(); // Start output buffering
?>
<a href="<?php echo esc_url($sponsor_instagram); ?>" target="_blank" style="display:inline-block;">
<div class="icon-bg custom-social-icon" style="display:inline-block; vertical-align:middle;">
<svg width="24" height="24" viewBox="0 0 24 24" fill="black" xmlns="http://www.w3.org/2000/svg">
<path d="M12 2.163c3.204 0 3.584.012 4.85.07 3.252.148 4.771 1.691 4.92 4.92.058 1.267.07 1.647.07 4.85s-.012 3.584-.07 4.85c-.149 3.229-1.668 4.771-4.92 4.92-1.266.058-1.646.07-4.85.07s-3.584-.012-4.85-.07c-3.229-.149-4.771-1.691-4.92-4.92C2.175 15.583 2.163 15.203 2.163 12s.012-3.584.07-4.85c.149-3.229 1.691-4.771 4.92-4.92 1.267-.058 1.647-.07 4.85-.07M12 0C8.741 0 8.332.012 7.053.07 2.94.25.25 2.94.07 7.053.012 8.332 0 8.741 0 12s.012 3.668.07 4.947c.18 4.113 2.87 6.803 6.983 6.983 1.279.058 1.688.07 4.947.07s3.668-.012 4.947-.07c4.113-.18 6.803-2.87 6.983-6.983.058-1.279.07-1.688.07-4.947s-.012-3.668-.07-4.947C23.75 2.94 21.06.25 16.947.07 15.668.012 15.259 0 12 0zm0 5.838a6.162 6.162 0 100 12.324 6.162 6.162 0 000-12.324zm0 10.162a3.999 3.999 0 110-7.998 3.999 3.999 0 010 7.998zM18.406 4.594a1.44 1.44 0 11-2.88 0 1.44 1.44 0 012.88 0z"/>
</svg>
</div>
</a>
<?php
$output .= ob_get_clean(); // Capture the output and add it to $output
}
//facebook
if ($sponsor_facebook) {
ob_start(); // Start output buffering
?>
<a href="<?php echo esc_url($sponsor_facebook); ?>" target="_blank" style="display:inline-block;">
<div class="icon-bg custom-social-icon" style="display:inline-block; vertical-align:middle;">
<svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path d="M22.675 0h-21.35C.595 0 0 .595 0 1.325v21.351C0 23.405.595 24 1.325 24H12.82v-9.294H9.692V11.08h3.128V8.412c0-3.1 1.892-4.786 4.659-4.786 1.325 0 2.463.099 2.795.143v3.24h-1.919c-1.505 0-1.796.716-1.796 1.764v2.307h3.589l-.467 3.625h-3.122V24h6.127c.73 0 1.325-.595 1.325-1.324V1.325C24 .595 23.405 0 22.675 0z"/>
</svg>
</div>
</a>
<?php
$output .= ob_get_clean(); // Capture the output and add it to $output
}
//email
if ($sponsor_email) {
ob_start(); // Start output buffering
?>
<a href="<?php echo esc_url($sponsor_email); ?>" target="_blank" style="display:inline-block;">
<div class="icon-bg custom-social-icon" style="display:inline-block; vertical-align:middle;">
<svg width="800px" height="800px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M4 9.00005L10.2 13.65C11.2667 14.45 12.7333 14.45 13.8 13.65L20 9" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M3 9.17681C3 8.45047 3.39378 7.78123 4.02871 7.42849L11.0287 3.5396C11.6328 3.20402 12.3672 3.20402 12.9713 3.5396L19.9713 7.42849C20.6062 7.78123 21 8.45047 21 9.17681V17C21 18.1046 20.1046 19 19 19H5C3.89543 19 3 18.1046 3 17V9.17681Z" stroke="#000000" stroke-width="2" stroke-linecap="round"/>
</svg>
</div>
</a>
<?php
$output .= ob_get_clean(); // Capture the output and add it to $output
}
//LinkedIn
if ($sponsor_linkedin) {
ob_start(); // Start output buffering
?>
<a href="<?php echo esc_url($sponsor_linkedin); ?>" target="_blank" style="display:inline-block;">
<div class="icon-bg custom-social-icon" style="display:inline-block; vertical-align:middle;">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M22.23 0H1.77C.79 0 0 .79 0 1.77v20.46c0 .98.79 1.77 1.77 1.77h20.46c.98 0 1.77-.79 1.77-1.77V1.77C24 .79 23.21 0 22.23 0zM7.14 20.45H3.56V9h3.58v11.45zm-1.79-12.98c-1.13 0-1.91-.77-1.91-1.73 0-.98.78-1.73 1.91-1.73s1.91.75 1.91 1.73c0 .96-.78 1.73-1.91 1.73zm14.89 12.98h-3.58v-5.94c0-1.42-.51-2.39-1.79-2.39-.97 0-1.55.66-1.81 1.3-.09.22-.12.53-.12.83v6.2h-3.58V9h3.58v1.56c.47-.71 1.28-1.76 3.11-1.76 2.28 0 4.11 1.48 4.11 4.67v6.98z"/>
</svg>
</div>
</a>
<?php
$output .= ob_get_clean(); // Capture the output and add it to $output
}
//twitter
if ($sponsor_twitter) {
ob_start(); // Start output buffering
?>
<a href="<?php echo esc_url($sponsor_twitter); ?>" target="_blank" style="display:inline-block;">
<div class="icon-bg custom-social-icon" style="display:inline-block; vertical-align:middle;">
<svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M13.982 10.622 20.54 3h-1.554l-5.693 6.618L8.745 3H3.5l6.876 10.007L3.5 21h1.554l6.012-6.989L15.868 21h5.245l-7.131-10.378Zm-2.128 2.474-.697-.997-5.543-7.93H8l4.474 6.4.697.996 5.815 8.318h-2.387l-4.745-6.787Z"></path></svg>
</div>
</a>
<?php
$output .= ob_get_clean(); // Capture the output and add it to $output
}
//phone
if ($sponsor_phone) {
ob_start(); // Start output buffering
?>
<a href="<?php echo esc_url($sponsor_phone); ?>" target="_blank" style="display:inline-block;">
<div class="icon-bg custom-social-icon" style="display:inline-block; vertical-align:middle;">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50" width="50px" height="50px"><path d="M 14 3.9902344 C 8.4886661 3.9902344 4 8.4789008 4 13.990234 L 4 35.990234 C 4 41.501568 8.4886661 45.990234 14 45.990234 L 36 45.990234 C 41.511334 45.990234 46 41.501568 46 35.990234 L 46 13.990234 C 46 8.4789008 41.511334 3.9902344 36 3.9902344 L 14 3.9902344 z M 18.005859 12.033203 C 18.633859 12.060203 19.210594 12.414031 19.558594 12.957031 C 19.954594 13.575031 20.569141 14.534156 21.369141 15.785156 C 22.099141 16.926156 22.150047 18.399844 21.498047 19.589844 L 20.033203 21.673828 C 19.637203 22.237828 19.558219 22.959703 19.824219 23.595703 C 20.238219 24.585703 21.040797 26.107203 22.466797 27.533203 C 23.892797 28.959203 25.414297 29.761781 26.404297 30.175781 C 27.040297 30.441781 27.762172 30.362797 28.326172 29.966797 L 30.410156 28.501953 C 31.600156 27.849953 33.073844 27.901859 34.214844 28.630859 C 35.465844 29.430859 36.424969 30.045406 37.042969 30.441406 C 37.585969 30.789406 37.939797 31.366141 37.966797 31.994141 C 38.120797 35.558141 35.359641 37.001953 34.556641 37.001953 C 34.000641 37.001953 27.316344 37.761656 19.777344 30.222656 C 12.238344 22.683656 12.998047 15.999359 12.998047 15.443359 C 12.998047 14.640359 14.441859 11.879203 18.005859 12.033203 z"/></svg>
</div>
</a>
<?php
$output .= ob_get_clean(); // Capture the output and add it to $output
}
// Add last event date for admins
if (current_user_can('administrator')) {
$output .= ' -- (Last Sponsored: ' . esc_html($last_event_date) . ')';
}
$output .= '</li>';
}
$output .= '</ul>';
} else {
$output = '<p>No recent sponsors found.</p>';
}
wp_reset_postdata();
return $output;
}
add_shortcode('recent_sponsors', 'list_recent_sponsors');
?>
If you are having issues getting this working drop me an email.
Other WordPress Plugin Posts
This has been deployed to this site