Following PHP code shows WordPress media files at user front end
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'author' => $current_user->ID,
'post_parent' => $post->ID,
'caller_get_posts'=> 1,
);
$attachments = get_posts( $args );
if ($attachments) {
foreach ($attachments as $attachment) {
echo '<tr><td><a href="'.wp_get_attachment_url($attachment->ID).'" rel="shadowbox" title="'.$attachment->post_excerpt.'">';
echo $attachment->_wp_attached_file;
echo '</a>
</td>
}
}?>
Above code shows the complete file name at front end. If someone upload sssssssssssssssssssssssssssssssssssssssssssss.txt, 123457xxxxxxxxxxxxxxx.gif or xls, pdf, docx and all other extensions with different long file names it create mess on my theme. Can anyone please help guide me how to trim the file name and show something like sssssss...ssss.txt xxxxxx...xxxx.pdf or gif at front end?
I tried following function for specific file in functions.php still no change
$wp_attached_file = 'sssssssssssssssssssssssssssssssssssssssssssss.txt';
echo preg_replace('/(.{3}).*(\..{2,4})/', '$1...$2', $wp_attached_file);
and
$attachment = $wp_attached_file;
echo preg_replace('/(.{3}).*(\..{2,4})/', '$1...$2', $wp_attached_file);
It is working now, I replace following line
echo ($attachment->_wp_attached_file);
with
echo preg_replace('/(.{15}).*(.{5})(\..{2,4})/', '$1...$2$3', $attachment->_wp_attached_file);