I would like to be able to get the URL of a html file that has been uploaded to a folder via FTP.
So if I go into a post in the admin back end of WordPress, I would like there to be a custom field with a file browser that would list the html files and subdirectories in: wp-content/uploads/articles/
Then when a file has been selected it will log the url of that file so it can be retrieved on the front end.
I’ve done countless searches on Google and here and can’t seem to find the answer I’m looking for. Would just like to know if it can or cant’t be done so I can plan accordingly.
Any insight would be greatly appreciated!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can scan the directory for files with glob() then loop throw the results and list them in a admin modal (thickbox) or just show the list within a admin metabox in the post editor.
Here’s a little snippet that i was playing with.
/**
* Get HTML Article Files from uploads/articles
* Support both .htm & .html ext.
*
* @return array List of html article files with path & url.
*/
function wpse_384622_get_articles() {
$uploads = wp_upload_dir();
$file_path = $uploads['basedir'] . '/articles/';
$file_url = $uploads['baseurl'] . '/articles/';
$articles = [];
foreach ( glob( $file_path . '*.htm*' ) as $path ) {
if ( is_readable( $path ) ) {
$file_type = wp_check_filetype( $path );
if ( 'text/html' === $file_type['type'] ) {
$file_list[] = [
'path' => $path,
'url' => str_replace( $file_path, $file_url, $path ),
];
}
}
}
return $articles;
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0