WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#32537 new defect (bug)

404 error on empty title

Reported by: arhit Owned by:
Milestone: Awaiting Review Priority: normal
Severity: critical Version: 4.2.2
Component: Permalinks Keywords: has-patch
Focuses: Cc:

Description

Creating a post without a title returns 404 on single. Even if you edit the post again, add title and update the post. This post will keep returning 404.

How to reproduce this error:

  1. Set permalink settings to "Post name"
  2. Create a new post, don't add title and save. (view post returns 404)
  3. Edit the post again, add title, save. (view post returns 404)

Tested on twentyfifteen theme, no plugins installed.

This bug seems very critical, beside the bad user experiece if user forgets to add the title, it also breaks post-format support on post formats, which don't use the title field (aside,...).

Attachments (1)

32537.patch (2.1 KB) - added by tyxla 3 years ago.
For post name consistency throughout the administration and the database, calling wp_unique_post_slug() on wp_insert_post() when title is empty and post name needs to be generated. Including a unit test of the issue.

Download all attachments as: .zip

Change History (8)

#1 @arhit
3 years ago

  • Severity changed from normal to critical

#2 in reply to: ↑ description @arhit
3 years ago

Update: It happens even with different permalink structure.

#3 @janhenckens
3 years ago

Tested this against 4.2.2 and trunk, both the Twenty Fifteen: Leave the title field empty but make sure you enter some text on the content field.

Permalink setting - post link Default - http://core.dev/?p=1382 works Day and name - http://core.dev/2015/05/30/1382/ works Month and name - http://core.dev/2015/05/1382/ works Numeric - http://core.dev/archives/1382 works Post name - http://core.dev/1382/ doesn't work

Note that when you leave both the title and content fields empty, you also get a Post published. View post message but the post doesn't excist. @arhit Is that what you are referring to when you say that this happens with different permalink settings?

#4 follow-up: @valendesigns
3 years ago

@janhenckens are you testing on the most recent version of trunk? This should have been fixed by #5305 which was merged yesterday.

#5 in reply to: ↑ 4 @janhenckens
3 years ago

Replying to valendesigns:

@janhenckens are you testing on the most recent version of trunk? This should have been fixed by #5305 which was merged yesterday.

Oops, looks like I was behind a couple of commits! Pulled & tested again but the problem still occurs.

With the permalink settings to Post name, the url now looks like this: http://core.dev/13-2/ but it still 404's.

#6 @tyxla
3 years ago

I just confirmed this bug.

After in-depth inspection, I discovered that this occurs because in the wp_unique_post_slug() that is being called in the Permalink section in the administration (the edit-slug-box section).

Here is a quick example: when you publish a post with no title (but with some content), if the post ID is 20, the post name becomes 20-2 in the Permalink section. When using /%postname%/ permalink structure the post URL becomes http://example.com/20-2/, but the post name in the database remains 20. This is why the 404 error occurs.

The Permalink section properly calls wp_unique_post_slug(), which changes 20 to 20-2 to avoid conflicts with date archives (see https://core.trac.wordpress.org/browser/trunk/src/wp-includes/post.php#L3803).

But, as said earlier, the post name remains 20, which leads to the 404 not found error.

Currently, in case the post status is publish, and the post title and post name are empty, the post name is automatically being made the same as the post ID. As a fix I recommend that we use wp_unique_post_slug() on the post name, so it becomes 20-2 as well, making it equal to the current behavior in the administration. Of course, this would happen only if the post title is empty, otherwise the current behavior should be retained.

I'm attaching a patch with a fix for that issue. Also including a unit test that replicates the issue.

@tyxla
3 years ago

For post name consistency throughout the administration and the database, calling wp_unique_post_slug() on wp_insert_post() when title is empty and post name needs to be generated. Including a unit test of the issue.

#7 @tyxla
3 years ago

  • Keywords has-patch added
Note: See TracTickets for help on using tickets.