• Resolved msargenttrue

    (@msargenttrue)


    More specifically: I am able to retrieve the links of uploads but how can I pass a variable from fu_after_upload to a shortcode that is placed on the upload redirect page?

    When the Frontend Uploader is used to upload files, I’ve made some progress on capturing the links of uploads and echoing them to the screen. However, I am unsure how to pass the links to the page that the upload form redirects to. I’ve tried to set $clickable_links to a global variable and pass it to a shortcode function, but I am unable to access the variable in this way.

    After an upload completes, the following code allows me to capture the URLs of the new media attachments that have been created. The URLs are stored in an array of clickable links.

    add_action( 'fu_after_upload', 'my_fu_after_upload', 10, 3 );
    function my_fu_after_upload( $attachment_ids, $success, $post_id ) {
    
    	global $clickable_links;
    	$count = count($attachment_ids);
    
    	for($i=0; $i<=$count; $i++) {
    		$fu_download_links[$i] = wp_get_attachment_url($attachment_ids[$i]);
    		$clickable_links[$i] = '<p><a href="' . $fu_download_links[$i] . '">' . $fu_download_links[$i] . '</a></p>';
    		// echo $clickable_links[$i];
    	}
    }

    Note: The echo line will output the links to the screen, but it is commented out because the end goal is to move the $clickable_links array to a shortcode that is placed on the redirect page. Here’s what I have so far:

    add_shortcode( 'fu-display-links', 'display_fu_links_shortcode' );
    function display_fu_links_shortcode ($atts) {
    
    	global $clickable_links;
    	$count = count($clickable_links);
    
    	for($i=0; $i<=$count; $i++) {
    		echo $clickable_links[$i];
    	}
    }

    The global doesn’t seem to work for this, is it being used incorrectly? Can I pass the variable to the shortcode via a parameter? Do I need to use a PHP Session? Or is there an easier way?

    Thanks for your help.

    https://www.ads-software.com/plugins/frontend-uploader/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Rinat

    (@rinatkhaziev)

    Howdy,

    Once you do a redirect, it’s a completely different request, meaning that all the variables you’ve set in your filter won’t be available to you.

    You can use session, if you’re already using it across your site. However, using session is not recommended dev practice. Here’s a nice plugin you can use: https://www.ads-software.com/plugins/wp-session-manager/ (it stores a session-like object in users cookies). Alternatively, you can use a transient (it might not work if you’re using one of caching plugins).

    This is an interesting use case, so I just have added a new filter ‘fu_upload_result_query_args’ in the development version. I suggest you to copy the plugin’s code from here and replace the code in your installation.

    Once you do so, you’ll be able to modify URL GET variables, and pass attachment ids that way.
    So it will look something like

    add_filter( 'fu_upload_result_query_args', 'my_fu_upload_result_query_args', 10, 2 );
    function my_fu_upload_result_query_args( $qa, $result ) {
    	$qa['media_ids'] = isset( $result['media_ids'] ) ? $result['media_ids'] : array();
    	return $qa;
    }

    and then you’ll need to modify your shortcode as follows:

    add_shortcode( 'fu-display-links', 'display_fu_links_shortcode' );
    function display_fu_links_shortcode ( $atts ) {
    	$clickable_links = '';
    	foreach( $_GET['media_ids'] as $attachment_id ) {
    		$fu_download_link = wp_get_attachment_url($attachment_id);
    		$clickable_links .= '<p><a href="' . $fu_download_link . '">' . $fu_download_links . '</a></p>';
    	}
    
    	return $clickable_links;
    }

    Keep in mind, the code is completely untested and written in a half-awake state ??

    Plugin Author Rinat

    (@rinatkhaziev)

    Also, I suggest you check out the source to make sure you understand what’s going on prior to triggering that filter.

    Cheers

    Thread Starter msargenttrue

    (@msargenttrue)

    Your half-awake coding skills aren’t half bad! ??

    Now it works exactly how I wanted it to. Great!

    The only issue I noticed is that there was a typo in the variable name, on this line:

    $clickable_links .= '<p><a href="' . $fu_download_link . '">' . $fu_download_links . '</a></p>';

    I changed it so it reads:

    $clickable_links .= '<p><a href="' . $fu_download_link . '">' . $fu_download_link . '</a></p>';

    So if anyone is interested, we could call this mod something like: Retrieve Upload URLs on Redirect

    I made a couple other little edits:

    1) I added a Title to introduce the links: “URLs to Uploaded Files”.
    2) The URLS are now outputted as unordered list items to improve the look.
    3) I added a condition to check if the media_ids are set. It will now output a message if they’re not. Before I did this navigating to the redirect page by itself in the browser would output a PHP error.

    So here’s the final code for functions.php:

    add_filter( 'fu_upload_result_query_args', 'my_fu_upload_result_query_args', 10, 2 );
    function my_fu_upload_result_query_args( $qa, $result ) {
    	$qa['media_ids'] = isset( $result['media_ids'] ) ? $result['media_ids'] : array();
    	return $qa;
    }
    
    add_shortcode( 'fu-display-links', 'display_fu_links_shortcode' );
    function display_fu_links_shortcode ( $atts ) {
    	$clickable_links = '';
    	$html_before = '<h3>URLs to Uploaded Files:</h3><ul class="fu-uploaded-files">';
    	$html_after = '</ul>';
    	if ( isset( $_GET['media_ids'] )) {
    		foreach( $_GET['media_ids'] as $attachment_id ) {
    			$fu_download_link = wp_get_attachment_url($attachment_id);
    			$clickable_links .= '<li><a href="' . $fu_download_link . '">' . $fu_download_link . '</a></li>';
    		}
    		return $html_before . $clickable_links . $html_after;
    	}
    	else {
    		echo "<p>No files were uploaded to this page.</p>";
    	}
    }

    And here’s what the shortcodes on my redirect page look like:

    [fu-upload-response]
    
    [fu-display-links]

    Thanks so much for your help ??

    Plugin Author Rinat

    (@rinatkhaziev)

    No problem, glad that I could help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Here's how to get links of uploads, but how to pass links to shortcode?’ is closed to new replies.