Skip to content

Instantly share code, notes, and snippets.

@glueckpress
Last active September 14, 2019 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glueckpress/fc0ef2d8f9a245163f1162f4f13ab1ca to your computer and use it in GitHub Desktop.
Save glueckpress/fc0ef2d8f9a245163f1162f4f13ab1ca to your computer and use it in GitHub Desktop.
[WordPress][Press This] Sets a custom default post format and category for post drafts saved via PressThis.
<?php
/**
* Plugin Name: Press This | Default Terms
* Description: Sets a custom default post format and category for post drafts saved via Press This.
* Version: 0.0.2
* Author: Caspar Hübinger
* Author URI: http://glueckpress.com/
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
defined( 'ABSPATH' ) or exit;
/**
* Add category and post format to Press This post before saving
*
* See: https://github.com/WordPress/press-this/blob/master/class-wp-press-this-plugin.php#L159
*/
add_filter( 'press_this_save_post', function ( $post_data ) {
// Post format must be supported by theme
// Note: Idk why, but the post format in Press This itself when you save a
// draft still says Standard. However, when you open the draft in the
// regular editor, it shows the value from here; so this works, even if
// it looks like it doesn’t as long as you’re in Press This.
$post_data['post_format'] = 'link';
// Get category ID from slug
$category = get_category_by_slug( 'example-category' );
$category_id = absint( $category->term_id );
if ( 0 < $category_id ) {
$post_data['post_category'] = [ $category_id ];
}
return $post_data;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment