Opened 8 years ago
Last modified 8 months ago
#42733 assigned defect (bug)
WordPress Embed URLs don't work for draft and scheduled posts with pretty permalinks
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Future Release | Priority: | normal |
Severity: | normal | Version: | |
Component: | Embeds | Keywords: | has-patch has-unit-tests |
Focuses: | Cc: |
Description
This came up during work on #41451. Two tests fail with is_embed is false but is expected to be true:
Tests_Embed_Template::test_oembed_output_draft_post
Tests_Embed_Template::test_oembed_output_scheduled_post
Without pretty permalinks, the return of get_post_embed_url()
is http://example.org/?p=8&embed=true
. This URL is recognised as an embed URL when the request is parsed.
But with pretty permalinks enabled, the return is http://example.org/?p=8/embed/
. This is not recognised as an embed URL.
The reason for the mixed URL structure seems to be get_permalink()
. This function does not return pretty permalinks for draft or future posts, see https://core.trac.wordpress.org/browser/tags/4.9/src/wp-includes/link-template.php#L165
Change History (7)
#1
@
8 years ago
- Keywords needs-patch needs-unit-tests added
- Milestone changed from Awaiting Review to 5.0
#2
@
7 years ago
Maybe adding a third parameter to get_permalink
- $force_pretty
that disregards the post status and outputs pretty? Just a thought.
This ticket was mentioned in PR #7747 on WordPress/wordpress-develop by @debarghyabanerjee.
8 months ago
#7
- Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed
Trac Ticket: Core-42733
## Problem
The get_post_embed_url()
function was failing to generate the correct embed URLs for draft and scheduled posts when pretty permalinks were enabled. The issue specifically occurred for these draft and scheduled posts, where the embed URLs were being generated incorrectly:
- When pretty permalinks were disabled, the function correctly generated embed URLs for draft and scheduled posts with the
?embed=true
query parameter (e.g., http://example.com/?p=8&embed=true), which is recognized as a valid embed URL by WordPress.
- When pretty permalinks were enabled, the function incorrectly generated URLs like http://example.com/?p=8/embed/ for draft and scheduled posts. These URLs were not recognized as embed URLs, causing the
is_embed()
check to fail (returning false instead of the expected true).
- This discrepancy occurred because
get_permalink()
does not return pretty permalinks for draft or scheduled posts. As a result, the embed URL logic was inconsistent for draft and scheduled posts, leading to test failures.
## Solution
- To address this issue, the logic in
get_post_embed_url()
has been updated to consistently generate correct embed URLs for all posts (published, draft, or scheduled), regardless of whether pretty permalinks are enabled.
## Key changes include
### For Published Posts
- When pretty permalinks are enabled, the embed URL now correctly ends with /embed/ (e.g., http://example.com/my-post/embed/), as expected.
- If pretty permalinks are disabled, the function falls back to using the ?embed=true query parameter format (e.g., http://example.com/?p=8&embed=true).
### For Draft and Scheduled Posts
- The function now consistently generates embed URLs with the ?embed=true query parameter for draft and scheduled posts, regardless of whether pretty permalinks are enabled. This ensures that draft and scheduled posts are always recognized as embed URLs.
### Fallback Logic:
- The function now gracefully handles conflicts between the post's permalink structure and the
/embed/
path to avoid generating invalid URLs. In cases where the permalink structure conflicts, the function defaults to the query string format (?embed=true
).
## Benefits
- Consistent URL Format: Ensures that embed URLs are correctly generated for all post statuses (published, draft, and scheduled), regardless of the permalink structure.
- Correct Handling of Pretty Permalinks: Fixes the issue where pretty permalinks caused the function to generate invalid embed URLs for draft and scheduled posts. Now,
get_post_embed_url()
always returns a valid embed URL with the correct format.
Yeah it'd be great if
get_post_embed_url()
could get some improvement to make the URLs correct for such posts.