WordPress allows us to easily generate PDF files and make them available for download. In this blog, I’ll explain how to simply create and open the PDF files with CPT and ACF in the WordPress site.
Create ACF Field
Advanced Custom Fields is a WordPress plugin which allows us to add extra content fields to our WordPress. Please follow the steps below.
• First download, Advance Custom Fields plugins.
• Next, go to the plugins option from the dashboard menu > Activate the plugins.
• From the Custom Fields admin screen, click the Add New button to create a new field group.
• Enter the field name > Click Add Field option.
• Type the Field Label such as ‘PDF’.
• Select File option from Field Type.
• Choose Location, where my field will be generated > Now Publish the ACF.
Create CPT UI Field
We can create our own custom post types and call them whatever we want. An amazing thing about this post type is that it can have different custom fields. Also, its own custom category structure. Plugins use is one of the easiest ways of creating a custom post type. Please follow the steps below.
• At first, we need to do install and activate the Custom Post Type UI plugin.
• After activation, the plugin will add a new menu item in our WordPress admin menu called CPT UI.
• Now go to CPT UI > Add New to create a new custom post type, named ‘Download’ and Post Type Slag named, ‘download-pdf’.
• Now add a suitable icon from Dashicons ( https://developer.wordpress.org/resource/dashicons/ ) for ‘Download’ CPT.
• Check only Title option from Supports field.
• After creating CPT, go to ‘Download’’ page in dashboard > Click ‘Add new’ option.
• Enter title. Next, click the Add File option and choose the pdf file. > Next Publish.
Now, go to function.php. In the filefunctions.php, write the following code to create a custom post type –
function get_download_pdf() { $args = array( 'post_type'=> 'download_pdf', 'post_per_page' => 20, 'order' => 'ASC' ); $the_query = new WP_Query( $args ); echo '<div class="row">'; while ($the_query>have_posts()) : $the_query->the_post(); echo '<div class="col-lg-12 col-md-12 col-sm-12 col-12">'; echo '<div class="pdf-download-field">'; echo '<h6>'.get_the_title().'</h6>'; $pdf = get_field( "pdf" ); echo '<a href="'.$pdf.'" target="_blank"> <img src="'.get_stylesheet_directory_uri().'img-location" alt="pdf"></a>'; echo '<a href="'.$pdf.'" target="_blank"> <i class="fa fa-download"></i> Download</a>'; echo '</div>'; echo '</div>'; endwhile; echo '</div>'; } add_shortcode( 'download-pdf', 'get_download_pdf' );
Now go to the page in the dashboard where it will generate the pdf file and put the shortcode [download-pdf ] there.
/* CSS */
Add some custom style here.
This is the simplest way to create a PDF file in WordPress.
Front-end Developer and Technology Enthusiast at Openweb Solutions