5

I'm wondering, is there a way to modify the "Press This" bookmarklet so that posts are saved to a custom post type instead of to a post. Looking through, it doesn't seem as if there are any actions that would let me do that, but I may be wrong.

I know there have been a lot of changes, so I'm thinking that the way it used to be done isn't still relevant.

  • 1
    Haven't super dived in, but this looks like the right approach. Checking it off! – Jason Hoffmann Jul 17 '15 at 19:22
  • I only did few tests when working this out, so it's interesting to hear if it works in general ;-) – birgire Jul 17 '15 at 19:25
6

Press This

Here's a simple plugin to modify the Press-This post type:

<?php
/**
 * Plugin Name: Press-This Custom Post Type
 * Plugin URI:  http://wordpress.stackexchange.com/a/192065/26350
 */
add_filter( 'wp_insert_post_data', function( $data )
{
    $old_cpt = 'post';
    $new_cpt = 'page';  // <-- Edit this cpt to your needs!

    $obj = get_post_type_object( $new_cpt );

    // Change the post type
    if( 
           doing_action( 'wp_ajax_press-this-save-post' ) // Check the context
        && isset( $data['post_type'] ) 
        && $old_cpt === $data['post_type']                // Check the old post type
        && isset( $obj->cap->create_posts ) 
        && current_user_can( $obj->cap->create_posts )    // Check for capability
    )
        $data['post_type'] = $new_cpt;

    return $data;

}, PHP_INT_MAX );

where you have to modify the post type to your needs.

Here we make sure we're in the press-this-save-post ajax context by checking for:

doing_action( 'wp_ajax_press-this-save-post' ) 

We alse make sure the current user has the capability to create the new custom posts.

Update

Since WordPress 4.5 the press_this_save_post filter is available to modify the post data.

Here's an example how we can use it to modify the post type and assign it to a custom taxonomy term:

/**
 * Plugin Name: Press-This Custom Post Type And Taxonomy Term
 * Plugin URI:  http://wordpress.stackexchange.com/a/192065/26350
 */
add_filter( 'press_this_save_post', function( $data )
{
    //---------------------------------------------------------------
    // Edit to your needs:
    //
    $new_cpt    = 'movie';              // new post type
    $taxonomy   = 'actor';              // existing taxonomy
    $term       = 'john-wayne';         // existing term
    //---------------------------------------------------------------

    $post_object = get_post_type_object( $new_cpt );
    $tax_object  = get_taxonomy( $taxonomy );

    // Change the post type if current user can
    if( 
           isset( $post_object->cap->create_posts ) 
        && current_user_can( $post_object->cap->create_posts ) 
    ) 
        $data['post_type']  = $new_cpt;

    // Change taxonomy + term if current user can    
    if ( 
           isset( $tax_object->cap->assign_terms ) 
        && current_user_can( $tax_object->cap->assign_terms ) 
    ) 
        $data['tax_input'][$taxonomy]   = $term;

    return $data;

}, 999 );
| improve this answer | |
  • @Paraglider Create the plugin file /wp-content/plugins/custom-press-this-post-type/custom-press-this-post-type.php and activate the plugin from the wp-admin as usual. – birgire Jan 26 '16 at 9:15
  • @Paraglider I just tested this and it seems to work. Please post a new question to address the problems you have, thanks (I first thought you're talking about the Quick-Draft feature and I came up with a solution to change the post type there and posted it here before realizing that this answer is about Press-This, so I removed that part again ;-) – birgire Jan 28 '16 at 18:21
  • Awesome plugin! Tested it in WordPress 4.7.2 running the Twenty Seventeen theme with a custom post type. Posts a saved in the desired custom post type, but 1) the post doesn't have an author, 2) the plugin doesn't find categories for the related custom post type and 3) the plugin can't create/add new categories for the related custom post type. Can this be solved? – Iurie Malai Feb 28 '17 at 22:02
  • 1
    Glad to hear it useful to you @lurie. I updated the answer with a taxonomy-term assigning example. But I think wp_insert_post() can't create a new term, but I haven't checked. Maybe you need to create it before. When I test it, the post is assign ot the current user id, but you can adjust it with $data['post_author'] and e.g. check if the current user can edit posts if it's a different user id. – birgire Mar 1 '17 at 12:31
  • I played two days with the "Press This" bokmarklet and your code (with the old and with the new one) and the result is the same: posts can be saved as custom post type, but categories and tags always can be created or selected only for the default "post" type. I suppose, this is the reason why @brasofilo suggests to hide the post formats, categories and tags stuff from the "Press This" screen. I can live with this, as categories and tags can be added later from the backend, but it would have been great to have all elements working. Anyway, thanks! – Iurie Malai Mar 2 '17 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.