Simple Media Library Folders: Function Reference
When working with the Simple Media Library Folders plugin, sometimes you may need to manage the folders programmatically in PHP.
Though the folders are actually just a custom taxonomy smlf_folder and any taxonomy management function will do the trick, it may be easier to do it with built-in functions.
The functions documentation is below.
Folders Management
smlf_get_folders()
This function allows you to get the list of media library folders from your site. It is very similar to the official get_terms() function in WordPress.
smlf_get_folders( $args = array() );- $argsarray
- All parameters of the
get_terms()function are supported here, except thetaxonomyparameter, here is the list.
Retrieves the list of folders.
Example. Print a list of child folders of a specific parent folder
$folders = smlf_get_folders(
array(
'parent' => 123, // it is a parent folder ID
'hide_empty' => false,
'orderby' => 'name',
)
);
if( $folders ) {
echo '<ul>';
foreach( $folders as $folder ) {
echo '<li>' . $folder->name . '</li>';
}
echo '</ul>';
}smlf_get_folder_by()
Allows you to retrieve data of a specific folder by its ID, slug, or name.
smlf_get_folder_by( $field, $value, $output = OBJECT );- $fieldstring
- Accepts
id,slug, andnamevalues. - $valueint | string
- Provide a folder ID, slug, or name here depending on what is set in the first parameter.
- $outputstring
- In which format you would like to get the folder data, either
OBJECT(an object),ARRAY_A(an associative array), orARRAY_N(a numeric array).
Returns a WP_Term object on success, an WP_Error object otherwise.
Example. Get a folder by its slug
$folder_slug = 'manga';
$folder = smlf_get_folder_by( 'slug', $folder_slug, ARRAY_A );
echo 'We found a folder with the ID = ' . $folder[ 'term_id' ];
/*
Array
(
[term_id] => 34
[name] => Manga
[slug] => manga
[term_group] => 0
[term_taxonomy_id] => 34
[taxonomy] => smlf_folder
[description] =>
[parent] => 0
[count] => 12
[filter] => raw
)
*/smlf_create_folder()
Allows you to create a new folder.
smlf_create_folder( $folder_name );- $folder_namestring
- A new folder name.
Returns a new folder ID on success, WP_Error otherwise.
smlf_update_folder()
Allows you to update data of a specific folder, for example, its name, slug, etc.
smlf_update_folder( $folder_id, $args );- $folder_idint
- The folder ID you need to update.
- $argsarray
- The array of folder data to update.
- namestring
- The folder name.
- slugstring
- The folder slug.
- parentint
- The parent folder ID.
Returns a folder ID on success, an WP_Error object otherwise.
smlf_delete_folder()
Allows you to delete a folder by its ID.
smlf_delete_folder( $folder_id );- $folder_idint
- The folder ID.
Returns true on success, false – if the given folder doesn’t exist.
Media Files Management
smlf_get_attachment_ids_in_folder()
This function will return an array of IDs of the media files in a specific folder or folders.
smlf_get_attachment_ids_in_folder( $folder_ids );- $folder_idsint | array
- You can provide a folder ID here or an array of folder IDs.
Returns an array of integers (attachment IDs).
smlf_set_attachment_folders()
With the help of this function, you can add an attachment to one or multiple folders programmatically.
smlf_set_attachment_folders( $attachment_id, $folders, $append = false );- $attachment_idint
- The attachment (media file) ID
- $foldersarray
- Provide an array of folder IDs or slugs you are going to add the attachment to.
- $appendbool
- Whether we need to replace the existing attachment folders with the new ones (by default) or just to append new folders without removing existing folders.
The function returns an array of folders affected or an WP_Error object on failure.