August 27, 2011
WordPress Press This With Custom Post Types
A few days ago I posted a question on twitter asking about the use of the WordPress Press This feature with custom post types. As I use a custom post type on this site for my Bookmarks I wanted to be able to use the WordPress Press This feature to quickly add a bookmark to my site. After a response from @_mfields I have managed to create a solution.
The WordPress “Press This” feature allows you to quickly blog about the web page that you are currently viewing and once used it automatically populates the post title with the <title> of the page and a link to the web page is placed in the post editor. I use a custom post type in this site to display my bookmarks and therefore I wanted to alter the functionality of the press this feature in two ways:
- The first was to change the way it works so that rather than adding a ‘post’ it would add the content as my bookmarks posts type (pj_bookmarks).
- Secondly the URL of the web page that I am adding as a bookmark needed to be a custom field with the key ‘bookmarkul’ and the value being the URL of the bookmarked web page.
Michael Fields pointed me in the direction of a blog post he wrote a while ago to do a very similar thing which helped a lot. This worked for me perfectly, all I had to do was to change a few of the values to represent my site and custom field names etc.
The two pieces of code below are what is needed. The first goes into your themes functions.php file and the second goes into the bookmarklet (I used the Press This one and then changed the URL):
add_action( 'admin_head-post-new.php', 'pj_process_bookmark_bookmarklet' );
function pj_process_bookmark_bookmarklet() {
global $post; // change pj_bookmarks for your custom post type and pj_bookmarkurl for the custom field key you are using for the bookmark url
if ( isset( $post->post_type ) && 'pj_bookmarks' === $post->post_type && isset( $_GET['pj_bookmarkurl'] ) ) {
$url = esc_url( $_GET['pj_bookmarkurl'] );
print <<< EOF
<script type="text/javascript">// <![CDATA[
jQuery( document ).ready( function ( $ ) {
$( '#metakeyselect' ).val( 'pj_bookmarkurl' );
$( '#metavalue' ).text( '{$url}' );
} );
// ]]></script>
EOF;
}
}
javascript:(function(){ var title = encodeURIComponent( document.title ), bookmark = encodeURIComponent( document.location.href ), url = 'http://markwilkinson.me/wp-admin/post-new.php?post_type=pj_bookmarks&pj_bookmarkurl=' + bookmark + '&post_title=' + title; window.open(url); })();
Original Post by Michael Fields is located here.
Sep 16, 2011 @ 04:49:03
Thanks for sharing your solution! I used it as the basis of a bookmarklet for a “bookmarks” custom post type… building a self-hosted Delicious replacement.
Really appreciate it!
Oct 23, 2011 @ 13:52:11
Thanks for posting this solution. I am trying to use it for a “book” custom type, where I want to populate custom fields like “publisher”. But I got stuck just before the finish line, I think. The javascript is inserted (correctly as far as I can see) into post-new.php:
//
But the value for my custom field is not set:
Do you have any hint for me, why it is not working?
Oct 24, 2011 @ 18:04:09
My first post screwed up and my code-snippets have gone lost on the way, so I am posting them here:
script in post-new.php:
//
custom field post-new.php:
Publisher
Oct 24, 2011 @ 19:18:16
Hi Christian. This blog will strip out certain code for security reasons. Please post your code on an external code paste bin and then repost and I will endeavour to help.
Oct 26, 2011 @ 18:07:44
Hi Mark,
after some research about jQuery I was able to understand the code and found the error by myself. Since I am using the More Fields plugin, I had to adress my custom fields with my own custom field keys like ‘book_publisher’ and not with ‘metakeyselect’ (see line 9).
Everything is working fine now. But thank you very much for offering me your help.
Nov 30, 2011 @ 23:03:22
This is the important article! Thanks for the idea! Using best regards Luke aka couchgool.