WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I’m doing up a photography theme in WP at the moment, and I’ve already got great help on this thread (thanks Milo!) for a previous question.

In that thread, I asked about displaying the file name of an uploaded file on the attachment.php page for that file. I’m now using the suggested answer, which uses this code to display the name: <?php echo basename( wp_get_attachment_url( $post->ID ) ); ?>

What I need to do, however, is to display this file without its extension: so, filename, instead of filename.jpg. Is there any easy tweak to this existing PHP snippet that could do this? If not, what would be a best-practice way of achieving this end result?

I should add that I’m far from a PHP expert, so take it easy on me... :)


share|improve this question
up vote 1 down vote accepted

You could use substr()

Returns the portion of string specified by the start and length parameters.

in combination with strrpos()

Find the numeric position of the last occurrence of needle in the haystack string.


$name = basename( wp_get_attachment_url( $post->ID ) );
echo substr( $name, 0, strrpos( $name, '.' ) );

This will show the string (in this case a filename) untill the last occurance of ..

share|improve this answer
That did it – thanks! – kopfschmerz Mar 25 '13 at 17:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.