WordPress.org

Make WordPress Core

Opened 16 months ago

Last modified 16 months ago

#44595 new defect (bug)

wp_insert_post() inserts wrong GUID (adds http:// prefix)

Reported by: Looimaster Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords: 2nd-opinion
Focuses: Cc:
PR Number:

Description

I manually set a GUID to e.g. abc123 before calling wp_insert_post() and it was inserted as http://abc123 to the database.

Expected: abc123

Change History (3)

#1 @JPry
16 months ago

  • Keywords 2nd-opinion added

The process that actually adds the http:// prefix is the esc_url_raw() function hooked to the pre_post_guid filter. This can be found by searching for the filter in default-filters.php.

I don't believe this is an actual bug. WordPress uses GUIDs as an actual URL for RSS feeds, and so GUIDs need to be a real URL rather than an arbitrary text string. With that said, I'm not going to change the status of this ticket, in case there's other feedback. I found a bit of information related to GUIDs here: https://deliciousbrains.com/wordpress-post-guids-sometimes-update/.

EDIT:

The actual filter is triggered within the sanitize_post_field() function in the form of "pre_post_{$field}".

Last edited 16 months ago by JPry (previous) (diff)

#2 follow-up: @Looimaster
16 months ago

I think this is a bug because:

  1. Maybe as you say


"...WordPress uses GUIDs as an actual URL for RSS feeds, and so GUIDs need to be a real URL rather than an arbitrary text string..."

and that's fine with me. You can generate http://example.com/ as your GUIDs by default.

  1. But when a user explicitly sets a GUID to an actual GUID in for example 123e4567-e89b-12d3-a456-426655440000 format then in my opinion that shouldn't be modified by WordPress.
  1. By definition (and expectation) GUID is a defined concept (https://en.wikipedia.org/wiki/Universally_unique_identifier). WordPress currently uses this concept in an unofficial/arbitrary way if it really uses it as "...actual URL for RSS feeds...".

In the RSS feed I only see <guid isPermaLink="false">http://example.com/?p=123</guid> so I don't think that GUID must be a URL. It could be anything.

I think GUID should be restricted to being an actual GUID as defined by Wikipedia (because it's a great and useful idea and the only column that I can count on being globally unique and I could use it to for example synchronize the content between multiple sites) and if you need "...actual URL for RSS feeds..." then that shouldn't be named "GUID" but something else like feed_link.

So I vote for implementing this fix:

  1. For new posts inserted like wp_insert_post( array( "post_title" => "Some title" ) ) generate whatever you like.
  2. But for new posts inserted like wp_insert_post( array( "post_title" => "Some title", "guid" => "abc123" ) ) (where user explicitly indicates that they want to actually use GUID concept for something) don't modify it.

#3 in reply to: ↑ 2 @JPry
16 months ago

@Looimaster One option for your case is to just remove the esc_url_raw() function from the pre_post_guid filter:

remove_filter( 'pre_post_guid', 'esc_url_raw' );

That would allow you to use any GUID you want without the http:// being prepended.

Note: See TracTickets for help on using tickets.