最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to only enqueue script if it's a post

programmeradmin4浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论