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, 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 Meta Box plugin, and then display this button on the front end in two ways: adding code to the theme’s file and using a shortcode. But first, let’s take a quick look at when you should create and use the download buttons!

 

The Applications of 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.
  • 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!

Before Getting Started

To create download buttons by custom fields, we need the following tools:

  • Core plugin Meta Box: a framework that helps us create custom fields, custom meta boxes easily and quickly. It’s free and available on wordpress.org.
  • Meta Box Builder: a premium extension of Meta Box that provides you a UI to 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.

Besides, I use 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:

Step 1: Create the Download Button Using Custom Fields

First, go to Meta Box > Custom Fields > Add New. After that, click the Add Field button, search and choose the File Advanced field type. Next, enter the field ID and Label. Here’s what I do:

Create the Download Button Using Custom Fields

To display custom fields in all posts on the website, go to Settings tab, and choose Posts in the Show for section. Then, choose the Post types as Post (you can choose other post types such as page or a custom post type).

Set up where the custom fields will be display in the settings of Meta Box plugin

And don’t forget to click Publish.

Step 2: Upload the Documents

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:

the custom field that we’ve created displays

Upload the document that you want to share with readers by clicking Add Media. This is the document that I’ve uploaded:

The document is inserted to the custom fields

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.

Step 3: Display the Download Button on the Front End

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 only one position on all pages. Meanwhile, the second method is faster and more convenient if you want to insert the download buttons in different positions on different pages.

Method 1: Display the Download Button by Adding Code to the Theme

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

Display the Download Button in the end of the post content

To do so, add this code to the functions.php file of the child theme:

add_action( 'estar_entry_footer_before', 'estar_child_add_link' );
function estar_child_add_link() {
	?>
	<div class="document_link_download abc">
		<?php
		$files = rwmb_meta( 'file_download' );
		foreach ( $files as $file ) : ?>
			
		<a class="document_link" href="<?php echo $file['url'] ?>" target="_blank">
			<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"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path><polyline points="7 10 12 15 17 10"></polyline><line x1="12" y1="15" x2="12" y2="3"></line></svg>
			<?php esc_html_e( 'Download document', 'estar' ) ?>		
		</a>

		<?php endforeach ?>
	</div>
	<?php
}

Explanation:

  • 'file_download' is the ID of the custom field for documents uploading that we’ve created in step 1.
  • 'estar' is the theme that I’m using.
  • add_action( 'estar_entry_footer_before', 'estar_child_add_link' );function estar_child_add_link() { ?> 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 '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).

And this is the result:

The download button is displayed on the front end of WordPress website

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.

Method 2: Display the Download Button Using a Shortcode

First, add this code to the functions.php to create the shortcode:

add_shortcode( 'estar_button_download', 'estar_button_download');
function estar_button_download() {
	ob_start();
	?>
	<div class="document_link_download abc">
		<?php
		$files = rwmb_meta( 'file_download' );
		foreach ( $files as $file ) : ?>
			
		<a class="document_link" href="<?php echo $file['url'] ?>" target="_blank">
			<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"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path><polyline points="7 10 12 15 17 10"></polyline><line x1="12" y1="15" x2="12" y2="3"></line></svg>
			<?php esc_html_e( 'Download document', 'estar' ) ?>		
		</a>

		<?php endforeach ?>
	</div>
	<?php
	return ob_get_clean();
}

Explanation:

estar_button_download 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.

For example, I insert the [estar_button_download] shortcode in the content of a post as follows:

Display the Download Button Using a Shortcode

Or I can add the shortcode to a widget like this:

Insert the shortcode into a widget in WordPress
Add the shortcode to a widget

Display the download button in the sidebar of WordPress website

 

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

Last Words

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 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 here!

 

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 a User List On the Frontend with Meta Box
  9. Display The Latest Products Section - P2 - Using Meta Box and Elementor
  10. Display The Latest Products Section - P3 - Using Meta Box And Oxygen
  11. Display the Latest Products Section - P4 - Using Meta Box + Zion
  12. Display the Most Viewed Posts - P1 - using MB Views
  13. How to Add Custom Fields to Display Banners using Meta Box Plugin
  14. How to Add Guest Author in WordPress using Meta Box (Part 1)
  15. How to Add Guest Author in WordPress using Meta Box (Part 2)
  16. How to Add Related Posts to WordPress Using Meta Box
  17. How to Build a Hotel Booking Website Using Meta Box - P1
  18. How to Build a Hotel Booking Website Using Meta Box - P2 - Booking Page in Backend
  19. How to Build a Hotel Booking Website Using Meta Box - P4 - Booking Management Page
  20. How to Build a Hotel Booking Website Using Meta Box – P3 – Booking Page for Customer
  21. How to Create a Classified Ads Website using Meta Box
  22. How to Create a Product Page - P2 - Meta Box + Oxygen
  23. How to Create a Product Page - P3 - Using Meta Box + Bricks
  24. How to Create a Product Page - P5 - Using Meta Box and Gutenberg
  25. How to Create a Product Page using Meta Box Plugin
  26. How to Create a Recipe - P2 - Using Meta Box and Oxygen
  27. How to Create a Recipe - P3 - Using Meta Box and Elementor
  28. How to Create a Recipe - P4 - Using Meta Box and Bricks
  29. How to Create a Recipe - P5 - Using Meta Box and Zion
  30. How to Create a Recipe with Meta Box Plugin
  31. How to Create a Simple Listing - P2 - Using Meta Box and Bricks
  32. How to Create a Team Members Page - P1- Using Meta Box and Elementor
  33. How to Create a Team Members Page - P2 - Using Meta Box and Oxygen
  34. How to Create a Team Members Page - P3 - Using Meta Box and Bricks
  35. How to Create a Team Members Page - P4 - Just Meta Box
  36. How to Create ACF Flexible Content Field with Meta Box
  37. How to Create an Auto-Updated Cheat Sheet in WordPress
  38. How to Create an FAQs Page - P1 - Using Meta Box and Elementor
  39. How to create an FAQs page - P2 - Using Meta Box and Oxygen
  40. How to Create an FAQs Page -P3- Using Meta Box
  41. How to Create Buttons with Dynamic Link using Custom Fields
  42. How to Create Category Thumbnails & Featured Images Using Custom Fields
  43. How to Create Download Buttons Using Custom Fields with Meta Box Plugin
  44. How to Create Menu for a Restaurant - P1 - Using Meta Box and Elementor
  45. How to Create Online Admission Form for School or University
  46. How to Create Online Reservation Form for Restaurants using Meta Box
  47. How to Create Taxonomy Thumbnails & Featured Images - P2 - Using Meta Box and Oxygen
  48. How to Display Images from Cloneable Fields - P1 - with Gutenberg
  49. How to Display Images from Cloneable Fields - P2 - with Oxygen
  50. How to Display Images from Cloneable Fields - P3 - with Elementor
  51. How to Display Opening Hours for Restaurants - P1 - Using Meta Box + Gutenberg
  52. How to Display Product Variations - P1 - Using Meta Box and Gutenberg
  53. How to Display Product Variations - P2 - Using Meta Box and Oxygen
  54. How to Display The Latest Products - P5 - Using Meta Box and Bricks
  55. How to Manually Reorder Posts with Meta Box
  56. How to Show Featured Restaurants on Homepage - P1 - Meta Box + Elementor + WP Grid Builder
  57. How to Use Custom HTML Field to Output Beautiful Texts or Output Custom CSS

2 thoughts on “How to Create Download Buttons Using Custom Fields with Meta Box Plugin

  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