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:

  1. 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).
  2. 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):

[php]
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
//
EOF;
}
}
[/php]
[js]
javascript:(function(){ var title = encodeURIComponent( document.title ), bookmark = encodeURIComponent( document.location.href ), url = ‘https://markwilkinson.me/wp-admin/post-new.php?post_type=pj_bookmarks&pj_bookmarkurl=&#8217; + bookmark + ‘&post_title=’ + title; window.open(url); })();
[/js]

Original Post by Michael Fields is located here.

Published by

Mark Wilkinson

Lead developer & owner at Highrise Digital @highrisedigital. Develops the @wpbroadbean plugins. Former teacher.

11 thoughts on “WordPress Press This With Custom Post Types”

  1. 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!

  2. 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?

  3. 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

  4. 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.

  5. 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.

  6. Unfortunately, it doesn’t work for me (WP 3.5). Are you still using this with the new version of WP and does it work for you? Thanks!

  7. Hi Mark,

    Do you know if it is possible to get the press this bookmark to grab an embed code from a page in the form of an iframe and add it to the page instead of grabbing the url or text content from the page?

  8. This fails to work. I assume it is because of updates to WordPress. Any chance of a link to a maintained plugin that can produce this. I too am interested in a self-hosted replacement for del.icio.us.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s