wp_nav_menu First and Last Classes

wp_nav_menu allows you to show navigation in the front-end. This simple function picks the menu defined in the wp-admin dashboard and displays it as the list. You can define menu separator using “link_before, link_after” or “before, after” params. But the common problem with this function is that sometimes we need to add a class to first or last menu link.

Like in the example below –

Home | PHP | WordPress | CSS |

Of course you do not want the trailing “|“. Now let’s see how do we achieve this. Open the functions.php file the same file where you have registered this menu and paste the following function.

function nav_menu_first_last( $items ) {
 $pos = strrpos($items, 'class="menu-item', -1);
 $items=substr_replace($items, 'menu-item-last ', $pos+7, 0);
 $pos = strpos($items, 'class="menu-item');
 $items=substr_replace($items, 'menu-item-first ', $pos+7, 0);
 return $items;
}
add_filter( 'wp_nav_menu_items', 'nav_menu_first_last' );

 

The above function adds a filter to the navigation such that the first navigation gets a class menu-item-first and the last menu item gets the class menu-item-last assigned. Now all you have to do is to include the necessary CSS style to make it invisible. To do so, simply add the following style information into your CSS stylesheet file, usually styles.css

.menu-item-last {
display: none;
}


The above function adds a filter to the navigation such that the first navigation gets a class menu-item-first and the last menu item gets the class menu-item-last assigned. Now all you have to do is to include the necessary CSS style to make it invisible. To do so, simply add the following style information into your CSS stylesheet file, usually styles.css

.menu-item-last {
display: none;
}

Getting Rid of Vertical Scrollbar from FB iframed pages

When working with FB iframed pages, Sometime the height of content area, you are going to show on FB pag, can need more than 800 pixels. But default height for Facebook’s iFramed pages is 800 pixels. This causes a vertical scrollbar on FB page.

To get rid of this type of scrollbar:

-> Change your “IFrame Size” to “Auto-resize”
On your application developer page, in the “Edit settings” section of your app, in the “Facebook Integration” section, make sure that “IFrame Size” is set to “Auto-resize”, NOT “Show scrollbars.”

-> Add the JavaScript to your iFramed page
First, load Facebook’s Javascript SDK by adding the following code just before the </body> tag of your index page:

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId : 'YOUR-APP-ID-HERE',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
</script>

MAKE SURE you change “YOUR-APP-ID-HERE” to your application ID!

 

-> Once you’ve loaded the Javascript SDK, you can use FB.Canvas.setSize() to get rid of those scrollbars. Put the following code before the </head> tag on your index page:

<script type="text/javascript">
window.fbAsyncInit = function() {
FB.Canvas.setSize();
}
// Do things that will sometimes call sizeChangeCallback()
function sizeChangeCallback() {
FB.Canvas.setSize();
}
</script>

This tells Facebook to resize your iFrame once the page has loaded and again whenever the size of your content changes.

Remove hashtag from URL when using prettyPhoto!

PrettyPhoto, a jQuery lightbox clone, supports image galleries, Flash content, video (including YouTube and vimeo) and embedding external websites using iframes. No doubt, it is really a very simple and flexible plugin which is compatible with all major browsers including IE6.
But if one will see it adds a hashtag like ‘!prettyPhoto’ in url when clicking on an image with prettyPhoto rel attribute. Sometimes we don’t want to add this hashtag in url. How to stop prettyPhoto for add this hashtag in url. Its very easy. Just follow these two steps:

1. Find in “jquery.prettyPhoto.js” :
function setHashtag(){
if(typeof theRel == ‘undefined’) return; // theRel is set on normal calls, it’s impossible to deeplink using the API
location.hash = ‘!’ + theRel + ‘/’+rel_index+’/’; //JST Code
};

And replace it with:
function setHashtag(){
if(typeof theRel == ‘undefined’) return; // theRel is set on normal calls, it’s impossible to deeplink using the API
//location.hash = ‘!’ + theRel + ‘/’+rel_index+’/’; //JST Code
};

2. Find
function clearHashtag(){
// Clear the hashtag only if it was set by prettyPhoto
url = location.href;
hashtag = (url.indexOf(‘#!prettyPhoto’)) ? true : false;
if(hashtag) location.hash = “!prettyPhoto”; //JST Code
}

And replace it with:
function clearHashtag(){
// Clear the hashtag only if it was set by prettyPhoto
url = location.href;
hashtag = (url.indexOf(‘#!prettyPhoto’)) ? true : false;
//if(hashtag) location.hash = “!prettyPhoto”; //JST Code
}

and thats all..

Remove Parent Categories from URL

How to remove the parent category from the url with WordPress 3.1?

One way is, Just paste the below one code in your functions.php :

add_action( ‘init’, ‘build_taxonomies’, 0 );
function build_taxonomies() {

register_taxonomy( ‘category’, ‘post’, array(
‘hierarchical’ => true,
‘update_count_callback’ => ‘_update_post_term_count’,
‘query_var’ => ‘category_name’,
‘rewrite’ => did_action( ‘init’ ) ? array(
‘hierarchical’ => false,
‘slug’ => get_option(‘category_base’) ? get_option(‘category_base’) : ‘category’,
‘with_front’ => false) : false,
‘public’ => true,
‘show_ui’ => true,
‘_builtin’ => true,
) );

}

This will rediect
http://www.mysite.com/category/parent-1/parent-2/child/
to
http://www.mysite.com/category/child/

But if one wants to remove “category” base as well from url, then there is a plugin named “No Category Parents” which hadn’t got any coverage on the web yet. This plugin will completely remove “Category Base” from your WordPress permalinks. It automatically redirects and rewrites all urls to the new url. It changes your “/category/name/” to ”/name/”.

Thats it. 🙂

Track the user who deauthorizes your FB app

Sometimes we may need to track which users of our facebook application are removing the application from their application setting. For this:
– Go to your current app setting page. One way to list your all apps is visit https://www.facebook.com/developers/apps.php
– Now in the application setting in the advanced tab, you’ll see a parameter named Deauthorize Callback in the authentication area. (Settings=>Advanced=>Deauthorize Callback). Here set the url which you want to call when a user removes your app.
– Like you set the url: www.yourdomain.com/remove_app.php , means you are going to track the user id on remove_app.php. So write following code in your rempve_app.php file:

<?phpfunction parse_signed_request($signed_request, $secret) {
list($encoded_sig, $payload) = explode(‘.’, $signed_request, 2);

// decode the data
$sig = base64_url_decode($encoded_sig);
$data = json_decode(base64_url_decode($payload), true);

if (strtoupper($data[‘algorithm’]) !== ‘HMAC-SHA256’) {
error_log(‘Unknown algorithm. Expected HMAC-SHA256’);
return null;
}

// check sig
$expected_sig = hash_hmac(‘sha256’, $payload, $secret, $raw = true);
if ($sig !== $expected_sig) {
error_log(‘Bad Signed JSON signature!’);
return null;
}

return $data;
}

function base64_url_decode($input) {
return base64_decode(strtr($input, ‘-_’, ‘+/’));
}

function write_to_file($user_fb_id) {
$myFile = “deauthorize.txt”;
$fh = fopen($myFile, ‘w’);
fwrite($fh, $user_fb_id);
fclose($fh);
}

$result = parse_signed_request($_REQUEST[‘signed_request’], APP_SECRET_ID);
$user_fb_id = $result[“user_id”];
write_to_file($user_fb_id);
?>

This whole process will happen in backend and you can’t see it on front end. So what i am doing is i am calling a function named “write_to_file()” with a perameter “$user_fb_id” which is the actual id of user. This function will put the user id in your defined file named “deauthorize.txt”. For call the above function, just made two changes before run it:
– place a file on server with name: “deauthorize.txt”
– CHANGE APP_SECRET_ID with your app’s secret ID.

Thats it..

Page 3 of 41234