我已经使用BoilerPlate创建了一个简单的自定义WordPress插件来调用Laravel应用程序接口。
我已经配置好了所有的东西,没有错误。但我不知道如何将返回的数据显示到页面或类似的内容中。
public function getRecords($id)
{
$cache_name = ‘records_’ . $id;
$cache = new FileStore(new Filesystem($cache_name . ‘.txt’), __DIR__ . ‘/cache’);
// If cache exists
if ($cache->get($cache_name)) {
return $cache->get($cache_name);
} else {
try {
// Try to get records
$client = new GuzzleHttp(‘https://api.hello.com/records/1399394access_token=w3r2232r’);
$request = $client->get()->send();
$records = json_decode($request->getBody(), true);
// Save records in cache
$cache->put($cache_name, $records, 600);
return $records;
} catch (GuzzleHttpExceptionBadResponseException $e) {
$raw_response = explode(“n”, $e->getResponse());
throw new IDPException(end($raw_response));
}
}
}
例如,在本例中,如何将返回的变量'return $records;‘显示到主页?这只是一个测试,看看它是如何工作的。
这是一个类:
类Plugin_Name_Public {
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
private $plugin_name;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $plugin_name The name of the plugin.
* @param string $version The version of this plugin.
*/
public function __construct( $plugin_name, $version ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
$capsule = new Capsule;
$capsule->setAsGlobal();
$capsule->bootEloquent();
}
提前谢谢你!
发布于 2018-01-18 19:11:06
有几种方法,但最简单的方法之一是在插件样板中创建一个shortcode
并执行请求。
我不熟悉您使用的样板文件,但在典型的方式中,类内部的内容应该是这样的。
class Hello_Plugin{
public function my_shortcode_func($atts,$content = ''){
// Make request and return it
return "Hello";
}
public function __construct( $plugin_name, $version ) {
add_shortcode("my_shortcode",array($this,"my_shortcode_func"));
}
}
所以在你把[my_shortcode]
放在一个特定的页面之后。
注意:一些样板将短码初始化放在__construct
函数之外,因此通常尝试遵循样板的标准。
其它方法包括具有用于访问插件信息的公共对象或函数的模板文件。
https://stackoverflow.com/questions/48328442
复制