php - Facebook SDK cannot autoload classes in Wordpress -
i've been searching solution days, , have no clue try or go next.
i'm trying set user token facebook app in custom plugin in wordpress website per documentation:
https://developers.facebook.com/docs/php/gettingstarted
since it's shared host, didn't seem use composer. anyhow, documentation manually installing sdk looked simple enough easier going way, want know going wrong.
so on plugin's front page have this:
<?php ini_set('display_errors', 1); error_reporting(e_all); // debug define('facebook_sdk_v4_src_dir', __dir__ . '/php-graph-sdk/facebook/'); require_once(__dir__ . '/php-graph-sdk/facebook/autoload.php'); if(!isset($_session['fb_access_token'])){ ?> <script> // pretty given facebook, except ajax seems working fine loginwithfacebook = function() { fb.login(function(response) { if (response.authresponse) { // ajax set token in $_session var xmlhttp = new xmlhttprequest(); xmlhttp.onreadystatechange = function() { if(this.readystate == 4){ if(this.status == 200){ alert(this.responsetext); }else{ alert(this.status); } } }; xmlhttp.open("get", "/path-to-plugin/set_token.php", true); xmlhttp.send(); } else { alert('user cancelled login or did not authorize.'); } }); return false; }; window.fbasyncinit = function() { fb.init({ appid: '000-000', cookie: true, // important, it's not enabled default version: 'v2.8' }); }; (function(d, s, id){ var js, fjs = d.getelementsbytagname(s)[0]; if (d.getelementbyid(id)) {return;} js = d.createelement(s); js.id = id; js.src = "//connect.facebook.net/en_us/sdk.js"; fjs.parentnode.insertbefore(js, fjs); }(document, 'script', 'facebook-jssdk')); </script> <p><a href="#" onclick="loginwithfacebook()">log facebook</a></p> <?php }else{ // carry on } ?>
in set_token.php page, there code provided facebook documentation:
$fb = new facebook\facebook([ 'app_id' => '000-000', 'app_secret' => '000-abc-def', 'default_graph_version' => 'v2.8', ]);
there more, understand should run autoloader function , find facebook\facebook class. autoload.php provided facebook, , wasn't modified @ (i removed comments sections here):
if (version_compare(php_version, '5.4.0', '<')) { throw new exception('the facebook sdk requires php version 5.4 or higher.'); } spl_autoload_register(function ($class) { // project-specific namespace prefix $prefix = 'facebook\\'; // backwards compatibility $custombasedir = ''; // @todo v6: remove support 'facebook_sdk_v4_src_dir' if (defined('facebook_sdk_v4_src_dir')) { $custombasedir = facebook_sdk_v4_src_dir; } elseif (defined('facebook_sdk_src_dir')) { $custombasedir = facebook_sdk_src_dir; } // base directory namespace prefix $basedir = $custombasedir ?: __dir__ . '/'; // class use namespace prefix? $len = strlen($prefix); if (strncmp($prefix, $class, $len) !== 0) { // no, move next registered autoloader return; } // relative class name $relativeclass = substr($class, $len); // replace namespace prefix base directory, replace namespace // separators directory separators in relative class name, append // .php $file = rtrim($basedir, '/') . '/' . str_replace('\\', '/', $relativeclass) . '.php'; // if file exists, require if (file_exists($file)) { require $file; } });
when click link on page, run facebook login prompt, fails find facebook\facebook class (as can see in error log file). if add echo('got autoloader');
command in autoload.php before spl_autoload_register
, printed page. found file. if similar inside spl_autoload_register
(i tried die('something')
, throw new exception('something')
), doesn't print , message in log file same. looks function not being registered, maybe wordpress. although wp have documentation on function too:
https://developer.wordpress.org/reference/functions/spl_autoload_register/
finally, if run global $_wp_spl_autoloaders; print_r($_wp_spl_autoloaders);
right after require
command in front page, doesn't print anything.
like said, don't know else look. can help?
Comments
Post a Comment