How to add a PDF upload field in AnsPress question form?

Since AnsPress 4.1.0, you can easily add new fields to existing form. For more detail about Form and field, checkĀ New form and validation classes introduced in AnsPress 4.1.0.

Below snippet shows how to add a new field in question form for allowing users to upload a PDF file. Notice allowed_mimes in field, you can change it to any mimes type as per your requirement.

Loading Gist...

AnsPress does not save field or upload files by itself but it provide helper functions to make it easier. we just need to hook after a question is processed and add a post meta for later use:

Loading Gist...

Now, pdf url can be fetched anywhere using wp_get_attachment_url( get_post_meta( $post_id, 'my_custom_pdf', true ) ).

5 Comments

  1. Hello!
    Thanks for this info but sorry – i dont understand where i can add this code?

    Reply
    1. Hello,
      You need to add this codes to your theme’s functions.php

    1. I added this two codes to my theme functions.php file and now i have a “AnsPress Input Field” with input field and nothing else.
      I think someting is missing or i need to make something more.
      Where i need to add this code: wp_get_attachment_url( get_post_meta( $post_id, ‘my_custom_pdf’, true ) )

      Regards,
      H.

  2. I added this code into theme function.php file.

    the field are shown at the page, but when we save the file from this function >

    function my_save_pdf_uploads( $post_id ) {

    from where we will get the post_id, I mean obviously we need to upload file by creating post_meta but can you please provide me complete code of it because i am new in wordpress.

    Thank you

    Regards
    Aarav Shrimali

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment