Custom Fields
Re-Envisioned
Support General change the name of the pdf file name that is displayed on front end

This topic contains 5 replies, has 2 voices, and was last updated by  Anh Tran 2 months, 3 weeks ago.

  • Creator
    Topic
  • #14213

    Brian
    Participant

    Hi Anh,
    I’m having a hard time understanding the best route to take for what I’m needing. I need to allow the user to upload a pdf and also give that pdf file a specific name to display on the front end. So the end result would need to look like this: https://d.pr/free/i/Rcyngm

    I’m currently using the “file advanced” upload field for the pdf. But when I use the shortcode [rwmb_meta id="monthly_pdf_calendar"] it shows as an unordered bullet point and displays the filename like this: https://d.pr/free/i/7SCqtA

    1. How can I change the filename to something custom? What I really would like is for the user to be able to type in a text field the name of the file and have it display in the output.
    2. How would you go about doing this?

    Lastly slightly related – Since I couldn’t figure out how to best do the above. I tried a different approach. I have a custom visual composer button module that allows me to pull in the link value of a custom meta field. I just add in the custom fields ID and it pulls in the value. But in this case the value it pulled in is “2931”. See screenshot example: https://d.pr/free/i/klZ1MU

    This didn’t work as the value needs to be the full link path to the pdf. How can the full link path be added to the value as opposed to the “2931”. Also what is the “2931” saying?

    Thanks in advance!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Author
    Replies
  • #14230

    Anh Tran
    Keymaster

    Hi Brian,

    Solution 1: adding a text field for file name.

    You can do that by adding a text field into your meta box, or wrap the file_advanced and text field into a group, whatever convenient for you.

    Then outputting the file using code like below (this is for the case when the file and the text field are not in a group):

    $file = rwmb_meta( 'my_file' );
    $file = empty( $file ) ? null : reset( $file ); // Just get the first file.
    $title = rwmb_meta( 'my_file_title' );
    if ( is_array( $file ) ) {
        echo "<a href='{$file['url']}'>{$title}</a>";
    }

    If they’re in a group, then you can do like this:

    $group = rwmb_meta( 'my_file_group' );
    $file_ids = isset( $group['file'] ) ? $group['file'] : null;
    $file_id = empty( $file_ids ) ? null : reset( $file_id );
    $file = RWMB_File_Field::file_info( $file_id );
    $title = isset( $group['title'] ) ? $group['title'] : '';
    if ( is_array( $file ) ) {
        echo "<a href='{$file['url']}'>{$title}</a>";
    }

    Solution 2: using a page builder

    If you use a page builder, like VC or Beaver Builder, you need to fetch the data of the file to use in the plugin. Meta Box stores attachment ID, not file URL.

    A quick solution for that is writing a shortcode to get file URL from the field, like this:

    add_shortcode( 'your_file_url', function( $atts ) {
        $file = rwmb_meta( 'my_file' );
        $file = empty( $file ) ? null : reset( $file );
    
        return empty( $file ) ? '' : wp_get_attachment_url( $file );
    } );

    And use shortcode [your_file_url] in your page builder module to get the file URL.

    #14256

    Brian
    Participant

    Hi Anh,
    Thanks for the detailed reply! Ok slowly learning. For the first two snippets you provided. I’m not using a template file. I’d like to incorporate it into a shortcode in my functions.php file and then just use the shortcode to output those added values? Is that possible / what would that look like?

    For the second solution. This didn’t work for me. I’ll touch base with theme author to see if that field accepts shortcodes.

    But I also tried adding this to the page manually like this to test:

    <a href="[your_file_url]">MY TEXT</a>

    But the link it generated was for the exact same page I was on. Not the pdf link? Not sure what went wrong there.

    Thoughts – Thanks!

    #14268

    Anh Tran
    Keymaster

    Hi Brian,

    Thanks for the detailed reply! Ok slowly learning. For the first two snippets you provided. I’m not using a template file. I’d like to incorporate it into a shortcode in my functions.php file and then just use the shortcode to output those added values? Is that possible / what would that look like?

    Yes, of course. Here is the modified code for the 1st snippet:

    add_shortcode( 'your_shortcode', function( $atts ) {
        $output = '';
        $file = rwmb_meta( 'my_file' );
        $file = empty( $file ) ? null : reset( $file ); // Just get the first file.
        $title = rwmb_meta( 'my_file_title' );
        if ( is_array( $file ) ) {
            $output .= "<a href='{$file['url']}'>{$title}</a>";
        }
        return $output;
    } );

    But the link it generated was for the exact same page I was on. Not the pdf link? Not sure what went wrong there.

    Can you check if the current page has PDF file in the my_file field?

    #14291

    Brian
    Participant

    Hi Anh,
    Thanks for the reply. Ok I’ve changed the meta box into a group. At first the code didn’t work – but I added an “s” to reset( $file_id ); and it seem to work just fine.

    1. Was that correct to do?
    2. add_shortcode( 'group_monthly_calendar', function( $atts ) {
          $output = '';
          $group = rwmb_meta( 'group_cal_pdf' );
          $file_ids = isset( $group['cal_pdf'] ) ? $group['cal_pdf'] : null;
          $file_id = empty( $file_ids ) ? null : reset( $file_ids );
          $file = RWMB_File_Field::file_info( $file_id );
          $title = isset( $group['cal_title'] ) ? $group['cal_title'] : '';
          if ( is_array( $file ) ) {
              $output .= "<a href='{$file['url']}' target='_blank'>{$title}</a>";
          }
          return $output;
      } );
    3. How do you use Meta Box shortcodes to output group fields? For standard non grouped fields I’ve been doing this [rwmb_meta id="field_id"] and it works. What do you do for grouped field values? Also your shortcode doc could really use an update / give a few more working examples.
    4. “Can you check if the current page has PDF file in the my_file field?”
      Yes it was in the field. But now since I changed it to be in a group I don’t know what the ID would be to test again.
    5. Are you having issues saving settings in the Group Field in Meta Box Builder?
      Here is a video showing the “default state” radio and “group title text” radio button not saving. I’ve tried multiple times. https://cl.ly/1e38a0f89071
    6. File Advanced showing “max number of files” even though I did not select the “show status” check box.
    7. If you remove the “Label” for the Group field – but then you add a “Group title text” it seems like that should then show up where the normal “Label” field showed. Currently all my “group” fields are blank. See the above video link.

    Thanks in advance!

    #14311

    Anh Tran
    Keymaster

    Hi Brian,

    1. Was that correct to do?

    Yes! Sorry, my mistake. I missed the “s”.

    1. How do you use Meta Box shortcodes to output group fields?

    The shortcode only outputs list of data of sub-fields in a group. I’d not recommend using it for groups. Instead, write your own shortcode, like what you did with group_monthly_calendar shortcode.

    1. Yes it was in the field. But now since I changed it to be in a group I don’t know what the ID would be to test again.

    As you need to get the PDF file URL, try modifying your shortcode to this:

    add_shortcode( 'group_monthly_calendar_pdf_url', function( $atts ) {
        $output = '';
        $group = rwmb_meta( 'group_cal_pdf' );
        $file_ids = isset( $group['cal_pdf'] ) ? $group['cal_pdf'] : null;
        $file_id = empty( $file_ids ) ? null : reset( $file_ids );
        $file = RWMB_File_Field::file_info( $file_id );
        if ( is_array( $file ) ) {
            return $file['url'];
        }
        return '';
    } );
    1. Are you having issues saving settings in the Group Field in Meta Box Builder?

    I see. Thanks a lot for your video. I’ll check and fix it.

    1. File Advanced showing “max number of files” even though I did not select the “show status” check box.

    Thanks. Let me check and fix it.

    1. If you remove the “Label” for the Group field – but then you add a “Group title text” it seems like that should then show up where the normal “Label” field showed. Currently all my “group” fields are blank. See the above video link.

    I see. Thanks a lot for your video. I’ll check and fix it.

Viewing 5 replies - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.