How to Create Download Buttons Using Custom Fields with Meta Box Plugin
Tags: wordpress, customfield
Published: 2021-01-19 07:06:47

<!-- wp:paragraph -->

<p>The Internet allows us to chat, exchange, search for news, share experiences, and download documents, gadgets, applications, etc. So if you have a WordPress website and want to share documents with users, <strong>creating a download button using custom fields</strong> is not a bad idea - it looks beautiful, professional, and not difficult to create at all.</p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>In this article, we're going to show you how to <strong>create a download button using custom fields with Meta Box plugin</strong>, and then <strong>display this button on the front end</strong> in two ways: <strong>adding code</strong> <strong>to the theme's file</strong> and <strong>using a shortcode</strong>. But first, let's take a quick look at when you should create and use the download buttons!</p>

<!-- /wp:paragraph -->

<!-- wp:heading -->

<h2>The Applications of Download Buttons</h2>

<!-- /wp:heading -->

<!-- wp:paragraph -->

<p>The download buttons can be used for many situations, but it works best in these kinds of site:</p>

<!-- /wp:paragraph -->

<!-- wp:list -->

<ul>

<li>Business websites: download profiles, catalogs, decisions, annual reports, ... of the company.</li>

<li>Real estate websites: download price lists, project brochures, information about apartments / lands for sale.</li>

<li>Travel websites: download trip brochures.</li>

<li>Law websites: download law documents.</li>

<li>Blogs: download documents that bloggers share.</li>

</ul>

<!-- /wp:list -->

<!-- wp:paragraph -->

<p>This is an example of how we use the download button on our <a href="https://metabox.io/pricing/">Meta Box pricing page</a>.</p>

<p><img class="aligncenter" src="https://i.imgur.com/f8gRytr.png" alt="Use the Download button on the product page of Meta Box" width="1349" height="625" /></p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>The download buttons are really useful, so how can we add them to our site? Let's see!</p>

<!-- /wp:paragraph -->

<!-- wp:heading -->

<h2>Before Getting Started</h2>

<!-- /wp:heading -->

<!-- wp:paragraph -->

<p>To create download buttons by custom fields, we need the following tools:</p>

<!-- /wp:paragraph -->

<!-- wp:list -->

<ul>

<li>Core plugin <a href="https://metabox.io/">Meta Box</a>: a framework that helps us create custom fields, custom meta boxes easily and quickly. It's free and available on <a href="https://wordpress.org/plugins/meta-box/">wordpress.org</a>.</li>

<li><a href="https://metabox.io/plugins/meta-box-builder/">Meta Box Builder</a>: a premium extension of Meta Box that provides you a UI to create custom fields right on the back end.</li>

</ul>

<!-- /wp:list -->

<!-- wp:paragraph -->

<p>If you don't want to use the premium extension Meta Box Builder, you can still manually code to create custom fields. Or you can use the free <a href="https://metabox.io/online-generator/">Online Generator</a> tool of Meta Box to save time and money. This tool comes up with an intuitive UI for creating custom fields. Once you finish creating fields, it will automatically generate code, and then you can add this code to the <code>functions.php</code> file. Get more details on how to use this tool <a href="https://docs.metabox.io/online-generator/">here</a>.</p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>Besides, I use eStar theme and create a child theme. This multipurpose theme is free and very lightweight, you can download it <a href="https://gretathemes.com/wordpress-themes/estar/">here</a>.</p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>Once you finished <a href="https://gretathemes.com/install-wordpress-plugin/">installing and activating the plugins</a>, follow these steps:</p>

<!-- /wp:paragraph -->

<!-- wp:heading -->

<h2>Step 1: Create the Download Button Using Custom Fields</h2>

<!-- /wp:heading -->

<!-- wp:paragraph -->

<p>First, go to <strong>Meta Box</strong> &gt; <strong>Custom Fields</strong> &gt; <strong>Add New</strong>. After that, search and choose the<strong> File Advanced</strong> field type.</p>

<p><img class="aligncenter" src="https://i.imgur.com/zX7H2u2.png" alt="Create the Download Button Using Custom Fields" width="1350" height="625" /></p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>Next, enter the field <strong>ID </strong>and <strong>Label</strong>. Here's what I do:</p>

<p><img class="aligncenter" src="https://i.imgur.com/AQ3em6G.png" alt="Enter the information for the custom fields" width="326" height="412" /></p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>To display custom fields in all posts on the website, go to <strong>Settings </strong>tab, and choose <strong>Posts </strong>in the <strong>Show for</strong> section. Then, choose the <strong>Post types </strong>as <strong>Post </strong>(you can choose other post types such as <strong>page </strong>or a <a href="https://metabox.io/create-wordpress-custom-post-types/">custom post type</a>).</p>

<p><img class="aligncenter" src="https://i.imgur.com/QEAoDOX.png" alt="Set up where the custom fields will be display in the settings of Meta Box plugin" width="746" height="625" /></p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>And don't forget to click <strong>Publish</strong>.</p>

<!-- /wp:paragraph -->

<!-- wp:heading -->

<h2>Step 2: Upload the Documents</h2>

<!-- /wp:heading -->

<!-- wp:paragraph -->

<p>Go to any post type that you want to display the download button. In the WordPress editor, the custom field that we've created displays as follows:</p>

<!-- /wp:paragraph -->

<!-- wp:image -->

<figure class="wp-block-image"><img class="aligncenter" src="https://lh3.googleusercontent.com/Gkhd3lSP9aEUZON2-CvsFdOWOTtNemaTKIEEB7pJjiRoU7MNbGKYKvK4thMPMeD_HjlinejKdfH0oRxGiwFzfsKmPGE6o_wMKgBASqd4_70wSU5Lci3AwtOler25-u7vDTVm6z5e" alt="Upload the Documents with the custom fields" width="1366" height="625" /></figure>

<!-- /wp:image -->

<!-- wp:paragraph -->

<p>Upload the document that you want to share with readers by clicking <strong>Add Media</strong>. This is the document that I've uploaded:</p>

<p><img class="aligncenter" src="https://i.imgur.com/LqUrZBf.png" alt="The document is inserted to the custom fields" width="537" height="273" /></p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>Now we've finished uploading a document. Still, readers can't see the download button on the front end, so let's do it in step 3.</p>

<!-- /wp:paragraph -->

<!-- wp:heading -->

<h2>Step 3: Display the Download Button on the Front End</h2>

<!-- /wp:heading -->

<!-- wp:paragraph -->

<p>This can be done using two methods: adding code to the theme or using a shortcode. The first method is suitable when you just need to display the download button in <strong>only one position on all pages</strong>. Meanwhile, the second method is faster and more convenient if you want to insert the download buttons <strong>in different positions on different pages</strong>.</p>

<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->

<h3>Method 1: Display the Download Button by Adding Code to the Theme</h3>

<!-- /wp:heading -->

<!-- wp:paragraph -->

<p>For example, I want to display the download button at the end of all posts.</p>

<p><img class="aligncenter" src="https://i.imgur.com/TBtasIU.png" alt="Display the Download Button in the end of the post content" width="811" height="624" /></p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>To do so, add this code to the <code>functions.php</code> file of the <a href="https://gretathemes.com/what-is-child-theme/#what-is-child-theme">child theme</a>:</p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<pre>add_action( 'estar_entry_footer_before', 'estar_child_add_link' );

function estar_child_add_link() {

?&gt;

&lt;div class="document_link_download abc"&gt;

&lt;?php

$files = rwmb_meta( 'file_download' );

foreach ( $files as $file ) : ?&gt;

&lt;a class="document_link" href="&lt;?php echo $file['url'] ?&gt;" target="_blank"&gt;

&lt;svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-download"&gt;&lt;path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"&gt;&lt;/path&gt;&lt;polyline points="7 10 12 15 17 10"&gt;&lt;/polyline&gt;&lt;line x1="12" y1="15" x2="12" y2="3"&gt;&lt;/line&gt;&lt;/svg&gt;

&lt;?php esc_html_e( 'Download document', 'estar' ) ?&gt;

&lt;/a&gt;

&lt;?php endforeach ?&gt;

&lt;/div&gt;

&lt;?php

}

</pre>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p><strong>Explanation</strong>:</p>

<!-- /wp:paragraph -->

<!-- wp:list -->

<ul>

<li><code>'file_download'</code> is the ID of the custom field for documents uploading that we've created in step 1.</li>

<li><code>'estar'</code> is the theme that I'm using.</li>

<li><code>add_action( 'estar_entry_footer_before', 'estar_child_add_link' );</code><code>function estar_child_add_link() { ?&gt;</code> is the code to specify the location in which you want to display the download button. In this case, I display the download button in the <code>'estar_entry_footer_before'</code> hook of eStar theme. This hook is created right on the tags list. You can refer to this hook <a href="https://github.com/elightup/estar/blob/master/template-parts/content/post.php">here</a> (line 42).</li>

</ul>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>And this is the result:</p>

<p><img class="aligncenter" src="https://i.imgur.com/Ds8cHTA.png" alt="The download button is displayed on the front end of WordPress website" width="879" height="607" /></p>

<!-- /wp:paragraph -->

<p>Done! We've finished displaying the download button in a fixed position on all pages by adding code to the theme. But if this method doesn't fit your requirements, try the second method below.</p>

<!-- wp:heading {"level":3} -->

<h3>Method 2: Display the Download Button Using a Shortcode</h3>

<p>First, add this code to the <code>functions.php</code> to create the shortcode:</p>

<pre>add_shortcode( 'estar_button_download', 'estar_button_download');

function estar_button_download() {

ob_start();

?&gt;

&lt;div class="document_link_download abc"&gt;

&lt;?php

$files = rwmb_meta( 'file_download' );

foreach ( $files as $file ) : ?&gt;

&lt;a class="document_link" href="&lt;?php echo $file['url'] ?&gt;" target="_blank"&gt;

&lt;svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-download"&gt;&lt;path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"&gt;&lt;/path&gt;&lt;polyline points="7 10 12 15 17 10"&gt;&lt;/polyline&gt;&lt;line x1="12" y1="15" x2="12" y2="3"&gt;&lt;/line&gt;&lt;/svg&gt;

&lt;?php esc_html_e( 'Download document', 'estar' ) ?&gt;

&lt;/a&gt;

&lt;?php endforeach ?&gt;

&lt;/div&gt;

&lt;?php

return ob_get_clean();

}

</pre>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p><strong>Explanation</strong>:</p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p><code>estar_button_download</code> is the shortcode for the download button (you can name it whatever you want). From now on, you just need to insert this shortcode in the desired positions such as posts, pages, widgets.</p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>For example, I insert the <code>[estar_button_download]</code> shortcode in the content of a post as follows:</p>

<p><img class="aligncenter" src="https://i.imgur.com/7sReHYx.png" alt="Display the Download Button Using a Shortcode" width="758" height="505" /></p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>Or I can add the shortcode to a widget like this:</p>

<img src="https://i.imgur.com/9SlVgZ7.png" alt="Insert the shortcode into a widget in WordPress" width="1092" height="625" />

<img src="https://i.imgur.com/3bHDx2D.png" alt="Display the download button in the sidebar of WordPress website" width="809" height="600" />

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>Also, you can display the download button in other positions depending on your needs so that the buttons look eye-catching and appealing.</p>

<!-- /wp:paragraph -->

<!-- wp:heading -->

<h2>Last Words</h2>

<!-- /wp:heading -->

<!-- wp:paragraph -->

<p>What do you think? Using the download buttons is more effective and professional than inserting an ugly long hyperlink, right? In addition to creating the download button using custom fields, Meta Box plugin has many other <a href="https://metabox.io/category/guide/case-study/">interesting applications</a> that we will seamlessly update in the upcoming tutorials.</p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>If you have any questions or ideas about using Meta Box plugin, feel free to share them with us in the comment section or in the Meta Box users group <a href="https://www.facebook.com/groups/metaboxusers">here</a>!</p>