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:

  1. Create an expected URL for the article PDF for our readers
  2. Make an easier means by which we can track PDF views through Google 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:

  1. Create an expected URL for the article PDF for our readers
  2. Make an easier means by which we can track PDF views through Google 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 badges
Add a comment  | 

2 Answers 2

Reset to default 2

Using 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