#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Posts
    55
    Rep Power
    7

    Best way to integrate a CMS with Facebook registration?


    Im currently designing a website which will do a variation of things.

    1) It will give articles to users which reside in different categories.
    2) Provide users a section to sign up to discounts.
    3) A section where users can get information about products etc
    4) Provide target content for the user based upon a registration field.

    Now, I have experience with Wordpress so the obvious solution for me is that Wordpress would work for sections 1 + 3.

    What I want is for the users to be able to register with Facebook for my site with a custom field. This custom field will be able to provide the user with targeted content for section 4.

    So far I have seen Facebook plugins which register the user as a Wordpress user, but this isn't what I am after.

    What do you think the best solution for this is? Use Wordpress with a custom database that stores the Facebook information? Or is there a plugin that allows me to do just this?

    I also might want to integrate a forum later on which ideally would use the Facebook registration information.
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    What information exactly are you trying to get from Facebook?
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Posts
    55
    Rep Power
    7
    Well I want the user to register with Facebook.

    In the form there is a custom field, lets called it CF1.

    What I want to be able to keep in my database is the Unique ID from facebook for that user and CF1. Maybe a few other things such as their name a profile picture - however it is my understanding that this is dynamically done and doesn't need to be stored in my database?

    This will enable me to target my content towards a user based on the custom field CF1
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    The feature you're looking for is called "Facebook Connect". This will allow you to place a button on your website, and when the user presses that button it will open a Facebook login form for the user to authenticate using their Facebook account. After authentication, you can use Facebook's JavaScript or PHP APIs to fetch information about the user, including their Facebook user ID. Last time I checked, you were only permitted to store Facebook information for 24 hours, but I don't think this applies to user IDs.

    Anyway, your basic workflow is going to be:
    1) Use Facebook Connect to authenticate the user with Facebook and get their ID.
    2) Send the user to a registration form to fill out CF1.
    3) Save that information to the database.

    If your application is going to pull data from Facebook like profile pictures then you also need to send the user to Facebook after step (1) to authorize your application and give it permission to do so.

    A custom table in the Wordpress database will work just fine for storing this information. I don't really know Wordpress plugins well, but I doubt there will be one that does exactly what you need. You could probably modify one of the existing ones that creates user accounts though. Remove the user creation part and replace it with your custom registration process. These plugins must also get the user's Facebook ID, so there is some overlap in functionality.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Posts
    55
    Rep Power
    7
    Thanks, i've managed to do it! However I have an issue while trying to implement it into my Wordpress theme.

    I'm getting the error:

    Fatal error: Cannot redeclare retrieve_fields() (previously declared in /home/students/public_html/wp/wp-content/themes/studentsso/functions.php:4) in /home/students/public_html/wp/wp-content/themes/studentsso/functions.php on line 7
    and lines 4-7 are:

    Code:
    function retrieve_fields($sf) {
    
        return json_encode($sf);
    }


    Now, this all works when its not broken down into Wordpress formats.

    In my header file I have:
    PHP Code:
    <?php
    require 'functions.php';
    require 
    'src/facebook.php';
    $facebook = new Facebook(array(
                
    'appId' => $config["fb_app_id"],
                
    'secret' => $config["fb_app_secret"],
                
    'cookie' => true,
            ));

    $congratulations FALSE;
    // capture the $_REQUEST coming to this page and check if it's a new user
    check_registration($facebook,$config["fb_fields"]);

    $session $facebook->getSession();

    $me null;
    // Session based API call.
    if ($session) {
        try {
            
    $uid $facebook->getUser();
            
    // We can use a Graph API call from the PHP-SDK
            //$me = $facebook->api('/me');
            // Or just use our Database!
            
    $me get_user_by_id($uid);
        } catch (
    FacebookApiException $e) {
            
    error_log($e);
        }
    }

    // login or logout url will be needed depending on current user state.
    if ($me) {
        
    $logoutUrl $facebook->getLogoutUrl();
    } else {
        
    // we are not using this url in our example
        
    $loginUrl $facebook->getLoginUrl();
    }

    ?>
    <?php
    /**
     * The Header for our theme.
     *
     * Displays all of the <head> section and everything up till <div id="main">
     *
     * @package WordPress
     * @subpackage Students
     * @since Students 1.0
     */
    ?>


    <div class="login-status">
                <?php if ($me): ?>
                    <div class="profile">
                        <img class="profile-img" src="https://graph.facebook.com/<?php echo $uid?>/picture" alt="" />
                        <span><?php echo $me['name']; ?></span>
                        <a href="<?php echo $logoutUrl?>">
                            <img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif" />
                        </a>
                    </div>
                <?php else: ?>
                        <fb:login-button registration-url="<?php echo $config["base_url"]; ?>register.php" />
                <?php endif ?>
                </div>

    In the index, I call in the Header and Footer files

    And in the footer I have:

    Code:
    <div id="fb-root"></div>
    <script type="text/javascript">
        window.fbAsyncInit = function() {
                FB.init({
                        appId   : '<?php echo $facebook->getAppId(); ?>',
                        session : <?php echo json_encode($session); ?>, // don't refetch the session when PHP already has it
                        status  : true, // check login status
                        cookie  : true, // enable cookies to allow the server to access the session
                        xfbml   : true // parse XFBML
                });
    
                // whenever the user logs in, we refresh the page
                FB.Event.subscribe('auth.login', function() {
                        window.location.reload();
                });
        };
    
        (function() {
                var e = document.createElement('script');
                e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
                e.async = true;
                document.getElementById('fb-root').appendChild(e);
        }());
    </script>


    Like I said, without this being broken into header, index and footer it works perfectly. However now I get that error when its split.

    Any ideas?
  10. #6
  11. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    It looks like you're including or requiring the functions.php file more than once.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Posts
    55
    Rep Power
    7
    Originally Posted by E-Oreo
    It looks like you're including or requiring the functions.php file more than once.
    Well I would have thought have thought that too but i've gone over and over it and its not.

    The include functions.php line is in the wordpress header though and I think this is the problem. Does Wordpress reload headers or something that could cause this?

    However using require_once fixes it, I dont know if this will affect the script

IMN logo majestic logo threadwatch logo seochat tools logo