<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: tacqiya</title>
    <description>The latest articles on DEV Community by tacqiya (@tacqiya).</description>
    <link>https://dev.to/tacqiya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1143466%2F23d23891-300e-44f1-afaf-64def6982397.jpg</url>
      <title>DEV Community: tacqiya</title>
      <link>https://dev.to/tacqiya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tacqiya"/>
    <language>en</language>
    <item>
      <title>Code to auto-download files from WordPress?</title>
      <dc:creator>tacqiya</dc:creator>
      <pubDate>Fri, 01 Sep 2023 05:41:14 +0000</pubDate>
      <link>https://dev.to/tacqiya/code-to-auto-download-files-from-wordpress-ee2</link>
      <guid>https://dev.to/tacqiya/code-to-auto-download-files-from-wordpress-ee2</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yft1GR4J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02hfcba8rf0kfw7c44wz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yft1GR4J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02hfcba8rf0kfw7c44wz.png" alt="Image description" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because of server issues &amp;amp; browser blocking auto-download, here’s rather an effective method for auto-downloading any file in WordPress.&lt;br&gt;
Create a function in functions.php of your theme folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function file_download()
    {
        if (isset($_GET['file'])) {
            $file = base64_decode($_GET['file']);
            $file_name = basename($file); //sanitize_file_name($file);
            $file_path = $_SERVER['DOCUMENT_ROOT'] . parse_url($file, PHP_URL_PATH);
            if (file_exists($file_path)) {
                header("Content-Disposition: attachment; filename=$file_name");
                readfile($file_path);
                exit;
            } else {
                echo WP_CONTENT_DIR . parse_url($file, PHP_URL_PATH);
            }
        }
    }

    add_action('init', 'file_download');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are checking here whether the file exists or not,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (file_exists($file_path))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the else part used for getting the file path&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo WP_CONTENT_DIR.parse_url($file, PHP_URL_PATH);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use &lt;strong&gt;“File not found”&lt;/strong&gt; for showing an error display.&lt;br&gt;
Here we’re passing a get method here to get the dynamic file the user wants to download.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://example-wp.com/?file=file-url
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>wordpress</category>
      <category>web</category>
      <category>developer</category>
      <category>php</category>
    </item>
  </channel>
</rss>
