Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi there,

    This is an old post but I found it when I was too looking for a way to get shortcodes to work for file URLs, so I thought I would follow up with what i have learned.

    I had created a shortcode that was able to pull a temp url via Rackspace’s Cloud API and wanted to use it as the File URL for my WooCommerce products. The shortcode worked everywhere except as the path to the file in WooCommerce.

    The only way I could get it to work was to install the Amazon S3 Storage plugin. This allowed my shortcode to work. But to me this was crazy. Installing a plugin just to get a shortcode to work? Plus the files were not even on Amazon S3.

    So…. after much digging, research, and plan ole fashioned thinking I came up with a solution.

    You need to use the woocommerce_file_download_path filter hook to hook into a function that then runs the do_shortcode to get the URL.

    Here is what I used.

    ## The WooCommerce hook to call the download file path ##
    add_filter( 'woocommerce_file_download_path', 'rackspace_download', 1, 3 );
    
    ## The function that will run the shortcode to the temp. URL ##
    function rackspace_download( $file_path, $product_id, $download_id ) {
    	return do_shortcode( $file_path );
    }

    Here the woocommerce_file_download_path hook runs my function rackspace_download where I pass the arguments $file_path, $product_id, $download_id (not sure if I needed to pass them all, but did anyway even though I will only use the $file_path). The function then runs the shortcode (which is found in the $file_path variable).

    I assume that you are putting the short code in the File URL field of the product. If so then this should work.

    Cheers

    Thread Starter GusRuss89

    (@gusruss89)

    Thanks @streamworksaudio, I did figure this out eventually by digging through WooCommerce’s source.

    My shortcode also needed access to the product sku, so I had to register that as a global in woocommerce_product_file_download_path

    add_filter( 'woocommerce_product_file_download_path', 'my_parse_download_link_shortcodes', 10, 3 );
    function my_parse_download_link_shortcodes( $file_path, $product, $download_id ) {
    
    	global $product_slug;
    	$product_slug = $product->get_sku();
    
    	return $file_path;
    }

    Then allow shortcodes in the file download path.

    add_filter( 'woocommerce_product_file_download_path', 'do_shortcode', 11 );

    This allowed me to just use a standardised shortcode [product-download-link] for every product.

    Great idea! Glad we both found a way to make it work ??

    I was thinking of doing the same thing, using the SKU or Product ID for the file name – but I don’t think Rackspace allows changing the name of the file to be downloaded, so I have to pass the object name (the file name) for each product.

    Cheers

    Hi,
    I am new to woocommerce, doesn’t have much idea about programming. Was wondering for a week how to secure download links.. Looked around a lot of stuff but nothing seems promising until this topic. You guys have made it.. Can you tell me from the step 1. Coz am not able to figure out how to create shortcode for url.

    Waiting for your reply.
    Thnaks in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How To Use Shortcode as Product Download Link’ is closed to new replies.