admin管理员组

文章数量:1192144

I am working on a WordPress site using the SportsPress plugin and WPForms. I have a form created with WPForms that includes checkboxes populated with all existing clubs and a dropdown menu for the number of legs (1-2). When the form is submitted, a round-robin league is created, and matches are generated for the league. Here is the relevant part of my functions.php:


function create_sportspress_club_for_new_user($user_id) {
    // Get the user data
    $user_info = get_userdata($user_id);
    $username = $user_info->user_login;
    $profile_picture = get_user_meta($user_id, 'profile_photo', true);

    // Create a new club in SportsPress
    $club_id = wp_insert_post(array(
        'post_title' => $username,
        'post_type' => 'sp_team',
        'post_status' => 'publish',
    ));

    if (is_wp_error($club_id)) {
        return;
    }

    if ($club_id) {
        // Set the club profile picture
        if ($profile_picture) {
            $attachment_id = media_sideload_image($profile_picture, $club_id, null, 'id');
            if (!is_wp_error($attachment_id)) {
                set_post_thumbnail($club_id, $attachment_id);
            }
        }
        // Assign the club to the user
        update_user_meta($user_id, 'sp_team', $club_id);

        // Assign the user to the club in SportsPress settings
        update_post_meta($club_id, 'sp_team_manager', $user_id);

        // Ensure the user is added to the correct taxonomy terms if needed
        wp_set_object_terms($club_id, $user_id, 'sp_team_manager');

        // Add the new club to the sportspress_league_menu_teams option
        $teams = get_option('sportspress_league_menu_teams', array());
        if (!is_array($teams)) {
            $teams = array();
        }
        $teams[] = $club_id;
        update_option('sportspress_league_menu_teams', $teams);
    }
}

function create_matches_for_schedule($schedule) {
    foreach ($schedule as $match) {
        $home_club_id = intval($match[0]);
        $away_club_id = intval($match[1]);

        // Debugging: Log the IDs being used and their types
        error_log("Home Club ID: $home_club_id (Type: " . gettype($home_club_id) . "), Away Club ID: $away_club_id (Type: " . gettype($away_club_id) . ")");

        // Ensure the IDs are integers
        if (!is_int($home_club_id) || !is_int($away_club_id)) {
            error_log("Error: Club IDs are not integers. Home Club ID: $home_club_id, Away Club ID: $away_club_id");
            continue;
        }

        // Fetch team names using their IDs
        $home_club_name = get_the_title($home_club_id);
        $away_club_name = get_the_title($away_club_id);

        // Debugging: Ensure club names are correct
        error_log("Home Club Name: $home_club_name, Away Club Name: $away_club_name");

        // Check if the club names are valid
        if (empty($home_club_name) || empty($away_club_name)) {
            error_log("Error: Invalid club names. Home Club Name: $home_club_name, Away Club Name: $away_club_name");
            continue;
        }

        // Create a new match in SportsPress
        $match_id = wp_insert_post(array(
            'post_title' => "$home_club_name vs $away_club_name",
            'post_type' => 'sp_event',
            'post_status' => 'publish',
            'meta_input' => array(
                'sp_team_home' => $home_club_id,
                'sp_team_away' => $away_club_id,
            ),
        ));
    }
}

here is my debug.log:

[24-Jan-2025 07:17:59 UTC] Selected clubs: Array
(
    [0] => mika
    [1] => Yoyo FC
)

[24-Jan-2025 07:17:59 UTC] Number of legs: 1
[24-Jan-2025 07:17:59 UTC] Club IDs: Array
(
    [0] => 1080
    [1] => 329
)

[24-Jan-2025 07:17:59 UTC] Generated schedule: Array
(
    [0] => Array
        (
            [0] => 1080
            [1] => 329
        )

)

[24-Jan-2025 07:17:59 UTC] Home Club ID: 1080 (Type: integer), Away Club ID: 329 (Type: integer)
[24-Jan-2025 07:17:59 UTC] Home Club Name: mika, Away Club Name: Yoyo FC

The form submission triggers a function to create matches for the league. However, the problem is that the clubs section of the "x vs y" match is blank, even though I can manually assign the clubs using the wp-admin UI.

How can I modify the function to automatically assign the selected clubs to the matches when they are created?

Any guidance or suggestions would be greatly appreciated. Thank you!

本文标签: phpclubs not assigned automatically in matches created by wpforms in sportspress pluginStack Overflow