admin管理员组文章数量:1278824
I'm trying to learn if this is possible, and how I might go about implementing it. Our site is a scientific journal, and our published articles follow this permalink structure:
/<article-slug>
i.e.
Every article that we publish has a corresponding PDF
that is available for viewing/download. Those PDFs
are currently stored outside of the WP Media structure.
What I am wondering is if there is a way to have a permalink URL for each article such as this:
/<article-slug>/pdf
i.e.
and that page either list the available attachment or being redirected to the file itself.
The goals of doing this would be to:
- Create an expected URL for the article
PDF
for our readers - Make an easier means by which we can track
PDF
views throughGoogle Analytics
Any ideas of how this might be done would be greatly appreciated.
I'm trying to learn if this is possible, and how I might go about implementing it. Our site is a scientific journal, and our published articles follow this permalink structure:
http://ehponline/<article-slug>
i.e. http://ehponline/1306796
Every article that we publish has a corresponding PDF
that is available for viewing/download. Those PDFs
are currently stored outside of the WP Media structure.
What I am wondering is if there is a way to have a permalink URL for each article such as this:
http://ehponline/<article-slug>/pdf
i.e. http://ehponline/1306796/pdf
and that page either list the available attachment or being redirected to the file itself.
The goals of doing this would be to:
- Create an expected URL for the article
PDF
for our readers - Make an easier means by which we can track
PDF
views throughGoogle Analytics
Any ideas of how this might be done would be greatly appreciated.
Share Improve this question edited Nov 7, 2016 at 5:40 jgraup 9,8843 gold badges32 silver badges69 bronze badges asked Nov 6, 2014 at 13:48 Hal AtkinsHal Atkins 951 silver badge9 bronze badges2 Answers
Reset to default 2Using add_rewrite_endpoint
<?php
if ( ! class_exists( 'PDFRewriteEndpoint' ) ):
class PDFRewriteEndpoint {
/**
* Add actions and filters in constructor.
*/
public function __construct() {
add_action( 'parse_request', array ( $this, 'sniff_requests' ), 0 );
add_action( 'init', array ( $this, 'add_endpoint' ), 0 );
}
/**
* Add rewrite rules.
*/
public function add_endpoint() {
// article-slug/pdf/
add_rewrite_endpoint( 'pdf', EP_PERMALINK | EP_PAGES );
//////////////////////////////////
flush_rewrite_rules( true ); //// <---------- REMOVE THIS WHEN DONE TESTING
//////////////////////////////////
}
/**
* Redirect to PDF page or download
*
* @param $wp_query
*/
public function sniff_requests( $wp_query ) {
if ( isset( $wp_query->query_vars[ 'pdf' ] ) ) {
// anything after `/pdf/` like `extra/stuff`
$pdf_extras = $wp_query->query_vars[ 'pdf' ];
echo $pdf_extras . PHP_EOL;
// Do PDF Logic Here
wp_die('PDF of ' . $wp_query->query_vars['name']);
}
}
}
// Create the class
$pdfRewriteEndpoint = new PDFRewriteEndpoint();
endif; // PDFRewriteEndpoint
Using add_rewrite_rule
<?php
if ( ! class_exists( 'PDFRewrite' ) ):
class PDFRewrite {
const ENDPOINT_QUERY_PARAM = '____pdf_api';
/**
* Add actions and filters in constructor.
*/
public function __construct() {
add_filter( 'query_vars', array ( $this, 'add_query_vars' ), 0 );
add_action( 'parse_request', array ( $this, 'sniff_requests' ), 99 );
add_action( 'init', array ( $this, 'add_endpoint' ), 0 );
}
/**
* Add our custom query arg to later check in `parse_request`.
*
* @param $vars
*
* @return array
*/
public function add_query_vars( $vars ) {
$vars[] = static::ENDPOINT_QUERY_PARAM;
return $vars;
}
/**
* Add rewrite rules.
*/
public function add_endpoint() {
// article-slug/pdf/
add_rewrite_rule( "^([^/]+)/pdf/?$", 'index.php?' . static::ENDPOINT_QUERY_PARAM . '=1&name=$matches[1]', 'top' );
//////////////////////////////////
flush_rewrite_rules( true ); //// <---------- REMOVE THIS WHEN DONE TESTING
//////////////////////////////////
}
/**
* Redirect to PDF page or download
*
* @param $wp_query
*/
public function sniff_requests( $wp_query ) {
global $wp;
if ( isset( $wp->query_vars[ static::ENDPOINT_QUERY_PARAM ] ) ) {
// Do PDF Logic Here
wp_die('PDF of ' . $wp->query_vars['name']);
exit;
}
}
}
// Create the class
$pdfRewrite = new PDFRewrite();
endif; // PDFRewrite
This seems like a suitable use case for rewrite endpoint.
If you create one for pdf
(targeting your posts, etc) you will be able to check for the respective query var in template or before template is loaded and output or redirect accordingly.
Not sure about GA part of it, but if you have an idea about it for this URL structure then endpoint will get you there relatively easily.
本文标签: rewrite rulesPermalink for PDF of article
版权声明:本文标题:rewrite rules - Permalink for PDF of article 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741288676a2370419.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论