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!

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 *