I'm building a plugin and I would like to only enqueue a script that resides in my API if it's a post. Example:
<?php
/**
* The public-facing functionality of the plugin.
*
* Defines the plugin name, version, and two examples hooks for how to
* enqueue the public-facing stylesheet and JavaScript.
*/
class PluginName_Public {
/**
* The id of this plugin.
*
* @var string $Plugin The id of this plugin.
*/
private $Plugin;
/**
* The version of this plugin.
*
* @var string $version The current version of this plugin.
*/
private $version;
/**
* Initialize the class and set its properties.
*
* @param $Plugin The id of the plugin.
* @param $version The version of this plugin.
*/
public function __construct( string $Plugin, string $version ) {
$this->Plugin = $Plugin;
$this->version = $version;
}
/**
* Register the JavaScript for the public-facing side of the site.
*/
public function enqueue_scripts() {
$this->enqueue_tracker_script();
}
private function enqueue_tracker_script() {
// $post_id = 0
// if $post is defined, enqueue it:
wp_enqueue_script(
$this->Plugin,
"//myapi?&post_id=$post_id",
array(),
false,
true
);
}
}
I don't have a lot of exposure to WordPress plugin development, so I'm stuck - I didn't find a way of doing in the docs what I would like to do.