保存帖子时更新Slug:基于Tribe Events Calendar实现唯一Permalink
Append Tribe Events Date to Slug on Save (Refined Logic)
Got it, let's refine your existing save_post hook logic to reliably append the event's date to its slug—while avoiding common pitfalls like duplicate date suffixes, infinite loops, and affecting non-event posts. Here's a robust implementation tailored for Tribe Events Calendar:
Step-by-Step Refined Code
add_action('save_post', 'append_tribe_event_date_to_slug', 10, 3); function append_tribe_event_date_to_slug($post_id, $post, $update) { // Skip autosaves, revisions, and non-Tribe Events posts if (wp_is_post_autosave($post_id) || wp_is_post_revision($post_id) || $post->post_type !== 'tribe_events') { return; } // Ensure current user has permission to edit this event if (!current_user_can('edit_post', $post_id)) { return; } // Grab the event's start date in a slug-friendly format (Y-m-d) $event_start_date = tribe_get_start_date($post_id, false, 'Y-m-d'); // Bail if we can't retrieve a valid date if (empty($event_start_date)) { return; } // Clean up existing slug: remove date suffix if it already exists $original_slug = $post->post_name; $base_slug = preg_match('/-\d{4}-\d{2}-\d{2}$/', $original_slug) ? preg_replace('/-\d{4}-\d{2}-\d{2}$/', '', $original_slug) : $original_slug; // Generate the new unique slug $new_slug = $base_slug . '-' . $event_start_date; // No need to update if slug hasn't changed if ($new_slug === $original_slug) { return; } // Prevent infinite loop: temporarily remove our hook before updating the post remove_action('save_post', 'append_tribe_event_date_to_slug'); // Update the post's slug (post_name) without altering other content wp_update_post([ 'ID' => $post_id, 'post_name' => $new_slug ]); // Re-add our hook for future saves add_action('save_post', 'append_tribe_event_date_to_slug', 10, 3); }
Key Logic Improvements Explained
- Targeted Execution: We only run this code for
tribe_eventsposts, so your regular posts/pages won't be affected. - Avoid Duplicate Suffixes: The regex check ensures that if you edit and re-save an event, we don't append the date again (e.g., no
event-2024-05-20-2024-05-20slugs). - Infinite Loop Prevention: By temporarily removing the
save_posthook before callingwp_update_post, we stop the update from triggering our function again. - Safety Checks: We skip autosaves/revisions and verify user permissions to prevent unintended actions.
- Tribe-Compatible Date Fetch: Using
tribe_get_start_dateensures we're pulling the correct event date (instead of relying on post publish dates, which might not match the event date).
Additional Notes
- Test First: Always test this in a staging environment before deploying to production.
- Custom Date Format: If you prefer a different slug format (e.g.,
20240520), modify the'Y-m-d'parameter intribe_get_start_date—just stick to alphanumeric characters and hyphens for valid slugs. - Conflict Check: If you use other slug-modifying plugins (like Yoast SEO), test for conflicts to ensure both tools play nicely together.
内容的提问来源于stack exchange,提问作者Klaas Leussink
相关产品推荐
相关产品推荐

