Today I will show you how you can post to Facebook pages with the identity of those pages. One of my readers came up with this question and truth is it is useful if you have various pages to administer.

How does it work?

  • As usual you need to first install a copy of the Facebook PHP SDK to authenticate user/app/permissions.
  • Then you need to make sure you have the manage_pages permission
  •    // Login or logout url will be needed depending on current user state.
       if ($user) {
        $logoutUrl = $facebook->getLogoutUrl();
       } else {
        $loginUrl = $facebook->getLoginUrl(
      			array('scope'=>'manage_pages,publish_stream')
        );
       }
    
  • Note that once you have the manage_pages permission each page requires its own access code, which you can see below:
  •    ..
       		<select name="page_to">
       			<?php
       			$result = $facebook->api("/me/accounts");
       			foreach($result["data"] as $page) { 
       				if($page["category"] == "Application") {
       					continue; // app pages dont work for this exercise
       				}
       				echo '<option value="'.$page["id"].'" ';
       				if ($page["id"] == $page_to) {
       					echo ' selected="selected"';
       				}
       				echo '>'. $page["name"] . '</option>' ;
       			}   
       			?>
       		</select>
       ..
    

Try it yourself

Go to this page and log in with FB, you will be asked to grant the "manage_pages" permission. Then the page shows a list of all your Facebook pages with an option to post to each one of them.

So this is a quick and easy way to administer all your pages. You can see this in action in the video demo below:

Code

The code can be downloaded here - after putting it on your domain, you need to add your URL your FB App configuration page. Then you need to update "your app id" and "your app secret" with the App codes.


Bob Belderbos

Software Developer, Pythonista, Data Geek, Student of Life. About me