Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
// work in progress
// WP php script
// create a list of direct links to files via atattchment post type
// manage documents with media library
// have the documents appear in search results via attatchment post type
// made to work with https://wordpress.org/plugins/media-library-organizer/ because it has a more user friendly UI for handling media organisation AND it uses simple taxonomy rather then some similar, maybe better plugins
// If one wants to use it without the plugin uncomment the following block in order to create taxonomies for attatchment post type, and modify the args accordingly
/*
// Add categories to attatchments
function apuri_add_categories_to_attachments() {
register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init' , 'apuri_add_categories_to_attachments' );
*/
// shortcode with parameters - default is to list all files
add_shortcode( 'apuri_dokumenti', 'apuri_dokumenti_parameters_shortcode' );
function apuri_dokumenti_parameters_shortcode( $atts ) {
ob_start();
// define attributes and their defaults
extract( shortcode_atts( array (
'type' => array( 'attachment' ),
'post_status' => 'any',
'order' => 'ASC',
'cat' => '',
'doctype' => array(
'application/pdf',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
),
), $atts ) );
// define query parameters based on attributes
$options = array(
'post_type' => $type,
'post_status' => 'any',
'order' => $order,
'orderby' => 'meta_value',
'meta_key' => 'order_number',
// 'category_name' => $cat, // if media library organizer is not used
'tax_query' => array(
array(
'taxonomy' => 'mlo-category', // Works either with custom tax or with default, only one cat at time
'field' => 'slug',
'terms' => array( $cat ),
// 'operator' => 'IN',
),
),
'post_mime_type' => $doctype,
);
$query = new WP_Query( $options );
// run the loop based on the query
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>
<ul class="documents-listing ">
<?php
$fileurl = wp_get_attachment_url( $the_post );
$filetype = wp_check_filetype( $fileurl );
echo '<li><a href="' . esc_url( $fileurl ) . '" class="document">';
echo esc_html( the_title() . '.' );
echo esc_html( $filetype['ext'] );
echo '</a></li>';
?>
</ul>
<?php
endwhile;
else:
echo "<p>Sorry no docs are here</p>";
endif;
wp_reset_query();
$apuri_clean = ob_get_clean();
return $apuri_clean;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment