if ( ! function_exists( 'rtmedia_custom_code_get_attribute_list' ) ) {
/**
* Function get list of attributes attached to rtMedia media.
*
* @param int|bool $id rtMedia ID | False when no ID is passed.
* @param bool $is_rtmedia_id Is passed ID is rtMedia ID or attachment ID.
* @return void
*/
function rtmedia_custom_code_get_attribute_list( $id = false, $is_rtmedia_id = true ) {
if ( empty( $id ) ) {
$id = rtmedia_id();
if ( empty( $id ) ) {
return false;
}
}
$rtmedia_attr_model = new RTMediaAttributesModel();
$attributes = $rtmedia_attr_model->get_all_attributes();
if ( empty( $is_rtmedia_id ) ) {
$media_post_id = $id;
} else {
$media_post_id = rtmedia_media_id( $id );
}
$media_terms = array();
foreach ( $attributes as $attr ) {
if ( 'taxonomy' !== $attr->attribute_store_as ) {
continue;
}
$term_order = 'id';
if ( 'name' === $attr->attribute_orderby ) {
$term_order = 'name';
}
$taxonomy_name = rtmedia_attribute_taxonomy_name( $attr->attribute_name );
$terms = wp_get_post_terms( $media_post_id, $taxonomy_name, array( 'orderby' => $term_order, 'fields' => 'all' ) );
$media_terms = array_merge( $media_terms, $terms );
}
// $media_terms is your final list of terms attached to 1 image.
return $media_terms;
}
}
The above function can be used to get list of attributes attached to an rtMedia media file. Paste this function in your theme’s functions.php
file.
By default this function assumes that you’re using rtMedia media ID. You can get it from single media page URL, i.e. https://example.com/members/example/media/579/
. Here 579
is the rtMedia ID.
If you’re using attachment ID, from wp-admin -> Media, then you need to pass true
in the second argument.
I.e. rtmedia_custom_code_get_attribute_list( 11200, true );
Here 11200
is the attachment ID taken from wp-admin -> Media page.
Use this function with rtMedia hooks
You can use this function with existing rtMedia hooks.
If you want to get list of attributes on rtMedia single media page, hook this function to rtmedia_before_item
action.
I.e.add_action( 'rtmedia_actions_before_description', 'rtmedia_custom_code_get_attribute_list' );
To get list of attributes of all media on rtMedia media gallery page, use rtmedia_before_item
action.
I.e.add_action( 'rtmedia_before_item', 'rtmedia_custom_code_get_attribute_list' );