WordPress社交登录>发布到用户的Facebook墙

时间:2015-02-04 作者:TensGardo

我正在尝试发布到当前登录的Facebook用户的墙/时间线。

我在插件作者github文档中发现:http://miled.github.io/wordpress-social-login/developer-api-apis.html

我把第二段代码(Facebook代码)放在我的函数中。php文件。

然后,在我的一个wordpress模板文件上,我执行了以下操作:

apply_filters(\'wsl_hook_process_login_before_wp_set_auth_cookie\', get_current_user_id(), "Facebook", ???);
问题是,我不知道我需要用什么来代替$hybridauth_user_profile

当我var\\u转储该变量时:

global $hybridauth_user_profile;
echo "HELLO WORLD: ";
var_dump($hybridauth_user_profile);
它在我的模板文件(位于wp-content/themes/mytheme文件夹)中返回为NULL。

我也试着access token 因此,我可以直接从以下文档中使用Facebook API:http://miled.github.io/wordpress-social-login/developer-api-snippets.html

不幸的是,同样的问题,不知道从哪里可以得到这个$hybridauth\\u user\\u profile变量。

1 个回复
SO网友:David Lawrence

我不是这方面的专家,但有一点:

阅读一些代码并对过滤器进行全局搜索后,它根本不是过滤器,唯一调用过滤器的时间是在do\\u操作中

    // 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 );
在这种情况下,您可以在插件文件或函数中执行以下操作。主题的php文件。

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 );
注意:我使用PHP 5内联函数,如果您没有PHP 5或此特定代码返回错误,请尝试删除函数部分并将其设置为专用函数,然后用专用函数名称add\\u action(\'hook\',\'function\',10,3)vs add\\u action(\'hook\',function(){},10,3)的字符串替换函数部分

此示例向用户配置文件发布“这是一条测试消息”,状态标记为您的应用程序名称。

在这里可以找到更多。

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

这是一篇很老的帖子,但这些信息可以帮助很多登陆这里的人。

请记住,您需要用户的权限才能发布,否则您将从FB或任何其他提供商处获得错误代码。您不能在没有许可的情况下发布,您可以通过与以下操作完全相反的方式设置范围:

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 );
与其将范围缩小为一个范围,不如:

$provider_scope .= \', scope1, scope2, scope3\'
return $provider_scope;
每次用户登录社交网络时都会使用此范围,因此,如果您没有权限,并且在用户登录后更改范围,则需要注销并重新登录才能授予您权限。

您可以通过更改wp配置上的salts来强制所有人注销。php文件,但这可能不是最佳做法。我相信wordpress 4.1有一个功能,只需在谷歌上搜索一下如何在下一个页面加载时重新授权每个人。

我不能绝对肯定这一点,我还没有测试过它,但你会这样做,当你想张贴在其他行动呼吁在网站上。

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 );
这个特殊的代码将加载facebook适配器并在获得许可的情况下发布到他们的facebook墙上,你可以全力以赴,通过get\\u comment\\u meta($id,\'rating\',true)在状态帖子中显示评级和五星评级;

还请记住,仅仅因为你请求了在他们的墙上张贴状态的权限,并不意味着他们必须给你,这就是尝试捕捉的目的,你永远不知道什么时候你有或没有发布状态的权限。如果他们拒绝权限,它将始终捕获$e->getMessage()。尝试做一些类似于日志系统的事情,或者如果不想捕捉错误,只返回null。

希望这有帮助。

结束

相关推荐

apply_filters() function

我用过apply_filters() 从WordPress帖子中检索内容时,如:$content=$query_val->post_content; $content = apply_filters( \'the_content\', $content ); 当我使用apply_filters() 这个apostrophe( \' ) 在我的文本中显示了一些字符。在我移除后apply_filters() 它显示正确。所以请解释清楚!!它在做什么?我已引用此链接Referal_lin