I created a wordpress plugin with a add_shortcode filter that sends an api call to our system and retrieves documents from the server. However, I noticed that it fires even in the Admin area while I am editing a post or a page. So as I type the value inside the shortcode, it continuously communicates with the server.
How do I stop the shortcode from firing the api call function unless it was published in the post/page?
The comments below are putting me on the right track, so I want to add more info for you guys.
I am using the WordPress Boilerplate. I added the add_shortcode filter to the plugin library and I register the add_shortcode in the define_public_hook:
$this->loader->add_shortcode( 'my_plugin', $plugin_public, 'my_shortcode', 10, 2 );
then in my-plugin-public-display.php I add the functions (I'll abbreviate it...):
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();
}
Do I need to add or edit something here?