-
-
Save 2ndkauboy/abf6df73fd9281ecb0d23166fcc1c22f 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment