我创建了一个带有add_shortcode过滤器的wordpress插件,该过滤器向我们的系统发送api调用并从服务器检索文档。但是,我注意到在编辑帖子或页面时,即使在管理区域也会触发。因此,当我在短代码中键入值时,它会不断与服务器通信。
除非发布在帖子/页面中,否则如何停止短代码触发api调用函数?
以下评论让我走上正轨,所以我想为你们添加更多信息。
我正在使用WordPress Boilerplate。我将add_shortcode过滤器添加到插件库中,并在define_public_hook中注册add_shortcode:
$this->loader->add_shortcode( 'my_plugin', $plugin_public, 'my_shortcode', 10, 2 );
然后在my-plugin-public-display.php中我添加了这些函数(我将它缩写为......):
public function api_get_as_json( $controller, $slug, $action, $params, $endpoint ) {
if ( null == $params ) {
$params = array();
}
// Create URL with params
$url = $endpoint . $controller . $slug . $action . '?' . http_build_query($params);
// echo $url;
// Use curl to make the query
$ch = curl_init();
curl_setopt_array(
$ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true
)
);
$output = curl_exec($ch);
// Decode output into an array
$json_data = json_decode( $output, true );
curl_close( $ch );
return $json_data;
}
/**
* Shortcodes
*
* @since 1.0.0
*/
public function my_shortcode( $atts ) {
//turn on output buffering to capture script output
ob_start();
// Get attributes from shortcode
$attributes = shortcode_atts( array(
...
), $atts, 'my_plugin' );
$my_array = //getting the array attributes here...
/* Send API calls to WhoTeaches, get profile and Packages */
$result = $this->api_get_as_json('controller/', null, 'action', $my_array, $host . "domain.com/api/v1/");
// Load the view
include (plugin_dir_path( dirname( __FILE__ ) ) . 'public/partials/my-plugin-public-display.php');
// Get content from buffer and return it
$output = ob_get_clean();
return $output;
// Clear the buffer
ob_end_flush();
}
我需要在这里添加或编辑内容吗?