Skip Navigation

[Resolved] Attached files file name

This support ticket is created 2 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 11 replies, has 2 voices.

Last updated by luisP-6 2 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#2349991

Hi,

I applied this solution: https://toolset.com/forums/topic/show-date-created-for-attached-files/ but finally it didn't fixed my problem at all. It's showing post title, instead of attached file title.

[show_file_info url="[types field='attachedfiles' output='raw'][/types]" info="title"] - [show_file_info url="[types field='attachedfiles' output='raw'][/types]" info="date"]

Is there any way to show Filename + Filetitle + Creation date?

For example, I've a file with this name "test-1.pdf", with the title "lorem ipsum".

I would like to show "TEST 1 (LOREM IPSUM) - 27/04/2022".

Thank you!

#2350315

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I will have to check how you applied the code you shared. Can you please share admin access details and where exactly you want to display the file information.

Please let me know where you added the custom code that you shared with the reference ticket.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2351657

Hi,

Finally I'm using this code from other support ticket. I'm using

 $attdate = get_the_date('d-m-Y', $attachement_id); 

to show attachement upload date, but instead it shows always post date.

How could I modify this code to show only attachement date instead post date?

Thank you!

Full code is:

<?php
/**
 * New custom code snippet (replace this with snippet description).
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

add_shortcode( 'my_file_name', 'wpml_hard_link'); // Actually activate the shortcode
function wpml_hard_link($atts) {
    global $post; // So we can get the post meta later on
 
    $url = "{$atts['file_url']}";
    $types = "wpcf-{$atts['types_field']}";
    $attdate = get_the_date('d-m-Y', $attachement_id);

   
     
    if ($types) { // if the types_field argument was provided 
 
        $urls = get_post_meta($post->ID,$types); // let's get the (potentially multiple) values


     
        $content = ''; // Setting up a variable to hold the links so we can return it later
     
        foreach ($urls as $fileurl) { // Let's iterate for each of the multiple values
            $arr = explode('/',$fileurl); // Split it up so that we can just grab the end part, the filename
                    $arr = str_replace('.pdf', '', $arr);
          			$arr = str_replace('.doc', '', $arr);
          			$arr = str_replace('-', ' ', $arr);

            $content .= '<i class="far fa-file-pdf"></i> <a href="'.$fileurl.'">'.end($arr).'</a>. (Publicació dia '.$attdate.') <br />'; // Create the link and store it in the $content variable
        }
         
        return $content; // Return the content as the shortcode value
     
    } else {  // Else we didn't use the fields_type argument, we just needed one URL we provided explicitly
            $arr = explode('/',$url); // So let's split that URL up so we can grab the end
            return '<a href="'.$url.'">'.end($arr).'</a>'; // And return the resultant link
     
    } // We're done!
     
}
#2351699

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What if you try to use the following modified code:

add_shortcode( 'my_file_name', 'wpml_hard_link'); // Actually activate the shortcode
function wpml_hard_link($atts) {

    global $post; // So we can get the post meta later on
  
    $url = "{$atts['file_url']}";
    $types = "wpcf-{$atts['types_field']}";

 $attachment_id = $wpdb->get_var($wpdb->prepare(
        "SELECT ID FROM $wpdb->posts WHERE guid = %s", $url
    ));

    $attdate = get_the_date('d-m-Y', $attachement_id);
      
    if ($types) { // if the types_field argument was provided 
  
        $urls = get_post_meta($post->ID,$types); // let's get the (potentially multiple) values
 
 
      
        $content = ''; // Setting up a variable to hold the links so we can return it later
      
        foreach ($urls as $fileurl) { // Let's iterate for each of the multiple values
            $arr = explode('/',$fileurl); // Split it up so that we can just grab the end part, the filename
                    $arr = str_replace('.pdf', '', $arr);
                    $arr = str_replace('.doc', '', $arr);
                    $arr = str_replace('-', ' ', $arr);
 
            $content .= '<i class="far fa-file-pdf"></i> <a href="'.$fileurl.'">'.end($arr).'</a>. (Publicació dia '.$attdate.') <br />'; // Create the link and store it in the $content variable
        }
          
        return $content; // Return the content as the shortcode value
      
    } else {  // Else we didn't use the fields_type argument, we just needed one URL we provided explicitly
            $arr = explode('/',$url); // So let's split that URL up so we can grab the end
            return '<a href="'.$url.'">'.end($arr).'</a>'; // And return the resultant link
      
    } // We're done!
      
}

does that helps?

#2352283

Hi,

After replacing for this code, I got this error on the front-end:

Warning: Undefined array key "file_url" in /var/www/vhosts/llucmajor.org/httpdocs/wp-content/toolset-customizations/showfilename.php on line 15

Warning: Undefined variable $wpdb in /var/www/vhosts/llucmajor.org/httpdocs/wp-content/toolset-customizations/showfilename.php on line 18

How could I fix it?
Thanks!

#2352285

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please tell me how you are calling the shortcode?

#2352293

Hi!

Shortcode is the following:

[my_file_name types_field="attachedfiles"]

#2352301

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please try to use shortcode as given under:

[my_file_name types_field="attachedfiles" file_url="[types field='attachedfiles' output='raw'][types]"]

Please let me know if it works as expected.

#2352309

Hi,

Still same error on the front-end:

Warning: Undefined array key "file_url" in /var/www/vhosts/llucmajor.org/httpdocs/wp-content/toolset-customizations/showfilename.php on line 15

Warning: Undefined variable $wpdb in /var/www/vhosts/llucmajor.org/httpdocs/wp-content/toolset-customizations/showfilename.php on line 18

#2352319

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please try to use the following modified code:

add_shortcode( 'my_file_name', 'wpml_hard_link'); // Actually activate the shortcode
function wpml_hard_link($atts) {
 
    global $post; // So we can get the post meta later on
   global $wpdb;

$atts = shortcode_atts(
        array(
            'file_url' => '',
            'types_field' => '',
        ),
        $atts
    );

    $url = "{$atts['file_url']}";
    $types = "wpcf-{$atts['types_field']}";
 
 $attachment_id = $wpdb->get_var($wpdb->prepare(
        "SELECT ID FROM $wpdb->posts WHERE guid = %s", $url
    ));
 
    $attdate = get_the_date('d-m-Y', $attachement_id);
       
    if ($types) { // if the types_field argument was provided 
   
        $urls = get_post_meta($post->ID,$types); // let's get the (potentially multiple) values
  
  
       
        $content = ''; // Setting up a variable to hold the links so we can return it later
       
        foreach ($urls as $fileurl) { // Let's iterate for each of the multiple values
            $arr = explode('/',$fileurl); // Split it up so that we can just grab the end part, the filename
                    $arr = str_replace('.pdf', '', $arr);
                    $arr = str_replace('.doc', '', $arr);
                    $arr = str_replace('-', ' ', $arr);
  
            $content .= '<i class="far fa-file-pdf"></i> <a href="'.$fileurl.'">'.end($arr).'</a>. (Publicació dia '.$attdate.') <br />'; // Create the link and store it in the $content variable
        }
           
        return $content; // Return the content as the shortcode value
       
    } else {  // Else we didn't use the fields_type argument, we just needed one URL we provided explicitly
            $arr = explode('/',$url); // So let's split that URL up so we can grab the end
            return '<a href="'.$url.'">'.end($arr).'</a>'; // And return the resultant link
       
    } // We're done!
       
}

If above does not work, please share admin access details. Please share problem URL as well where you want to display the image file name with date.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2352417

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've adjusted the custom code you added to "Custom Code" section as given under:

add_shortcode( 'my_file_name', 'wpml_hard_link'); // Actually activate the shortcode
function wpml_hard_link($atts) {
  
    global $post; // So we can get the post meta later on
   global $wpdb;
 
$atts = shortcode_atts(
        array(
            'file_url' => '',
            'types_field' => '',
        ),
        $atts
    );
 
    $types = "wpcf-{$atts['types_field']}";   
        
    if ($types) { // if the types_field argument was provided 
    
        $urls = get_post_meta($post->ID,$types); // let's get the (potentially multiple) values
        
        $content = ''; // Setting up a variable to hold the links so we can return it later
        
        foreach ($urls as $fileurl) { // Let's iterate for each of the multiple values
          
          $attachment_id = $wpdb->get_var($wpdb->prepare(
        "SELECT ID FROM $wpdb->posts WHERE guid = %s", $fileurl
    ));
      $attdate = get_the_date('d-m-Y', $attachment_id);
          
            $arr = explode('/',$fileurl); // Split it up so that we can just grab the end part, the filename
                    $arr = str_replace('.pdf', '', $arr);
                    $arr = str_replace('.doc', '', $arr);
                    $arr = str_replace('-', ' ', $arr);
   
            $content .= '<i class="far fa-file-pdf"></i> <a href="'.$fileurl.'">'.end($arr).'</a>. (Publicació dia '.$attdate.') <br />'; // Create the link and store it in the $content variable
        }
            
        return $content; // Return the content as the shortcode value
        
    } else {  // Else we didn't use the fields_type argument, we just needed one URL we provided explicitly
            $arr = explode('/',$url); // So let's split that URL up so we can grab the end
            return '<a href="'.$url.'">'.end($arr).'</a>'; // And return the resultant link
        
    } // We're done!
        
}

From the content template the shortcode needs to be called as given under:

  <div class="listelementtool">
[my_file_name types_field="attachedfiles"]</div>

Can you please confirm it works as expected:
- hidden link

#2352697

My issue is resolved now. Thank you!