Skip to content

Instantly share code, notes, and snippets.

@2ndkauboy
Created December 20, 2016 01:23
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 2ndkauboy/8858b9f3f1fb3de2aedabcdcd283ce7e to your computer and use it in GitHub Desktop.
Save 2ndkauboy/8858b9f3f1fb3de2aedabcdcd283ce7e to your computer and use it in GitHub Desktop.
A small plugin that enables tags on PDF files to create a simple press review archive using the theme's attachment archive template with a custom bulk action to add multiple files to that tag.
<?php
/**
* Press Review Archive
*
* @package press-review-archive
* @author Bernhard Kau
* @license GPLv3
*
* @wordpress-plugin
* Plugin Name: Press Review Archive
* Plugin URI: https://kau-boys.de
* Description: A small plugin that enables tags on PDF files to create a simple press review archive using the theme's attachment archive template
* Version: 0.1
* Author: Bernhard Kau
* Author URI: https://kau-boys.de
* License: GPLv3
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
*/
/**
* Enable tags on attachments
*/
function press_review_archive_register_taxonomy_for_object_type() {
register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
add_action( 'init', 'press_review_archive_register_taxonomy_for_object_type' );
/**
* Adds a rewrite rule for the press review archive
*/
function press_review_archive_add_rewrite_rule() {
add_rewrite_rule(
'^pressespiegel(?:/(\d{4}))?(?:/(\d{2}))?(?:/(\d{2}))?/?',
'index.php?tag=pressespiegel&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]',
'top'
);
}
add_action( 'init', 'press_review_archive_add_rewrite_rule' );
/**
* Change the main query to show only PDF files on the press review archive
*
* @param WP_Query $query The main query.
*/
function press_review_archive_pre_get_posts( WP_Query $query ) {
if ( is_press_review_archive() ) {
$query->set( 'post_type', 'attachment' );
$query->set( 'post_mime_type', 'application/pdf' );
$query->set( 'post_status', 'inherit' );
}
}
add_action( 'pre_get_posts', 'press_review_archive_pre_get_posts' );
/**
* Set the archive title and remove thr prefix.
*
* @param string $title The archive title.
*
* @return string
*/
function press_review_archive_get_the_archive_title( $title ) {
if ( is_press_review_archive() ) {
$tag = get_term_by( 'slug', 'pressespiegel', 'post_tag' );
$title = $tag->name;
}
return $title;
}
add_filter( 'get_the_archive_title', 'press_review_archive_get_the_archive_title' );
/**
* Set the post thumbnail to large size for the press review archive
*
* @param string $p The attachment HTML output.
*
* @return string
*/
function press_review_archive_prepend_attachment( $p ) {
if ( is_press_review_archive() ) {
$p = '<p class="attachment">';
$p .= wp_get_attachment_link( 0, 'large', false );
$p .= '</p>';
}
return $p;
}
add_filter( 'prepend_attachment', 'press_review_archive_prepend_attachment' );
/**
* A helper function to test if the currently requested page is a press review archive pae
*
* @return bool
*/
function is_press_review_archive() {
return is_archive() && is_tag( 'pressespiegel' );
}
/**
* Adds a new option to the bulk actions of the media library.
*
* @param array $bulk_actions The bulk actions for the screen.
*
* @return mixed
*/
function press_review_archive_register_bulk_actions( $bulk_actions ) {
$bulk_actions['press_review_archive'] = __( 'Add to press review', 'press-review-archive' );
return $bulk_actions;
}
add_filter( 'bulk_actions-upload', 'press_review_archive_register_bulk_actions' );
/**
* Add the selected elements to the press review archive tag
*
* @param string $redirect_to The redirect URL.
* @param string $doaction The action being taken.
* @param array $post_ids The items to take the action on.
*
* @return string
*/
function press_review_archive_bulk_action_handler( $redirect_to, $doaction, $post_ids ) {
if ( 'press_review_archive' !== $doaction ) {
return $redirect_to;
}
$added_items = 0;
foreach ( $post_ids as $post_id ) {
$post = get_post( $post_id );
if ( 'application/pdf' === $post->post_mime_type ) {
$term_taxonomy_ids = wp_set_object_terms( $post_id, 'pressespiegel', 'post_tag', true );
if ( ! is_wp_error( $term_taxonomy_ids ) ) {
$added_items++;
}
}
}
$redirect_to = add_query_arg( 'press_review_archive_added', $added_items, $redirect_to );
return $redirect_to;
}
add_filter( 'handle_bulk_actions-upload', 'press_review_archive_bulk_action_handler', 10, 3 );
/**
* Adds a notice after the bulk action completed
*/
function press_review_archive_bulk_action_admin_notice() {
if ( ! empty( $_REQUEST['press_review_archive_added'] ) ) {
$items_added = intval( $_REQUEST['press_review_archive_added'] );
printf( '<div id="message" class="updated fade"><p>' .
_n( 'Added %d file to the press review archive.',
'Added %d files to the press review archive.',
$items_added,
'press-review-archive'
) . '</p></div>', $items_added );
}
}
add_action( 'admin_notices', 'press_review_archive_bulk_action_admin_notice' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment