0

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.org/<article-slug>
i.e. http://ehponline.org/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.org/<article-slug>/pdf
i.e. http://ehponline.org/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.

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
0

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.

  • That's an interesting API. I'll muck around with that on my development server. I'm also wondering if I should try and create a custom post type for PDF files that is a child of the article post type. – Hal Atkins Nov 6 '14 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.