Opened 18 years ago
Closed 16 years ago
#3794 closed enhancement (invalid)
Create filter hooks on post save and publish that provide access to the entire post array
Reported by: | Justinsomnia | Owned by: | markjaquith |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 2.1 |
Component: | General | Keywords: | has-patch |
Focuses: | Cc: |
Description
Scenario
Title-less blog posts are common in linklogs (aka asides). Currently WordPress does not generate titles for these types of posts, which has two implications:
- nonexistent titles appear in feeds, which break some aggregators that rely on the existence of a title to link back to the post
- post_names (aka slugs) get generated based on the post ID, creating human-unreadable permalinks, e.g.
http://example.org/2007/02/4201/
Prior Art
- Blogger uses the first 32ish characters of the post for the post_title and post_name, without breaking a word in half
- Jason Kottke generates a post_title based on the first 70ish characters of his post, without breaking words, adding ellipsis to titles that were based on truncated content. Note: the generated title is only visible in his feed.
- I'm not sure what MT does
Proposal
I propose that WordPress generate a title for title-less posts, at the moment the post is published (similar to how a post_name is generated) based on the first 100 characters of the post_content (excepting html tags).
This generated title would then be the basis for the post_name. In the event that 100 characters falls in the middle of a word, that word would not be included in the generated title. For all titles generated from content greater than 100 characters in length, 3 periods (...
) will be added to the end of the title (aka an ellipsis) which wptexturize will later translate into a ellipsis entity.
Implementation
I've implemented this as a function called generate_title_from_content()
, see attached patch.
Attachments (4)
Change History (27)
#4
@
18 years ago
Wouldn't using the new links/BlogRoll/bookmarks functionality for linklogs/asides make more sense? Just asking.
Also, I don't like that this auto-generates a non-blank title. What if I want a blank title? If you want the post_name to be generated better because of the blank title, then do that. Don't auto-fill the title with something I might not want.
#5
follow-ups:
↓ 8
↓ 10
@
18 years ago
Andy, I just pulled the 100 chars number out of my butt. Going over my 4+ year archive of 800+ posts, 99% had titles less than 100 characters in length. 95% had titles 50 characters or less in length.
One of the pleasures of WordPress (imho) is its unconstrained post_name length, so I went with a larger number to forestall any concerns about truncating too much (like MT's 16-20 chars). Nonetheless it's trivial to adjust the default length downward.
One other implication of leaving the title blank I forgot to mention: WordPress's own Manage > Posts interface (wp-admin/edit.php) displays nothing if the published post's title is blank. Kind of makes it difficult to determine at a glance what the post is about (see attached).
Otto42, I hear your concern, but I would suggest that the way to turn it off would be through the template, rather than a global config option. Remember having a title really benefits the feeds. It seems to me that the first X chars of the post makes for the least offensive generated title. That said, if consensus is for enabling or disabling this feature via a config option, I could be happy with that.
#6
follow-up:
↓ 9
@
18 years ago
I think a good way of allowing people to disable this is via a filter on the auto-generated title. That way they can just add a filter that returns a blank string and it will revert to the old behavior. Filters cost us a lot less than an option.
#7
@
18 years ago
Yeah, lets keep options down to a minimum. In fact, couldn't this just be left as plugin material? I've personally no problem with it, but as Mark points out, it is a big change in behaviour... Have there been many calling for this change? People can actively choose to install a plugin that filters the title and generate one from the post content if they really want to.
The post name could be generated in the way you suggest just for the purpose of viewing in edit.php, just as it is displayed.
#8
in reply to:
↑ 5
@
18 years ago
Replying to justinsomnia:
Otto42, I hear your concern, but I would suggest that the way to turn it off would be through the template, rather than a global config option. Remember having a title really benefits the feeds. It seems to me that the first X chars of the post makes for the least offensive generated title. That said, if consensus is for enabling or disabling this feature via a config option, I could be happy with that.
I'm not suggesting a global config option. I'm suggesting not having this change at all. Or, if you do have it, have it a) done via a filter and b) not on by default. I'd prefer that this be in a plugin entirely, as I cannot see any use for it outside of these specialized cases, but I do think that having a filter on the title could be useful for other things.
#9
in reply to:
↑ 6
@
18 years ago
Replying to markjaquith:
I think a good way of allowing people to disable this is via a filter on the auto-generated title. That way they can just add a filter that returns a blank string and it will revert to the old behavior. Filters cost us a lot less than an option.
A better idea would be to put a filter on the title. If you then want to run this function as a filter, you can. But having it do this and then having to make me make a filter to put it back to the correct behavior is silly. At most I should have to do a "remove_filter" and nothing else. I would prefer that active action have to be taken in the first place.
There is nothing inherently wrong with blank titles. They have uses. Don't eliminate them as an option, especially when this whole thing could (should) be a plugin in the first place.
#10
in reply to:
↑ 5
@
18 years ago
Replying to justinsomnia:
Remember having a title really benefits the feeds.
I missed this until just now.
Title is not a required element under item, according to the RSS 2 specification. If aggregators are treating it as such, then the aggregator is broken.
From the spec:
"All elements of an item are optional, however at least one of title or description must be present."
#11
follow-up:
↓ 12
@
18 years ago
Behavior change indeed, but a pretty vanilla one that I think fills in a hole of missing functionality. Feed items with missing titles is not a "specialized case" in my opinion. I just saw your RSS 2.0 quote, a spec which will surely lead us down a path of ambiguity. Just for fun, you might be curious to know that the Atom spec sez:
"atom:entry elements MUST contain exactly one atom:title element."
I sympathize with the "I don't want WP generating my titles" perspective, but I believe that the vast majority of people write their own titles, so this is really only a behavior change for the minority of us who happen to not for whatever reason (link blogs, custom CMS). I am curious though, Otto, what would this break for you?
I'd planned on creating a plugin to accomplish this, but I wanted title generation to occur in the same way that post slugs do, at the moment the post is published, and only if the title is blank, and before the post_name is generated. Based on my cursory understanding of the code, the only place to accomplish that is exactly where I inserted my generate_title_from_content()
function call in wp-includes/post.php.
I'm not sure whether this same functionality could be accomplished as a plugin with the present pre_save filters (I didn't think so, but I'm not a WP filter guru), or if this warrants a new filter hook... All pointers are welcome.
#12
in reply to:
↑ 11
@
18 years ago
Replying to justinsomnia:
Just for fun, you might be curious to know that the Atom spec sez: "atom:entry elements MUST contain exactly one atom:title element."
Yes, well, I've disabled atom on my blog anyway, so that doesn't much concern me. Atom is dead except for Google using it. :P
I am curious though, Otto, what would this break for you?
Err.. The ability to write posts without titles? Seems fairly obvious. ;-)
I just don't necessarily think a "title" should be a required part of a post. I can see cases where you could write untitled posts and thus have no title show up and that be desired behavior. Okay, that's a little emo I suppose, but still... It doesn't seem like an edge case to have title-less posts.
Given that the original request complained about the post-name being ugly, it seems to me that the solution should address that directly instead of making it pretty by forcing a post title that the user may not want. It's not necessarily desirable behavior to always have a post title, and having to use a hack/workaround to allow blank titles seems silly.
I also object to the implementation given, however if the thing was turned into a filter which could be easily disabled, I would not have nearly as much of a problem with it.
I'd planned on creating a plugin to accomplish this, but I wanted title generation to occur in the same way that post slugs do,
Which is a good reason to have a filter acting here and allow different plugins to access it that way, allowing for differing implementations.
#13
@
18 years ago
If the central issue is feed validation, couldn't we generate a title for title-less posts just in the feed?
Forcing the creation of titles seems to go against what I see as WordPress's trend towards being a light-weight CMS instead of just blogging software.
#14
follow-up:
↓ 15
@
18 years ago
Filosofo, I guess I saw 2-3 birds (title-less feeds, busted wp-admin/edit.php interface, and human unreadable post_names) and figured it would be easier to kill them with a single stone (auto-generating a title), rather than having to track down every place the existence of a title is expected and then dynamically running something like my generate_title_from_content()
function.
I think the behavior change that I've proposed is a good one that should be the default, but I agree with markjaquith's suggestion that there should be a filter + plugin available (which I will happily write) to disable auto-generated titles.
I'm not a WP filter expert, but I'll look into creating a patch that has a filter which can be used to disable title generation via a plugin.
#15
in reply to:
↑ 14
@
18 years ago
Replying to justinsomnia:
I think the behavior change that I've proposed is a good one that should be the default, but I agree with markjaquith's suggestion that there should be a filter + plugin available (which I will happily write) to disable auto-generated titles.
The existing behaviour has been around for a long time, and there's no great big call from people to change it. When Googling this (admittedly briefly), I couldn't find any complaints in regards to this. A filter is a good idea, but how about writing a plugin that provides what you ask, instead of one for turning off introduced functionality which nobody else has requested. +1 for a filter, +1 for filosofo's idea of generating a post title as and when necessary, -1 for automatically generating the title by default.
#16
@
18 years ago
In fact, the filter already exists: 'title_save_pre'. Maybe we could just add a second parameter to that which contains an array of the post data - category, content, author, id etc.
Then plugins can autogenerate titles flexibly such as "Jamie's Post 1" or 'Michael on [Aeroplanes, Boeing]".
Less restrictive, off by default and able to do what you're asking for as well...
#17
@
18 years ago
majelbstoat, I do like your idea of more configurable auto-generated titles.
In that spirit, I dug a little deeper into filters (I hadn't written any plugins using them) so I didn't know well enough what I could or could not do with them. My hunch, that the save_pre filters are atomic and thus one value (the content) can not affect another (the title), turned out to be true. Actually given the order of the filters I could store the content as a global variable or an instance variable, but this depends heavily on the order of the filters.
So I set out to add the filter (actually two) that I've always wanted, one that has access to all attributes of a post and differentiates between publishing and just saving. I called these filters post_publish_pre
and post_save_pre
respectively, they happen very early on in the wp_insert_post()
method because of the use of extract()
early on, and they pass the entire $postarr
array along to the filter function.
See attachment:ticket:3794:post.php.diff for the diff and attachment:ticket:3794:title-auto-generation.php as a proof of concept plugin that implements the generate_title_from_content()
function from my previous diff using these new filters.
#18
@
18 years ago
Now, that I like. By passing in the whole post array, you can make filters modify posts in all sorts of interesting ways. Very wide open. Much better.
Clever. I like it.
+1, but will wait for other feedback since this is a big behavior change.