I have a baseball related website with multiple authors. I use the “Stick this post to the front page” to denote an “Editor’s Pick” on an article. I would like to add a link/button to allow editor to do this from the front end. The method can either be in the article itself or in the Admin Bar. I do not really have a preference.
I have looked through many, many different “Admin Bar” plugins, but haven’t been to find anything relating to “Stick this post to the front page”.
Thanks
I think this small source code is your solution. It currently doesn’t have frontend feedback for the Sticky Post change, but you can enhance this string, class or whatever you want in the function
fb_stick_post
.The first function adds the item in the Admin Bar and creates a new Url with a param value. Also,
on click
calls a function to read the Url param and, if it’s true, then do the change to the sticky status of this post id.Update 07/30/2012
Now an small plugin with easy solution. The plugin adds an item inside the Admin Bar. The string of the button check for is sticky and give the possibility to stick or unstick the current post.
I change the hook for add the admin bar item to ´template_redirect` for use an redirect after update the stick flag on post.
or download this plugin Gist 3214922
There are a few functions that come in handy here:
unstick_post
– Unstick a poststick_post
– Stick a postis_sticky
– Figure out if a post is stickyWith those three in mind, all we need to do is stick them together with some admin menu bar glue.
First off, let’s wrap everything in a class for fun and profit. This class will have some constants that we’ll use later: a nonce, an action for unsticking the post and an action for sticking the post.
Then let’s add an init function to add our actions. The first action being that we hook into template_redirect.
NOTE: from here on out I’ll be omitting the
class
bit. You can view the entire thing here.In the function hooked into
template_redirect
, we’ll check to see if we’re on a single post page and whether or not the user can edit it. If they can, we’ll hook intoadmin_bar_menu
andwp_footer
.In the
menu
function, hooked intoadmin_bar_menu
, we can add our new item:Here we get our first “utility function” that builds a URL for the admin menu bar node. It’s just a wrapper around
add_query_arg
to nonce, and build a url that we’ll use with AJAX later on:The
footer
function just spits out some JavaScript to make the AJAX calls. Basic overview: when someone clicks on our new link, make a GET request to the given URL. If it’s successful, change the (un)stick link’s href, text, and title.And now we come to the AJAX callbacks. Ajax in plugins/themes is worth reading up on.
We’ll modify our
init
function a bit to add two more actions:And our AJAX callbacks. These could very easily be the same function. I split them up here because it seemed easier to extend/change in the future. Both of these check to see if the AJAX request is valid, (un)stick the post accordingly, and echo out the new URL for future (un)sticking.
Our second “utility function” makes an appearance here.
can_ajax
verifies our nonces and user permissions and returns post ID to (un)stick. If any checks fail, it exits (viadie('1')
).Here is that whole mess as a plugin.
Here is a much simpler solution that will get the job done using
the_content
filter hook