Support

Account

Home Forums Front-end Issues Frontend PDF embed

Unread

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.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.