Add Meta Box to Media Upload Section Wordpress

Read Time: 8 mins Languages:

Over the course of the next ii posts, nosotros'll take a look at how we tin can leverage the WordPress API to define our own custom meta boxes for attaching a document (such as a PDF) to our WordPress pages. We'll also accept a look at how to properly remove files should we choose to delete them from our posts.

A few months ago, Christopher Davis demonstrated how to create Custom WordPress Write/Meta Boxes (https://wp.tutsplus.com/tutorials/plugins/how-to-create-custom-wordpress-writemeta-boxes/). As mentioned in his article, custom meta boxes are incredibly powerful features that permit usa to add various pieces of additional information to our WordPress posts and pages.

Of class, nosotros're non limited to text or options such as radio buttons or checkboxes. Nosotros can also attach images and files to posts, pages, and other post types by taking reward of custom meta boxes; however, dealing with file types requires a chip more than lawmaking to properly handle uploads and deletions of the file.

Before getting started, it'south important to note that this series assumes that we're working with the Twentyeleven Theme. Although the code volition piece of work with any WordPress Theme, this ensures that we're all working with the same codebase and should make it easier to follow along with the tutorial.


Defining The Custom Meta Box

Get-go, let'due south create the post meta box. Specifically, the meta box should…

  • Be available on both posts and pages
  • Appear beside the post editor
  • Accept an input file

At this betoken, locate functions.php in the root of the Twentyeleven Theme directory. Nosotros'll exist making all of our changes to the lesser of the file. We'll start past defining a role called add_custom_meta_boxes and information technology will exist registered with the add_meta_boxes claw.

Next, let'south define our custom meta box. Showtime, we'll write the code after which I'll explain what the code is doing:

Notice that the following two calls to add_meta_box are well-nigh identical.

  • The first parameter is the ID of the meta box. This is used when saving the value.
  • The second parameter is the label. This value appears in the caption in a higher place the post meta box
  • The third value is the callback role that is used to really define the markup that appears in the meta box. We'll get to this momentarily.
  • The fourth value tells WordPress the mail type on which this custom meta box should appear. Since we want it on both posts and pages, we've defined it twice.
  • The last parameter defines where we desire the meta box to appear. It tin can exist either side, advanced, or normal. Nosotros've select side so that it appears abreast the mail editor

Setting Upwardly The Callback

At this indicate, the custom meta box doesn't do anything. In fact, it doesn't fifty-fifty display anything:

This is because we haven't divers the callback role that is used generate the markup for the meta box. In social club to practice that, nosotros need to define the office with the proper name that we listed it higher up. Specifically: 'wp_custom_attachment.'

The input should accept a PDF and so nosotros'll give a short clarification and the proper input element for accepting files:

The first line of the lawmaking defines a nonce value in society to properly validate and secure our upload.

Next, we're simply setting upwardly the markup for displaying the input field.

At this betoken, we've got a custom meta box that looks decent but doesn't really work.


Saving The File

Now nosotros're prepare to save the file. Kickoff, we need to create a function that hooks into the save_post hook. Let'south define that now:

Although this function doesn't actually save the value - not yet, at least - it includes a flake of lawmaking that ensures we're set to salvage the file. Specifically, the function makes certain that the expected nonce value is present, that an automatic save is non occurring, and that the user attempting to save data has permissions to do and so.

Now we're fix to begin validating and saving the file. When it comes to saving file-based custom meta data, additional lawmaking has to exist introduced to properly handle indiosynchrocies of uploading files. First, we'll define the lawmaking then we'll explain information technology. The function should look like this:

The new block of code does several things. Comments accept been provided in order to requite clarity, but hither's what's happening:

  • Starting time, we make sure the file array isn't empty
  • Next, we setup an array for the supported file types and verify that the uploaded file is of that blazon
  • Next, we use wp_upload_bits to copy the file to the server
  • Finally, if there are whatever errors, we'll halt execution and display them to the user.

A note on wp_upload_bits(http://codex.wordpress.org/Function_Reference/wp_upload_bits). This office is an alternative to wp_handle_upload(http://codex.wordpress.org/Function_Reference/wp_handle_upload). In my experience, wp_handle_upload has yielded some problems with certain server configurations such that it gives a false negative. Past that, I hateful that information technology claims that information technology has a problem uploading the file when, in reality, the file was actually uploaded.


Linking Upwards the File

At this point, we should be ready to provide a link to the file on our posts and our pages. In the Twentyeleven Theme directory, locate the two files single.php and page.php. Each file contains a line of lawmaking that looks similar this:

Only below that line, nosotros'll need to request the custom post meta data by doing this:

Specifically, this function is requesting the postal service meta data identified by 'wp_custom_attachment' associated with this post ID. Clear, right? The final parameter is telling WordPress that we want the result back in a string format (the alternative is in the format of an assortment and that's across the telescopic of this tutorial).

The concluding block of code should wait like this:

Now, attempt to upload a file for a folio or post. Load up the post and folio in your browser. Permitting everything has gone correctly, yous'll detect that ... oops ... you don't actually have the path to a file.


Update The Post Class

The problem is that, by default, the form element used to save all of the post information and its associated data doesn't have file types. This can be fixed with adding one more than function that hooks into the WordPress folio life cycle:

This will suspend the enctype attribute to the postal service editor form element so that file uploads will non exist supported.

Now, allow'southward try to upload a file a over again. Locate your post or folio with the custom postal service meta box and attempt to upload a PDF. If all goes well, yous should exist able to navigate to the mail and/or page and come across the URL to the file.


Linking It Upwardly

The last footstep is the easiest. Next up, revisit the single.php and page.php files and wrap the call to the custom meta information request in an anchor such that information technology looks like this:

At this indicate, you should be able to attach a custom PDF to your page and have a link appear at the lesser of the folio content providing a link to the download. In the adjacent post, we'll take a look at how we tin can provide some improve styling for the download anchor also as delete the file from the page using the WordPress API.

In the meantime, try experimenting with customizing the meta box even further. For case, attempt to include a custom label to provide content for the link.

Did yous find this mail service useful?

milleraftecte75.blogspot.com

Source: https://code.tutsplus.com/articles/attaching-files-to-your-posts-using-wordpress-custom-meta-boxes-part-1--wp-22291

0 Response to "Add Meta Box to Media Upload Section Wordpress"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel