如何使用 Boiler 插件

How to user Boiler plugin

我已经使用 BoilerPlate 创建了一个简单的自定义 WordPress 插件来调用 Laravel 应用程序 API。

我都配置好了,没有错误。但我不知道如何将返回的数据显示到页面或类似的东西中。

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;'显示到首页?只是一个测试,看看它是如何工作的。

这是class:

class 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();
}

提前致谢!

有几种方法,但最简单的方法之一是在插件样板中创建一个 shortcode 并执行请求。

我不熟悉你使用的样板文件,但在 class 中的典型方式看起来像这样。

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 函数之外,因此通常尽量遵循样板文件的标准。

其他方法包括使用带有 public 对象或函数的模板文件来访问插件信息。