The Internet allows us to chat, exchange, search for news, share experiences, and download documents, gadgets, applications, etc. Besides saving documentation from websites, you can share your documents with users on the WordPress website. Creating a download button using custom fields is not a bad idea - it looks beautiful, professional, and not difficult to create at all.

In this article, we’re going to show you how to create a download button using custom fields with the Meta Box plugin, and then display this button on the front end in two ways: adding code to the theme’s file and using MB Views - an extension from Meta Box.

Here is an example.

the clickable buttons allow you to save an attached file to your device in the content area and on the right sidebar

I put the download buttons in two positions: one in the content area of a single post, and one on the right sidebar. Both of them must be clickable and allow you to save an attached file to your device.

The Applications of the Download Buttons

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

  • Business websites: download profiles, catalogs, decisions, annual reports, ... of the company.
  • Real estate websites: download price lists, project brochures, information about apartments/lands for sale.
  • Travel websites: download trip brochures.
  • Law websites: download law documents and legal forms.
  • Blogs: download documents that bloggers share.

This is an example of how we use the download button on our Meta Box pricing page.

Use the Download button on the product page of Meta Box

The download buttons are really useful, so how can we add them to our site? Let’s see!

Video Version

Before Getting Started

I’m creating a button to download files which are saved in a custom field created with Meta Box. So, we need the following tools:

  • Meta Box core plugin: a framework that helps us create the custom field for the download button. It’s available on wordpress.org.
  • Meta Box Builder: a premium extension of Meta Box that provides you a UI to easily create custom fields right on the back end.

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 Online Generator 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 functions.php file. Get more details on how to use this tool here.

If you prefer adding code directly to the theme file to create the buttons, there is nothing more for this practice. But, if you prefer another more optimal way, you can use MB Views.

Besides, I use the eStar theme and create a child theme. This multipurpose theme is free and very lightweight, you can download it here.

Once you finished installing and activating the plugins, follow these steps:

Create Custom Fields

Go to Meta Box > Custom Fields > Add New to create a new field group.

Go to Meta Box, Custom Fields, Add New to create a new field group

In this practice, I use only one field to save the attached files for the buttons. So, add a field with the File Advanced field type.

add the File Advanced field to save the attached file for the buttons

Move to the Settings tab, choose the Location as Post type, and select the page or the post type as you want to apply the field. For the demonstration purpose, I keep it as the blog posts.

move to the Settings tab, choose the Location as Post type, and select the page or the post type as you want to apply the field

After publishing the field group, in the post editor, you’ll see the created field.

the created field displays in the post editor
Just upload the document that you want to share with readers by clicking the Add Media button.

clicking the Add Media button to upload the document that you want to share with readers

This field will appear below every post editor, so each post will have its own attached file. Our buttons will allow downloading the corresponding file as well.

Still, readers can’t see the download button on the front end, so let’s do it in the next step.

Display the Download Buttons on the Frontend

As I mentioned before, I’m providing two ways to display the buttons for the same result. Both of them are using code:

  • Adding code to the theme’s files (using PHP code): I do not recommend this method since the code may be affected when we change the theme.
  • Using MB Views from Meta Box: with MB Views, we will not type code directly, the plugin will generate it automatically.

I still show both these ways, and then you can experiment yourself to choose which makes sense to you.

Method 1: Use PHP Code

Get URLs of the Attached Files

In the Theme File Editor, add some code to the functions.php file of the child theme:

function estar_child_add_link() {
    $files = rwmb_meta( 'file_download' );
    foreach ( $files as $file ) :
        echo $file['url'];
    endforeach;
}
add_action( 'estar_entry_footer_before', 'estar_child_add_link' );

add some code to the functions.php file

Explanation:

  • 'estar' is the theme that I’m using;
  • rwmb_meta( ): is the function to get data from custom field created with Meta Box;
  • 'file_download' is the ID of the custom field for documents uploading that we’ve created;
  • echo $file['url']: to display the URL of the attached file;
  • add_action( 'estar_entry_footer_before', 'estar_child_add_link' );function estar_child_add_link() { ?> : to specify the location in which you want to display the download button. In this case, I display the download button in the 'estar_entry_footer_before' hook of eStar theme. This hook is created right on the tags list. You can refer to this hook here (line 42).

Now, go to the single post page on the frontend, there will be an URL in the text format before the footer as we set.

go to the single post page on the frontend, there will be an URL in the text format before the footer as we set

The URL is the one from the file that I attached to this post. We’ll turn it to a button later.

Create Shortcode for the Button

To put the download button in different positions, we should customize the code one more time to create a shortcode.

Add one more line:

add_shortcode( 'estar_button_download', 'estar_child_add_link');

create a shortcode to put the download button in different positions

Then, go to any page, any place to embed the shortcode (in this case, I named it as 'estar_button_download'). I’ll put it on the right sidebar.

embed the shortcode any place you want

After that, we had one more URL on the right sidebar.
one more URL on the right sidebar

Style the Button

To make those URLs to be the button format, we should go back to the theme file to stipulate the style of it.

I’ll do it in a simple way with an HTML tag and icon, also named the buttons.

style URLs to be the button format with an HTML tag and icon, also named the buttons

Now, go back to a post on the frontend. The download buttons are ready.

The download buttons are ready on the frontend

Method 2: Use MB Views

If you have the Meta Box AIO, you can use MB Views to get and display the download button easily.

Go to Meta Box > Views to create a template for the buttons.

Go to Meta Box, Views to create a template for the buttons

In the Template tab, click on the Insert Field button.

In the Template tab, click on the Insert Field button

And, find the field you created in the previous step that stores the files.

find the field you created in the previous step to store the file

There will be some options to output the files. I just keep the default option to output the URLs of the files.

some options to output the file

Next, scroll down to the Settings section. Also there are multiple options for the type.

in the Settings section, also there are multiple options for the type

No matter which option you choose here, this view still generates a shortcode for this template, then you can use it anywhere.

So that, to display the button below the content of the post and the sidebar at the same time, I choose to set the Type as Singular, and choose the Location as Post to apply the template to all single posts.

choose to set the Type as Singular, and choose the Location as Post to apply the template to all single posts

In the Render for section, select the position of the download button as you want. I also set it before the footer.

In the Render for section, select the position of the download button as you want

After publishing, you can see the shortcode I mentioned before.

the shortcode displayed automatically after publishing
You should copy it then embed it if you want to put the button on some other place on your website.

On the single post page, the download button is displayed. It’s clickable already.

On the single post page, the download button is displayed. It’s clickable already

I’ll also add the button to the sidebar. Just copy the shortcode and paste it to the wanted position.

copy the shortcode and paste it to the wanted position

And now, we have 2 download buttons on the page.

two download buttons on the page

To rename, as well as style the button, we should go back to the view, and customize the template a little bit more.

style the buttons

This is the new look of the buttons.

the new look of the buttons

Last Words

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 interesting applications that we will seamlessly update in the upcoming tutorials.

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!

Other case studies you might be interested in

  1. Create A Dynamic Landing Page in WordPress Using Custom Field
  2. Create a Filter to Find Hotels by Location
  3. Create an OTA Website Like Booking.com with Meta Box Plugin - P1: Create a Page to Introduce Hotel Rooms
  4. Create an OTA Website Like Booking.com with Meta Box Plugin - P2: Create Filters on the Archive Page
  5. Create an OTA Website Like Booking.com with Meta Box Plugin - P3: Create Filters for Single Hotel Pages
  6. Create Dynamic Favicon in WordPress using Meta Box plugin
  7. Create Posts Series in WordPress Using Meta Box
  8. Display The Latest Products Section - P2 - Using Meta Box and Elementor
  9. Display The Latest Products Section - P3 - Using Meta Box And Oxygen
  10. How to Add Custom Fields for WooCommerce - P2 - Using MB Views
  11. How to Add Custom Fields to Display Banners using Meta Box Plugin
  12. How to Add Guest Authors and Guest Posts - Using Meta Box
  13. How to Add Related Posts - Using Custom Fields
  14. How to Build a Hotel Booking Website Using Meta Box - P1
  15. How to Build a Hotel Booking Website Using Meta Box - P2 - Booking Page in Backend
  16. How to Build a Hotel Booking Website Using Meta Box - P4 - Booking Management Page
  17. How to Build a Hotel Booking Website Using Meta Box – P3 – Booking Page for Customer
  18. How to Create a Classified Ads Website using Meta Box
  19. How to create a FAQs page - P5 - Using Meta Box and Breakdance
  20. How to Create a Product Page - P2 - Using Meta Box and Oxygen
  21. How to Create a Product Page - P3 - Using Meta Box and Bricks
  22. How to Create a Product Page - P4 - Using Meta Box and Elementor
  23. How to Create a Product Page - P5 - Using Meta Box and Gutenberg
  24. How to Create a Product Page - P6 -Using Meta Box and Breakdance
  25. How to Create a Product Page - P7 - Using Meta Box + Kadence
  26. How to Create a Product Page - P8 - Using Meta Box and Brizy
  27. How to Create a Product Page - P9 - Using Meta Box and Divi
  28. How to Create a Product Page using Meta Box Plugin
  29. How to Create a Recipe - P2 - Using Meta Box and Oxygen
  30. How to Create a Recipe - P3 - Using Meta Box and Elementor
  31. How to Create a Recipe - P4 - Using Meta Box and Bricks
  32. How to Create a Recipe - P5 - Using Meta Box and Zion
  33. How to Create a Recipe - P6 - Using Meta Box and Brizy
  34. How to Create a Recipe - P7 - Using Meta Box and Breakdance
  35. How to Create a Recipe - P8 - Using Meta Box and Kadence
  36. How to Create a Recipe - P9 - Using Meta Box and Divi
  37. How to Create a Recipe with Meta Box Plugin
  38. How to Create a Simple Listing - P2 - Using Meta Box and Bricks
  39. How to Create a Simple Listing - P3 - Using Meta Box and Breakdance
  40. How to Create a Simple Listing - P4 - Using Meta Box and Elementor
  41. How to Create a Team Members Page - P1- Using Meta Box and Elementor
  42. How to Create a Team Members Page - P2 - Using Meta Box and Oxygen
  43. How to Create a Team Members Page - P3 - Using Meta Box and Bricks
  44. How to Create a Team Members Page - P4 - Just Meta Box
  45. How to Create a Team Members Page - P6 - using Meta Box and Breakdance
  46. How to Create a Team Members Page - P7 - Using Meta Box and Kadence
  47. How to Create a Video Gallery Page - P2 - Using Meta Box + Bricks
  48. How to Create a Video Gallery Page - P3 - Using Meta Box and Breakdance
  49. How to Create a Video Gallery Page - P4 - Using Meta Box + Elementor
  50. How to Create a Video Gallery Page - P5 - Using MB Views
  51. How to Create a Video Gallery Page - P6 - Using Meta Box and Zion
  52. How to Create a Video Gallery Page Using Meta Box + Oxygen
  53. How to Create ACF Flexible Content Field with Meta Box
  54. How to Create an Auto-Updated Cheat Sheet in WordPress
  55. How to Create an FAQs Page - P1 - Using Meta Box and Elementor
  56. How to create an FAQs page - P2 - Using Meta Box and Oxygen
  57. How to create an FAQs page - P4 - Using Meta Box and Bricks
  58. How to Create an FAQs Page - P6 - Using MB Views
  59. How to Create an FAQs Page - P7 - Using Meta Box and Divi
  60. How to Create an FAQs Page - P8 - Using Meta Box and Kadence
  61. How to Create an FAQs Page -P3- Using Meta Box
  62. How to Create Buttons with Dynamic Link using Custom Fields
  63. How to Create Category Thumbnails & Featured Images Using Custom Fields
  64. How to Create Download and Preview Buttons - P1 - Using Meta Box and Bricks
  65. How to Create Download and Preview Buttons - P2 - Using Meta Box and Oxygen
  66. How to Create Download and Preview Buttons - P3 - Using MB Views
  67. How to Create Download Buttons in WordPress - Using Custom Fields
  68. How to Create Dynamic Landing Page in WordPress - P1 - Using Meta Box and Elementor
  69. How to Create Dynamic Landing Page in WordPress - P2 - Using Meta Box and Bricks
  70. How to Create Menus for Restaurants - P1 - Using Meta Box and Elementor
  71. How to Create Menus for Restaurants - P2- Using Meta Box and Bricks
  72. How to Create Notification Using Custom HTML Field
  73. How to Create Online Admission Form for School or University
  74. How to Create Online Reservation Form for Restaurants using Meta Box
  75. How to Create Relationships - P1 - Using Meta Box and Oxygen
  76. How to Create Relationships - P2 - Using Meta Box and Bricks
  77. How to Create Relationships - P3 - Using MB Views
  78. How to Create Relationships - P4 - Using Meta Box and Breakdance
  79. How to Create Taxonomy Thumbnails & Featured Images - P2 - Using Meta Box and Oxygen
  80. How to Create Taxonomy Thumbnails & Featured Images - P3 - Using Meta Box and Bricks
  81. How to Create Taxonomy Thumbnails & Featured Images - P4 - Using MB Views
  82. How to Create YouTube Video Timestamps on WordPress Website - P1 - Using MB Views
  83. How To Display All Listings On A Map With Meta Box
  84. How to Display Author Bio in WordPress - P1 - Using Meta Box and Bricks
  85. How to Display Images from Cloneable Fields - P1 - with Gutenberg
  86. How to Display Images from Cloneable Fields - P2 - Using Meta Box and Oxygen
  87. How to Display Images from Cloneable Fields - P3 - with Elementor
  88. How to Display Images from Cloneable Fields - P4 - with Bricks
  89. How to Display Opening Hours for Restaurants - P1 - Using Meta Box + Gutenberg
  90. How to Display Opening Hours for Restaurants - P2 - Using Meta Box and Oxygen
  91. How to Display Product Variations - P1 - Using Meta Box and Gutenberg
  92. How to Display Product Variations - P2 - Using Meta Box and Oxygen
  93. How to Display Product Variations - P3 - Using Meta Box and Bricks
  94. How to Display the Dynamic Banners - P2 - Using Meta Box and Bricks
  95. How to Display The Latest Products - P5 - Using Meta Box and Bricks
  96. How to Display the Latest Products - P6 - using Meta Box and Breakdance
  97. How to Display the Latest Products - P7 - Using Meta Box + Kadence
  98. How to Display the Latest Products Section - P4 - Using Meta Box + Zion
  99. How to Display the Most Viewed Posts - P1 - using MB Views

2 thoughts on “How to Create Download Buttons in WordPress - Using Custom Fields

  1. What is the best way to add an email form to this method? Basically, I'd like to be able to capture the viewer's information before sending them to the download link.

    Is it best to create a user role for this and effectively log them in, or is there a simple way to have an email field popup that must be completed before the download is started?

Leave a Reply

Your email address will not be published. Required fields are marked *