Archive for the ‘Web Development’ Category


Facebook AS3 API – Not getting response from auth.sessionChange or auth.login after password login?

Facebook’s AS3 API has handy built in methods for dealing with Javascript / Flash communication regarding authentication and other Facebook events. However, it doesn’t always work when it should. Consider the situation below:

Facebook.addJSEventListener('auth.login', onSessionChange);
var opts:Object = {scope:"publish_stream, user_photos, create_event"};
Facebook.login(onResult, opts);

In this case, we simply want to login and get the response from Facebook when our user has completed authentication. 

This works when the user is already logged into Facebook and Flash merely needs to register the application instance. However, if the user has to deal with the login prompt, the API does something funny. Flash will receive the response from Facebook but it will not necessarily be ready on Facebook’s server by the time you get it. Meaning your method receiving your auth.login event  will be called but if you try to do anything like …

Facebook.api('/me',onMeResult);

… your result will be an unauthenticated user error.

So the problem is that Facebook doesn’t assure it’s ready on its end before it sends you the response. My solution is terrible but it works — consider using a timer for your first API call with one second intervals from your auth.login method. In case the user cannot log in you might want to limit it to 5 cycles since no one will realistically time out longer than 5 seconds.

Facebook Javascript Update / OAuth 2.0 Requirement breaking login pop-up?

In their December 2011 changes to the Javascript SDK, Facebook has caused certain websites using the AS3 API to break. The issue arises with the login pop-up window no longer appearing when Facebook.login is called.

The problem lies within the perms parameter used in Facebook.login. You must now use scope instead.

var opts:Object = {scope:"publish_stream, user_photos, create_event"};
Facebook.login(onResult, opts); 

This solve was found at the facebook-actionscript-api Google Code group. Thanks guys!

How to send URL Variables into a Facebook App / Tab

If you need to send URL variables into a Facebook App (within a Page tab), doing so is surprisingly easy.

Firstly, know that the JSON-based object you are interacting with is called signed_request. It is used primarily for internal communication between Facebook and your app. However, Facebook does give you one app_data variable to use for your own variables. To send a variable to your Facebook app, all you need to do is set a value to app_data in your URL request and Facebook will handle communicating that data to your app’s iFrame.

URL example (sending data):

https://www.facebook.com/pages/Walrus-In-A-Canoe/?sk=app_0000000000&app_data=APP_DATA_CONTENT

For an example of retrieving your app_data, I have found some nifty code from fbadurbin on this thread which I have pasted below.

$signed_request = $_REQUEST['signed_request']; // Get the POST signed_request variable.

 if(isset($signed_request)) // Determine if signed_request is blank.
 {
 $pre = explode('.',$signed_request); // Get the part of the signed_request we need.
 $json = base64_decode($pre['1']); // Base64 Decode signed_request making it JSON.
 $obj = json_decode($json,true); // Split the JSON into arrays.
 $page = $obj['page']; // Get the page array. It has a sub array.

 echo("Your App Data: " . $obj['app_data']);
 }
 else
 {
 die('No signed request avaliable.'); //If there is no signed_request, stop processing script.
 }

How to connect Facebook Apps to Facebook Pages with no View App Profile button

As of late 2011, Facebook has begun to phase out the View App Profile button you may have used to connect Facebook Apps to fan pages.

The replacement technique is rather strange. You need to type a specific URL into your browser and fill out the following:

https://www.facebook.com/dialog/pagetab?app_id=APP_ID&redirect_uri=TAB_SOURCE_URL

If you provide the correct parameters, you will receive the following dialogue box:

You can read more at Facebook’s official documentation below.
https://developers.facebook.com/docs/reference/dialogs/add_to_page/

Facebook Comments Moderation & WordPress

If you are having trouble with Facebook Comments moderation through WordPress, this may be your solution.

The first thing to always check with Facebook is that you are using their most recent protocols. Facebook is notoriously changing how you must integrate it into your website, to be sure to check this link regularly: https://developers.facebook.com/docs/guides/web/

And the latest code (August 2011) is below. Place this below the opening <body> tag in your HTML document.

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId:'YOUR_APPLICATION_ID', cookie:true,
status:true, xfbml:true
});
</script>

In order to have comment moderation, you’ll need to do a few things:

1) Set up an App and enable moderator access to whoever needs to have it.
2) Add relevant <meta> property tags to the <head> portion of your website.
3) Match the comment plugin’s href attribute to the current permalink URL.

There are two ways to do the <meta> property tags. You can either enable a specific user to have moderator access or give access to all moderators within the App. Going with the App method is more ideal because it will connect to your App’s dashboard in www.facebook.com/developers/. A direct link to managing comments via the App interface would be https://developers.facebook.com/apps/{YOUR_APPLICATION_ID}/plugins?view=queue. This method’s code is below:

<meta property="fb:app_id" content="{YOUR_APPLICATION_ID}">

You can always read more about the comments plugin by going to https://developers.facebook.com/docs/reference/plugins/comments/ although their documentation is typically confusing and subpar.

One way WordPress can make moderation break is due to the fact that page GUID’s may be different from your permalink URL’s. If you put a $post->GUID into your <fb:comments href=”">, moderation will NOT work. The comments URL and the page URL must be identical. To get the page URL of a post, you can use get_permalink(). By feeding the permalink into your comments plugin and doing the other steps above, you should be in good shape! Just be aware that by changing permalink structure or changing URL’s you will lose all of your comments.

(this solution was discovered via http://forum.developers.facebook.net/viewtopic.php?id=102099)

WordPress – How to Limit Number of Search Results

I had a heck of a time looking for a solution to this very common problem. Fortunately, the solution is quite easy!

WordPress, by default, limits your search results to 10 per page. When doing a normal query, you’d use get_posts(numberposts=99) to determine how many results appear from your query. You use a different parameter, showposts, for search pages.

So how do you modify the query to show what you want? Simply take the current $query_string (generated by WordPress’s header functions) and modify its showposts. Make sure to do this before the loop and you’re all good! All the code you need is below.

[php]query_posts($query_string . ‘&showposts=12′);[/php]


WordPress Pagination 404′s with Custom Post Types

If you’re using custom post types in WordPress and your category pages are throwing 404′s every time you try to paginate, you may be running into a bug with the WordPress core (as of version 3.0.4).

There is a solution — but first make sure that you have:

1) Checked your permalink settings to make sure there is nothing wonky. Try the recommended presets in the permalinks settings page and see if you are still having a problem.

2) View the same category page on the default skin. This will rule out issues that may lie within your specific code.

3) Cried a bit. Just kidding… kinda (I suffered for days before coming to a resolution courtesy of Mark / t31os)

If you’re still getting 404′s… here’s the code that saved my life. Replace your_custom_type with your custom type (surprise). You can put however many custom types as you’d like. Do this in your functions.php.

[php]add_action( 'parse_query','changept' );
function changept() {
	if( is_category() && !is_admin() )
		set_query_var( 'post_type', array( 'post', 'your_custom_type' ) );
	return;
}[/php]

Best of luck! You can view my entire sob story / thread here (and give Mark all of your money if possible).

How to Dynamically Create WordPress Posts

If you’ve ever had a need to dynamically create (or update) WordPress posts, there’s a simple way to do so.  Use wp_insert_post.

// Create post object
  $my_post = array(
     'post_title' => 'My post',
     'post_content' => 'This is my post.',
     'post_status' => 'publish',
     'post_author' => 1,
     'post_category' => array(8,39)
  );

// Insert the post into the database
  wp_insert_post( $my_post );


If you set the ID property of your post array to a number  (i.e. $my_post['ID']  => 15), it will update an existing post rather than adding a new one. Leaving ID blank will create a new post. You cannot force a new post to acquire a specific ID.

For any further details, visit wp_insert_post in the WordPress Codex. For whatever reason, this documentation is not easy to find on Google, so I hope this simple article will make it easier for everyone.

WordPress: Automatically Generate Thumbnails from Your Post Images

WordPress automatically generates thumbnails for every image you upload. However, it does not make these thumbnails obviously accessible through code, and people have built a number of plugins to allow you to work around this.

Grabbing thumbnails and resized images is essential to creating galleries or grids of similar images. Knowing a few tricks about WordPress will help you do this more efficiently and without causing conflicts with other sections of your site.

1) Custom thumbnail sizes. You can have WordPress generate any number of thumbnails from every image you upload. For example, put the following code in functions.php.

if ( function_exists( ‘add_image_size’ ) ) {

add_image_size( ‘press-thumb’, 226, 292, true );
add_image_size( ‘news-thumb-small’, 63, 42, true );
add_image_size( ‘news-thumb-medium’, 228, 146, true );
add_image_size( ‘news-thumb-large’, 450, 230, true );
add_image_size( ‘news-thumb-xl’, 640, 390, true );

}

Note that creating a new image size requires WordPress to regenerate your thumbnails. Otherwise you will have odd sizes scaled to whatever you’ve recently specified. There is a handy plugin that can help you out with that.

Regenerate Thumbnails: http://wordpress.org/extend/plugins/regenerate-thumbnails/

2. wp_get_attachment_link

Every time you upload or insert images into a post, they will appear in the post’s attachments (or Gallery tab in the Upload/Insert pop-up). You can grab all of these images and select a thumbnail size using a combination of get_posts and wp_get_attachment_link.

$args = array(
‘post_type’ => ‘attachment’,
‘numberposts’ => -1,
‘post_status’ => NULL,
‘post_parent’ => $post->ID,
‘exclude’ => get_post_thumbnail_id()
);

$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo wp_get_attachment_link($attachment->ID, ‘media-thumb’);
}
}

In this code we simply echo’d the attachment, but by storing it in a variable instead you can just imagine how it’d be simple to reorganize this into a gallery or image grid.

Preventing Duplicate Facebook Comments in WordPress or Other Dynamic Pages

Have you tried using Facebook Comments on your WordPress or dynamic website only to find that every comment is the same?

The solution is simple. In your embed code, make sure that the XID is set to a unique ID, blank, or “”. When this is filled in, Facebook uses it to identify your specific comment box in case you want to place it in multiple locations and maintain the same comments. If you leave it blank, Facebook will use the URL of the page as the box’s unique identifier, meaning that every page will get a different comment box.

<div id=”fb-root”></div><script src=”http://connect.facebook.net/en_US/all.js#appId=0000000000000&amp;amp;xfbml=1″></script>
<fb:comments xid=”” numposts=”10″ width=”640″ publish_feed=”true”></fb:comments>