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):
[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=’ + bookmark + ‘&post_title=’ + title; window.open(url); })();
[/js]
Original Post by Michael Fields is located here.
Leave a Reply