Earlier today I posted that I was thinking about creating a Chrome extension which would allow me to easily add new sites to my bookmarks section. My friend Devin responded asking if I knew about the Press This functionality. Although I was in fact aware of Press This, I had to admit that I’ve never actually used it before so I gave it a try. While I was quite impressed with what it did, it did not really do what I needed.
How Press This Works
- The title tag of the html document is inserted as the value of the post title.
- If any text was selected, it is inserted into the content box
- A link to the document is appended to the content box.
What I Need to Do
- I need to create a bookmark and not a post.
- I need to automatically set the title.
- I need all custom taxonomies to display.
- I need to set the name and value of a custom field.
On my site I have one custom post_type registered (mfields_bookmark) which I use to archive interesting articles about web development that I have read. Unfortunately, Press This only seems to enable you to create “posts”. My initial idea was to create a custom popup window by copying the code from
press-this.php into a custom function which would be triggered via
admin-ajax.php. Seemed like a good idea, but it also sounded like a lot of work! After thinking about it, I really didn’t need the popup window, I would be happy if my bookmarklet just opened
post-new.php in a new tab. So I started hacking
post-new.php's query string to see if the post form would recognize any values… And it did!
The post title can be injected by setting a value to post_title. Something Like this works rather well:
Although, I will not need to set the content here, I found that it can easily be set in much the same way:
This function will fire during the
admin_head action on
post-new.php for the mfields_bookmark post_type. If mfields_bookmark_url is defined in the query string, the function will automatically select it as the name from the select box. Likewise, the value of mfields_bookmark_url will be used as the value of the custom field.
Creating the Bookmarklet
Basically when you call this function, it will open post-new.php in a new tab sending the title and url of the document you were just viewing as well as setting the value of post_type to mfields_bookmark.
I hope that someone out there finds this useful. I’m pretty excited about it as it will really cut down the steps involved in posting bookmarks on my site.