Is there any way to add class to edited posts in order to style them differently?
If there is a way to do that without adding a class, this option would be much better.
EDITED – IGNORE THE ABOVE QUESTION – SEE BELOW
Is there a way to add a check box (meta box) in the area that a user writes an article/post so when this is checked a class will be added on the specific post (in loop and in single post page)? If something like this way is impossible, is there a way to get this done with a custom field? If both are possible I would prefer the first way.
This is very similar to @mrwweb’s answer, but a little bit more fleshed out. We’re also not going to overload taxonomies in order to get our post class, but do what you wanted initially: use a custom meta box.
There is a built in template tag called
post_class
. Like almost every other function in WordPress you can control its output via a filter.Step one: add the meta box.
This is done via
add_meta_box
. If you’re wondering what the arguments are, you can head on over to the codex — the docs are very good. The thing to be concerned about is the third argument which specifies a callback function — the callback is what renders the HTML. To add the meta box we’ll hook intoadd_meta_boxes_post
and calladd_meta_box
.Here’s the whole deal:
Now we need to actually save that stuff when the post is saved. You hook into
save_post
to do that:Finally, hook into
post_class
to add your ‘edited’ class. Your hooked function will get three arguments, we’re concerned with the first and last: the array of post classes and the post id respectively.Done! All that seems like quite a bit of work, however. So why not just hook into
post_class
and compare thepost_created_gmt
andpost_modified_gmt
?Here is all of that as a plugin.
Basically what you want to do is write either a function, or a small plugin (not so challenging if you know php).
To have you code fired at the right time, you can hook into an action or filter that will happen when a post is updated.
If you are not sure what that means, you should read up on wordpress.org on how to write plugins, and see the Action and Filter API’s. The main difference between actions and filters is that a filter lets you change some data. In your case that is interesting, because the filter wp_insert_post_data allows you access to all of the post content and metadata before it is stored in the database.
This means you could use the content to compare to the content that is already stored to see if this is a substantial edit, or just a small one. Of course you could implement a feature where the user can choose, or top it all and do both, like a checkbox that will turn on if the editor content differs by more than 15 characters from the stored post, but will not change state anymore once the user has checked/unchecked it manually.
However I would keep it simple for a first version and just do a comparison in php and add the right class automatically. That is considerably simpler to implement and probably more desirable than needing the user to decide on this consciously.
So, technically it comes down to this:
Regarding 3, note that wp doesn’t keep classes in the database, they are generated at runtime when pages are generated. You want to keep some durable info linked to the post though, so you need to add something. I think you have the choice between a custom meta field or the existing status field. The latter might have some complications in the sense that if you change articles status from published to edited say, you might have to check what all the consequences of that are. Potentially wordpress code or plugins count on posts being “published” to do certain things.
Now, I don’t know what your php skill is. If you need more explanation let me know and I can elaborate those five points in a bit more detail. Otherwise, codex.wordpress.org has all the explanation you need.
Ps: Don’t forget that programming is 75% error handling. For example wp_insert_post_data will also fire when a new post is created and obviously checking the database for a previous version will not give you a sensible result to compare with. Things like that should be obvious, but I think for a lot of people they aren’t. In essence, think scenarios. Try to conceive all possible input your code could get and try to consciously do the best thing you can in every situation.
A custom meta box with a checkbox field would probably work best, but the easiest solution I can think of is this:
post_class()
on the wrapper element of each article/post/page.*.category-edited
class on the post wrapper (or .category-{ID}, which is safer if the “edited” slug ever changes).The
post_class()
function (read about it on the Codex) is a template tag used in theme files. It’s put on the wrapping element for each post in a loop. It can be on any HTML element, but common ones arediv
,article
(HTML5 only), andli
. Using the class looks like this:Post_class()
then generates something like “class=’post post-23 category-events'”.