anttiviljami / wp-pdf-templates

Add PDF templates to your WordPress theme
https://wordpress.org/plugins/wp-pdf-templates
GNU General Public License v3.0
42 stars 18 forks source link

Add filters to modify filename and cache id #27

Closed tnottu closed 7 years ago

tnottu commented 7 years ago

I had a situation where the product names (and slugs) were identical across different languages and as a result the PDFs were cached and served in wrong translations. Also, it became necessary to customize the filename more specifically, adding info from post metadata etc.

This PR adds two filters to customize the pdf filename: the actual name and the cache id which is used to determine if the file should be generated.

Example use:

add_filter('pdf_template_filename', function( $filename ) {

    $type_translations = [
        'fi' => 'tuotekortti',
        'en' => 'product-data',
        'de' => 'produktblad',
        'sv' => 'produktkarte'
    ];

    $type = isset( $type_translations[ pll_current_language() ] ) ? $type_translations[ pll_current_language() ] : null;

    if ( $type ) {
        $title = sanitize_file_name( get_the_title() );
        $version = get_field('product__version');
        $filename = "{$type}-{$title}-{$version}";
    }

    return $filename;

}, 10, 2);
add_filter('pdf_template_cache_id', function( $cache_id ) {
    return get_the_modified_time('U'); // use unix timestamp as cache id
}, 10, 2);