Support » Plugin: Document Gallery » Insert “View” and “Download” Options

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello??

    Plugin Author demur

    (@demur)

    Hi David!

    I understand Your anxiety.
    Unfortunately if You want something besides “Yes that’s possible.” You have to wait for us to find some sufficient _free_ time (as this activity doesn’t feed us and we have to work) to get proper solution and test it so we would provide answer that could fit majority cases.
    If You need it desperately and ready to pay some money You can write a few words to Dan through Contact form.

    Thank You for Your patience.

    Thanks for getting back to me.

    Plugin Author demur

    (@demur)

    Hi David!

    For now I see two solutions. One with less code based on HTML5 features, another (as You’ve mentioned) – on HTTP header approach, with more code. BTW they can be combined 😉

    1. HTML5 anchor has attribute download, which now is supported by majority of web browsers.
    To have this solution working You need to add the given below code to functions.php file of Your active WordPress theme:

    /** Modify DG icons (pdf ones) to show "VIEW" and "DOWNLOAD" links alongside title. */
    function dg_icon_template( $icon, $use_descriptions, $id ) {
    	if ( get_post_mime_type( $id ) == 'application/pdf' ) {
    		$file_url = wp_get_attachment_url( $id );
    		$doc_icon =
    			'   <div class="document-icon">' . PHP_EOL .
    			'      <a href="%link%" target="%target%">' . PHP_EOL .
    			'         <img src="%img%" title="%title_attribute%" alt="%title_attribute%" %data%/>' . PHP_EOL .
    			'         <span class="title">%title%</span>' . PHP_EOL .
    			'      </a>' . PHP_EOL .
    			'      <a href="' . $file_url . '" target="%target%">VIEW</a>' . PHP_EOL .
    			'      <a href="' . $file_url . '" target="%target%" download="' . basename( $file_url ) . '">DOWNLOAD</a>' . PHP_EOL .
    			'   </div>' . PHP_EOL .
    			($use_descriptions ? '   <p>%description%</p>' : '');
    		return $doc_icon;
    	}
    	return $icon;
    }
    add_filter( 'dg_icon_template', 'dg_icon_template', 10, 3 );

    As You’ve requested additional links only for PDFs, the condition was introduced that checks if an item is PDF file and only for such elements modifies the output.

    2. To be able to produce different values of the Content-Disposition header for the same attachment we need to set and track different URLs. For this purpose I suggest utilizing REST API v 2.0 (enabled in WordPress since 4.4).
    Same thing: to make use of this solution You need to add given the below code to functions.php:

    /** Modify DG icons (pdf ones) to show "VIEW" and "DOWNLOAD" links alongside title. */
    function dg_icon_template( $icon, $use_descriptions, $id ) {
    	if ( get_post_mime_type( $id ) == 'application/pdf' ) {
    		$filename = basename( get_attached_file( $id ) );
    		$doc_icon =
    			'   <div class="document-icon">' . PHP_EOL .
    			'      <a href="%link%" target="%target%">' . PHP_EOL .
    			'         <img src="%img%" title="%title_attribute%" alt="%title_attribute%" %data%/>' . PHP_EOL .
    			'         <span class="title">%title%</span>' . PHP_EOL .
    			'      </a>' . PHP_EOL .
    			'      <a href="/wp-json/dg/v1/view/'.$id.'/'.$filename.'" target="%target%">VIEW</a>' . PHP_EOL .
    			'      <a href="/wp-json/dg/v1/download/'.$id.'/'.$filename.'" target="%target%">DOWNLOAD</a>' . PHP_EOL .
    			'   </div>' . PHP_EOL .
    			($use_descriptions ? '   <p>%description%</p>' : '');
    		return $doc_icon;
    	}
    	return $icon;
    }
    add_filter( 'dg_icon_template', 'dg_icon_template', 10, 3 );
    
    add_action( 'rest_api_init', function () {
    	register_rest_route( 'dg/v1', '/(?P<action>view|download)/(?P<id>\d+)/.+', array(
    		'methods' => WP_REST_Server::READABLE,
    		'callback' => 'dg_process_link',
    		'args' => array(
    			'id' => array(
    				'validate_callback' => 'is_numeric',
    				'required' => true,
    			),
    			'action' => array(
    				'required' => true,
    			),
    		),
    	) );
    } );
    
    /**
     * Processing PDF file request from Document Gallery and responding in appropriate way
     *
     * @param array $data Options for the function.
     * @return WP_Error if non-existing or non-pdf attachment was requested, otherwise the requested attachment would be outputted.
     */
    function dg_process_link( $data ) {
    	if ( in_array( $data['id'], array_keys( DG_Thumb::getThumbs() ) ) && get_post_mime_type( $data['id'] ) == 'application/pdf' ) {
    		$file = get_attached_file( $data['id'] );
    		if ( file_exists( $file ) ) {
    			header( 'Content-Type: application/pdf' );
    			header( 'Content-Disposition: ' . ( $data['action']=='view' ? 'inline' : 'attachment' ) . '; filename="' . basename( $file ) . '"' );
    			header( 'Content-Length: ' . filesize( $file ) );
    			readfile( $file );
    			exit;
    		}
    	}
    	return new WP_Error( 'invalid_link', 'Invalid link :-(', array( 'status' => 404 ) );
    }

    I hope this would work for You.
    Please don’t hesitate to let us know if You run into any further issues.

    PS: If You’ve found our answers and/or the plugin useful, please rate the efforts. Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Insert “View” and “Download” Options’ is closed to new replies.