Skip Navigation

[Resolved] display the file name of uploaded pdf files brings notice

This thread is resolved. Here is a description of the problem and solution.

Problem:
display the file name of uploaded pdf files brings notice

Solution:
The custom shortcode was not correctly added. We have adjusted and corrected it.

You can find proposed solution, in this case with the following reply:
https://toolset.com/forums/topic/display-the-file-name-of-uploaded-pdf-files-brings-notice/#post-2232991

Relevant Documentation:

This support ticket is created 3 years 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 3 replies, has 2 voices.

Last updated by katerinaA 3 years ago.

Assisted by: Minesh.

Author
Posts
#2232933

Hello.

I used your instructions from the following ticket, in order to display the file name of uploaded pdf files:

https://toolset.com/forums/topic/display-the-file-name-of-uploaded-pdf-files-and-the-link-in-a-different-tab/

Everything works great but I get a notice, one for each of my displayed posts:

Notice: Undefined index: file_url in /wp-content/themes/Divi-Child/functions.php on line 22

Line 22 is this: $url = "{$atts['file_url']}";

The full code I use is this:

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']}";

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
$filetitle = get_the_title(pippin_get_image_id($fileurl));
//$arr = explode('/',$fileurl); // Split it up so that we can just grab the end part, the filename
$content .= '

  • '.$filetitle.'
  • '; // 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 ''.end($arr).''; // And return the resultant link

    } // We're done!

    }

    // retrieves the attachment ID from the file URL
    function pippin_get_image_id($image_url) {
    global $wpdb;
    $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
    return $attachment[0];
    }

    Any ideas on hoe to get rid of this notice?
    Thank you in advance

    #2232991

    Hello and thank you for contacting the Toolset support.

    From what I can read so far, the shortcode expects two arguments (file_url or types_field). I assume that you are not passing the file_url to it. So, it will trigger a warning. Add the following code at the beginning of the function to make sure there are null/false values when not provided:

    function wpml_hard_link($atts) {
        global $post; // So we can get the post meta later on
        // Attributes
        $atts = shortcode_atts(
    	array(
    		'file_url' => '',
    		'types_field' => '',
    	),
    	$atts
        );
    
    

    This should fix the notice because the $atts will have an index "file_url". Does it make sense?

    #2233357

    Hi Jamal! Of course it doesnt make any sense at all to me, but it worked exactly as it should!
    So, yes! Thank you very much, always best support from all of you!
    When I grow up I want to be a programmer 😉
    Thanx again!

    #2233361

    My issue is resolved now. Thank you!