Home › Forums › Front-end Issues › Frontend PDF embed
i want when a user uploads a pdf from frontend, it automatically gets embedded in the post. i am using following code for that but didn’t get the result.
// Handle form submission
function handle_pdf_submission() {
// Check if form was submitted
if (isset($_POST['submit_pdf'])) {
// Get PDF file from form
$pdf_file = $_FILES['pdf_file'];
// Upload PDF file to server
$upload = wp_upload_bits($pdf_file['name'], null, file_get_contents($pdf_file['tmp_name']));
$pdf_url = $upload['url'];
// Embed PDF file
$pdf_embed = '[pdf-embedder url="' . $pdf_url . '"]';
// Create new post with embedded PDF file
$post = array(
'post_title' => 'New PDF Post',
'post_content' => $pdf_embed,
'post_status' => 'publish',
'post_type' => 'PDFs',
);
$post_id = wp_insert_post($post);
// Redirect to success page
wp_redirect('/pdf-submission-success/');
exit;
}
}
// Display form on frontend
function display_pdf_submission_form() {
?>
<form method="post" enctype="multipart/form-data">
<label for="pdf_file">Upload PDF:</label>
<input type="file" id="pdf_file" name="pdf_file">
<br><br>
<input type="submit" name="submit_pdf" value="Submit PDF">
</form>
<?php
}
// Shortcode to display form on a page
function pdf_submission_form_shortcode() {
ob_start();
display_pdf_submission_form();
return ob_get_clean();
}
add_shortcode('pdf_submission_form', 'pdf_submission_form_shortcode');
// Add form action to current page URL
function add_pdf_submission_form_action($form) {
$form_action = get_permalink();
return str_replace('<form', '<form action="https://adify.in/project/aamarsahitya/submit-pdf/' . $form_action . '"', $form);
}
add_filter('pdf_submission_form_shortcode', 'add_pdf_submission_form_action');
// Handle form submission on init
add_action('init', 'handle_pdf_submission');
// Add filter to automatically embed PDF file
function embed_pdf_file($content) {
if (is_singular('PDFs')) {
$attachments = get_posts(array(
'post_type' => 'attachment',
'post_mime_type' => 'application/pdf',
'post_parent' => get_the_ID(),
));
if ($attachments) {
foreach ($attachments as $attachment) {
$pdf_embed = '[pdf-embedder url="' . wp_get_attachment_url($attachment->ID) . '"]';
$content = str_replace('[pdf-embedder]', $pdf_embed, $content);
}
}
}
return $content;
}
add_filter('the_content', 'embed_pdf_file');
Kindly help.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.