Custom Fields
Re-Envisioned
Support Meta Box Group Trouble getting file url of file attachment in cloneable group

  • Creator
    Topic
  • #18563
    Resolved sgtscience
    Participant

    Hi I am trying to make an attached files drop down button for WooCommerce Products. Everything is working fine but I am having trouble getting the file url to place in a link. Here are my registered fields:

    add_filter( 'rwmb_meta_boxes', 'prefix_register_file_attachments' );
    function prefix_register_file_attachments( $meta_boxes ) {
        $meta_boxes[] = array(
            'id' => 'File_Attachments',
            'title' => esc_html__( 'File Attachments' ),
            'post_types' => array( 'product' ),
            'context' => 'advanced',
            'priority' => 'default',
            'autosave' => 'true',
            'fields' => array(
                array(
                    'id' => 'file_attachments',
                    'type' => 'group',
                    'group_title' => 'Attachment {#}',
                    'collapsible' => true,
                    'save_state' => true,
                    'clone'  => true,
                    'sort_clone' => true,
                    'fields' => array(
                        array(
                            'id'   => 'file_name',
                            'type' => 'text',
                            'name' => 'File Name',
                        ),
                        array(
                            'id' => 'attachment',
                            'type' => 'file_advanced',
                            'name' => esc_html__( 'Attach File' ),
                            'desc' => esc_html__( 'Upload your pdf files to show on single product page' ),
                            'force_delete' => false,
                        ),
                    ),
                ),
            ),
        );
        return $meta_boxes;
    }

    These work fine in the backend. Here is my code to display the results:

    add_action( 'woocommerce_after_add_to_cart_button', 'product_download_button', 5 );
    function product_download_button() { ?>
        <div class="dropdown">
            <button class="product-downloads-button" onclick="return false;">DOWNLOADS
                <i class="fa fa-caret-down"></i>
            </button>
            <div class="dropdown-content"> <?php
                $file_attachments = rwmb_meta( 'file_attachments' );
                if ( ! empty( $file_attachments ) ) {
                    foreach ( $file_attachments as $file_attachment ) {
                        $file_name = isset( $file_attachment['file_name'] ) ? $file_attachment['file_name'] : '';
                        $file_info = get_attached_file( $file_attachment['attachment'] );
                        echo '<a href="' . $file_info['url'] . '" target="_blank" rel="noopener noreferrer">' . $file_name . '</a>';
                    } 
                } ?>
            </div>
        </div>
    <?php }

    The $file_name returns just fine but when I turned on WP_DEBUG it returns:

    Notice: Undefined index: attachment
    

    for this line:

    $file_info = get_attached_file( $file_attachment['attachment'] );
    

    Could you please advise?

Viewing 10 replies - 1 through 10 (of 21 total)
  • Author
    Replies
  • #18564
    sgtscience
    Participant

    sorry here is the code:

    add_filter( 'rwmb_meta_boxes', 'prefix_register_file_attachments' );
    function prefix_register_file_attachments( $meta_boxes ) {
        $meta_boxes[] = array(
            'id' => 'File_Attachments',
            'title' => esc_html__( 'File Attachments' ),
            'post_types' => array( 'product' ),
            'context' => 'advanced',
            'priority' => 'default',
            'autosave' => 'true',
            'fields' => array(
                array(
                    'id' => 'file_attachments',
                    'type' => 'group',
                    'group_title' => 'Attachment {#}',
                    'collapsible' => true,
                    'save_state' => true,
                    'clone'  => true,
                    'sort_clone' => true,
                    'fields' => array(
                        array(
                            'id'   => 'file_name',
                            'type' => 'text',
                            'name' => 'File Name',
                        ),
                        array(
                            'id' => 'attachment',
                            'type' => 'file_advanced',
                            'name' => esc_html__( 'Attach File' ),
                            'desc' => esc_html__( 'Upload your pdf files to show on single product page' ),
                            'force_delete' => false,
                        ),
                    ),
                ),
            ),
        );
        return $meta_boxes;
    }
    add_action( 'woocommerce_after_add_to_cart_button', 'product_download_button', 5 );
    function product_download_button() { ?>
        <div class="dropdown">
            <button class="product-downloads-button nectar-button large regular accent-color regular-button" onclick="return false;">DOWNLOADS
                <i class="fa fa-caret-down"></i>
            </button>
            <div class="dropdown-content"> <?php
                $file_attachments = rwmb_meta( 'file_attachments' );
                if ( ! empty( $file_attachments ) ) {
                    foreach ( $file_attachments as $file_attachment ) {
                        $file_name = isset( $file_attachment['file_name'] ) ? $file_attachment['file_name'] : '';
                        $file_info = get_attached_file( $file_attachment[‘attachment’] );
                        echo '<a href="' . $file_info['url'] . '" target="_blank">' . $file_name . '</a>';
                    } 
                } ?>
            </div>
        </div>    
    <?php }
    #18568
    Anh Tran
    Keymaster

    Hi, you need to check if the file is uploaded in your group, like this:

    if ( isset( $file_attachment['attachment'] ) ) {
        $file_info = get_attached_file( $file_attachment[‘attachment’] );
        echo ...
    }
    #18581
    sgtscience
    Participant

    Hi thanks for the response. The if statement is a nice touch. I tried that out and now I am not getting a php error, but the url is still not returning. Here is the updated code:

    add_action( 'woocommerce_after_add_to_cart_button', 'product_download_button', 5 );
    function product_download_button() { ?>
        <div class="dropdown">
            <button class="product-downloads-button nectar-button large regular accent-color regular-button" onclick="return false;">DOWNLOADS
                <i class="fa fa-caret-down"></i>
            </button>
            <div class="dropdown-content"> <?php
                $file_attachments = rwmb_meta( 'file_attachments' );
                if ( ! empty( $file_attachments ) ) {
                    foreach ( $file_attachments as $file_attachment ) {
                        $file_name = isset( $file_attachment['file_name'] ) ? $file_attachment['file_name'] : '';
                        if ( isset( $file_attachment['attachment'] ) ) {
                            $file_info = get_attached_file( $file_attachment['attachment'] );
                            echo '<a href="' . $file_info['url'] . '" target="_blank">' . $file_name . '</a>';
                        }
                    } 
                } ?>
            </div>
        </div>    
    <?php }

    and here is what the resulting link is on the frontend:
    <a href="" target="_blank" rel="noopener noreferrer">Spec Sheet</a>
    When I echo $file_info; nothing is returned so I’m guessing $file_info = get_attached_file( $file_attachment['attachment'] ); is not actually retrieving anything for some reason…

    #18599
    Anh Tran
    Keymaster

    Hi,

    The function you use get_attached_file is not correct. It returns the file path instead of array, see the docs.

    To get the file URL, you can use wp_get_attachment_url file.

    if ( isset( $file_attachment['attachment'] ) ) {
         $file_url = wp_get_attachment_url( $file_attachment['attachment'] );
          echo '<a href="' . $file_url . '" target="_blank">' . $file_name . '</a>';
    }
    #18614
    sgtscience
    Participant

    Hi Thanks again, unfortunately this is still not returning anything for $file_url. echo $file_url; also returns nothing, but it passes the if loop and outputs the html and the file name fine, so it does know that there is an attachment, it just can’t get any info from it. Even if get_attached_file() was the wrong use case in the previous example, it still should have returned a value, but it didn’t, correct?

    #18623
    Anh Tran
    Keymaster

    If it outputs nothing, probably the file is already deleted from the WordPress admin (maybe someone has deleted the file in the Media Library). If that’s the case, you can make a better check with this:

    if ( isset( $file_attachment['attachment'] ) && get_attached_file( $file_attachment['attachment'] ) ) {
         $file_url = wp_get_attachment_url( $file_attachment['attachment'] );
          echo '<a href="' . $file_url . '" target="_blank">' . $file_name . '</a>';
    }
    #18624
    sgtscience
    Participant

    Unfortunately that is not working either. When I tried that nothing shows up in the dropdown. The file is attached and it exists in the media library. I tried removing it and re-attaching it also.

    #18651
    sgtscience
    Participant

    I did a test with the code using the file attachment only not in a group like this and it works fine:

    add_action( 'woocommerce_after_add_to_cart_button', 'product_download_button_test', 5 );
    function product_download_button_test() { ?>
        <div class="dropdown">
            <button class="product-downloads-button nectar-button large regular accent-color regular-button" onclick="return false;">DOWNLOADS
                <i class="fa fa-caret-down"></i>
            </button>
            <div class="dropdown-content"> <?php
                $file_attachments = rwmb_meta( 'attachment_test' );
                if ( ! empty( $file_attachments ) ) {
                    foreach ( $file_attachments as $file_attachment ) { 
                        if ( isset( $file_attachment ) ) {
                            $file_url = ( $file_attachment['url'] ); ?>
                            <a href="<?php echo $file_url; ?>" target="_blank"><?php echo $file_attachment['name']; ?> </a>
                        <?php } 
                    }
                } ?> 
            </div>
        </div>    
    <?php }

    But when it seems as soon as the file attachment is in a group, I can return any info from it. This is the same only with the file attachment in a group:

    add_action( 'woocommerce_after_add_to_cart_button', 'product_download_button_test', 5 );
    function product_download_button_test() { ?>
        <div class="dropdown">
            <button class="product-downloads-button nectar-button large regular accent-color regular-button" onclick="return false;">DOWNLOADS
                <i class="fa fa-caret-down"></i>
            </button>
            <div class="dropdown-content"> <?php
                $file_attachments = rwmb_meta( 'attachment_test' );
                if ( ! empty( $file_attachments ) ) {
                    foreach ( $file_attachments as $file_attachment ) { 
                        if ( isset( $file_attachment ) ) {
                            $file_url = ( $file_attachment['url'] ); ?>
                            <a href="<?php echo $file_url; ?>" target="_blank"><?php echo $file_attachment['name']; ?> </a>
                        <?php } 
                    }
                } ?> 
            </div>
        </div>    
    <?php }

    I get an error “undefined index url” and when I try to use wp_get_attachment_url() or get_attached_file() it returns nothing.
    It seems like whatever I try to do to get info from $file_attachment['attachment'], or when a file attachment is in a group, it is always empty.

    #18652
    sgtscience
    Participant

    Sorry I repeated that code block, Here is the second one properly:

    add_action( 'woocommerce_after_add_to_cart_button', 'product_download_button', 5 );
    function product_download_button() { ?>
        <div class="dropdown">
            <button class="product-downloads-button nectar-button large regular accent-color regular-button" onclick="return false;">DOWNLOADS
                <i class="fa fa-caret-down"></i>
            </button>
            <div class="dropdown-content"> <?php
                $file_attachments = rwmb_meta( 'file_attachments' );
                if ( ! empty( $file_attachments ) ) {
                    foreach ( $file_attachments as $file_attachment ) {
                        $file_name = isset( $file_attachment['file_name'] ) ? $file_attachment['file_name'] : '';
                        if ( isset( $file_attachment['attachment'] ) ) {
                            $file_url = $file_attachment['attachment']; ?>
                            <a href="<?php echo $file_url['url']; ?>" target="_blank"><?php echo $file_name; ?> </a> <?php 
                        }
                    }   
                } ?>
            </div>
        </div>    
    <?php }
    #18658
    Anh Tran
    Keymaster

    I got it. We forget that the returned value of the file_advanced field is an array! So we need to get only the first file ID.

    if ( isset( $file_attachment['attachment'] ) ) {
         $file_id = reset( $file_attachment['attachment']; // Get the first file only.
         $file_url = wp_get_attachment_url( $file_id );
          echo '<a href="' . $file_url . '" target="_blank">' . $file_name . '</a>';
    }
Viewing 10 replies - 1 through 10 (of 21 total)
  • You must be logged in to reply to this topic.