PHP Get File Extension
November 22nd, 2011 by Fire No comments »Pathinfo does a number of things, depending on what we ask of it, but put simply it returns (path) information about a filepath. Full details can be found on the handy dandy pathinfo page in the PHP manual. Explore the PHP manual page when you get time, but for the purpose of this blog post I’ll hone in to one specific use of the function: getting the file extension. In order to do that, we simply call the function passing along the full filename and a ‘flag’ (a constant which dictates the behaviour of the function) asking for the extension only.
$filename = 'mypic.gif'; $ext = pathinfo($filename, PATHINFO_EXTENSION);
That’s all there is to it! In my opinion, this approach ‘reads’ much more easily than the mess of nested function calls and playing around with string positions, regular expressions, etc.. It is concise and to the point: call pathinfo and only give me back the extension. Simple.
Now for the icing on the cake. When pitched against the other methods detailed above, this call to pathinfo beats all of the others into submission. At least in my testing, it is the fastest method of all (though in random hiccups strrchr does win in around 1% of tests) being on average 1/10th faster than even the strrchr approach.
WordPress wp_nav_menu Seperator And First Last Classes
November 10th, 2010 by Fire 3 comments »Using WordPress wp_nav_menu function you can display navigation menus on your site nicely. You can even add the separators between each navigation menu. It can be done by passing these arguments either “link_before, link_after” or “before, after” to your wp_nav_menu funtion. However, you might face the problem with adding separators between each navigation menu.
Let’s say you would like to add “|” between navigation menu links but do not want to show it after the last menu.
e.g.
Home | About Us | FAQs | Blog | <– you do not want this
So far wp_nav_menu function does not add any additional classes to the first and last of the navigation menus. But don’t worry to much about that because there is a trick to fix it. Just copy and paste below code to your functions.php
Put this into your functions.php
function nav_menu_first_last( $items ) { $position = strrpos($items, 'class="menu-item', -1);<br />
$items=substr_replace($items, 'menu-item-last ', $position+7, 0);<br />
$position = strpos($items, 'class="menu-item');<br />
$items=substr_replace($items, 'menu-item-first ', $position+7, 0);<br />
return $items;<br />
}<br />
add_filter( 'wp_nav_menu_items', 'nav_menu_first_last' );
Put this into your style.css or any css file you using.
.menu-item-last {<br />
display: none;<br />
}
Get WordPress User Id Displayed On Your Post/Page
August 28th, 2010 by Fire 7 comments »This code snippet let you display you wordpress user’s ID number on Post or Page.
<?php function Display_user_ID_for_Me() {
<pre>global $current_user;
$current_user = wp_get_current_user();
return $current_user->ID;
}
add_shortcode('DisplayUserID', 'Display_user_ID_for_Me');
?>
How To Use:
1. Copy and Paste this code in functions.php file of your wordpress theme.
2. Use [DisplayUserID] this shortcode the display User’s ID number on Post or Page.
