Save Press This Posts in a Custom Post Type
-
03-10-2020 - |
Question
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.
La solution
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 );