1

I'm trying to post to a currently logged in Facebook user's wall/timeline.

I found this on the plugin authors github documentation: http://miled.github.io/wordpress-social-login/developer-api-apis.html

I put the second snippet of code (the Facebook code) in my functions.php file.

Then on one of my wordpress template files, I did this:

apply_filters('wsl_hook_process_login_before_wp_set_auth_cookie', get_current_user_id(), "Facebook", ???);

The thing is, I have no idea what I need to put in place of $hybridauth_user_profile

When I var_dump that variable:

global $hybridauth_user_profile;
echo "HELLO WORLD: ";
var_dump($hybridauth_user_profile);

It comes back as NULL in my template file (located in wp-content/themes/mytheme folder).

I also tried to just get the access token so I can just use the Facebook API directly from this document: http://miled.github.io/wordpress-social-login/developer-api-snippets.html

Unfortunately same problem, no idea where to get this $hybridauth_user_profile variable.

1
  • Why don't you ask to the developer of the plugin? We can not know what he/she coded and studty his/her code is off-topic here.
    – cybmeta
    Commented Feb 4, 2015 at 18:08

1 Answer 1

2

Im no expert on this but here goes:

After reading some code and doing a global search of the filter, its not a filter at all, the only time that filter is called is in a do_action

    // HOOKABLE: This action runs just before logging the user in (before creating a WP cookie)
    do_action( "wsl_hook_process_login_before_wp_set_auth_cookie", $user_id, $provider, $hybridauth_user_profile );

    // DEPRECIATED: as of 2.2.3
    // do_action( 'wsl_hook_process_login_before_set_auth_cookie', $user_id, $provider, $hybridauth_user_profile );

In this case you would do the following either in a plugin file or in your functions.php file of your theme.

add_action( 'wsl_hook_process_login_before_wp_set_auth_cookie', function( $user_id, $provider, $hybridauth_user_profile ){
    // $user_id is the ID of the WP User Object or in other words the users ID on your site.
    // $provider is the provider in which you are working with
    // $hybridauth_user_profile is a instance of the social media profile information: IE: $hybridauth_user_profile->email. This variable stores all profile information for the adapter, such as facebook.
    if( 'Facebook' != $provider ){
        return;
    }

    include_once( WORDPRESS_SOCIAL_LOGIN_ABS_PATH . '/hybridauth/Hybrid/Auth.php' );

    try
    {
        $provider = Hybrid_Auth::getAdapter( 'Facebook' ); 

        # https://developers.facebook.com/docs/graph-api/reference/v2.2/user/feed
        $response = $provider->api()->post( 'https://graph.facebook.com/v2.0/me/feed', array(
            'message' => 'This is a test message',
        ));
    }
    catch( Exception $e )
    {
        // Do some logging or something, in this example we echo the error.
        echo "Ooophs, we got an error: " . $e->getMessage();
    }
}, 10, 3 );

Note I am using PHP 5 to inline the function, if you dont have PHP 5 or this particular code returns an error, try removing the function part and making it a dedicated function, then replace the function part with a string of the dedicated function name add_action( 'hook', 'function', 10, 3 ) vs add_action( 'hook', function(){}, 10, 3 )

This example posts "This is a test message" to the users profile with the status labeled with your apps name.

More can be found here.

http://miled.github.io/wordpress-social-login/developer-api-apis.html

This is a pretty old post but this information can help alot of people who land here.

Remember you need the permission from the user to post or youll get an error code from FB or any other provider. You cant just post without permission, you can set scopes by doing the exact opposite of:

function wsl_lower_default_permissons( $provider_scope, $provider ){
    if( 'facebook' == strtolower( $provider ) ){
        $provider_scope = 'email'; // should not be empty or it will be overwritten
    }

    if( 'google' == strtolower( $provider ) ){
        $provider_scope = 'profile'; // should not be empty or it will be overwritten
    }

    return $provider_scope;
}

add_filter( 'wsl_hook_alter_provider_scope', 'wsl_lower_default_permissons', 10, 2 );

Instead of reducing scope to one scope you would:

$provider_scope .= ', scope1, scope2, scope3'
return $provider_scope;

This scope will be used everytime a user logs in with the social network, so if you dont have permission and change the scope after a user logs in they will need to log out and log back in to give you permission.

You can force everyone to log out by changing salts on the wp-config.php file but this may or may not be best practice. I believe with wordpress 4.1 you have a function, just do a little google searching on how to reauth everyone on the next page load.

I cant be absolutely sure on this and I havent tested it but you would do something like this when wanting to post on other actions called on the site.

add_action( 'wp_insert_comment', function( $id, $comment ){

    include_once( WORDPRESS_SOCIAL_LOGIN_ABS_PATH . '/hybridauth/Hybrid/Auth.php' );

    try
    {
        $provider = Hybrid_Auth::getAdapter( 'Facebook' ); 

        # https://developers.facebook.com/docs/graph-api/reference/v2.2/user/feed
        $response = $provider->api()->post( 'https://graph.facebook.com/v2.0/me/feed', array(
            'message' => "I just posted on this cool website, heres what i said:\r\n" . $comment->comment_content,
        ));
    }
    catch( Exception $e )
    {
        // Do some logging or something, in this example we echo the error.
        echo "Ooophs, we got an error: " . $e->getMessage();
    }
}, 10, 2 );

This particular code would load facebook adapter and post to their facebook wall with given permission, you could go all out and do things like 5 star ratings and displaying the rating in the status post, with get_comment_meta( $id, 'rating', true );

Also remember just because you requested the permission to post statuses to their wall, doesnt mean they are required to give it to you, thats the purpose of the try catch, you never know when you do or dont have permission to post. If they deny permission it will always catch with $e->getMessage(). Try doing something like a logging system or just return null if you dont care about catching errors.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.