Support » Developing with WordPress » How do I make the plugin only execute when it is used?

  • I developed a plugin to play self-hosted asciinema videos. The plugin adds the CSS and JavaScript files required for the player to run on each page, regardless of whether the plugin is used or not.

    How do I make the plugin to detect when to add the style and CSS files required for the player to work. The video is inserted using the following tag: <asciinema-player src=”asciicast.json”></asciinema-player>. Can I check for that tag in the post and if it’s used don’t add the CSS and JavaScript files?

    I’ll show you the code I used because it’s very short, and you can better understand me. Here it is:

    defined('ABSPATH') or die('No script kiddies please!');
    
    function enqueue_scripts() {
        if (!is_admin()) {
            wp_register_script('asciinema-player-js', plugins_url('asciinema-player.js', __FILE__), array(), $ver = '2.4.0', true);
            wp_register_style('asciinema-player-css', plugins_url('asciinema-player.css', __FILE__), array(), $ver = '2.4.0');
    
            wp_enqueue_script('asciinema-player-js');
            wp_enqueue_style('asciinema-player-css');
        }
    }
    
    function execute() {
        add_action('wp_enqueue_scripts', 'enqueue_scripts');
    }
    
    execute();
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Steve Stern (sterndata)

    (@sterndata)

    Forum Moderator & Support Team Rep

    if the player is embedded (by the user) using a shortcode, you can use has_shortcode to determine if it’s on the page and then load your stuff based n the return of that function.

    https://codex.wordpress.org/Function_Reference/has_shortcode

    Thread Starter Jorge Maldonado Ventura

    (@jorgesumle)

    Thanks for your suggestion Steve. But in this case, I want to embed the player using an HTML tag (<asciinema-player src=”asciicast.json”></asciinema-player>), which is the default way to use asciinema-player. Any way to do that?

    Moderator Steve Stern (sterndata)

    (@sterndata)

    Forum Moderator & Support Team Rep

    Write a shortcode which creates that tag, then you can use has_shortcode.

    Moderator bcworkz

    (@bcworkz)

    has_shortcode() merely does a preg_match on content to identify if shortcode tags exist. You could do the same thing, search content for “<asciinema-player”. Then there’s no need for making a shortcode, though that is a perfectly reasonable approach as well, unless there is a lot of existing content with the asciinema tags already.

    Thread Starter Jorge Maldonado Ventura

    (@jorgesumle)

    I’m trying to use your solution, but I don’t know how to get the content. How can I do that? get_the_content() doesn’t work, I don’t know why. When I echo get_the_content() it doesn’t print anything. This is what I have so far…

    /**
     * Enqueue asciinema-player files.
     */
    function enqueue_player() {
        wp_register_script('asciinema-player-js', plugins_url('asciinema-player.js', __FILE__), array(), $ver = '2.4.0', true);
        wp_register_style('asciinema-player-css', plugins_url('asciinema-player.css', __FILE__), array(), $ver = '2.4.0');
    
        wp_enqueue_script('asciinema-player-js');
        wp_enqueue_style('asciinema-player-css');
    }
    
    /**
     * Whether the passed content contains the <asciinema-player> tag.
     *
     * @param string $content Content to search for the <asciinema-player> tag.
     *
     * @return bool Whether the passed content contains the <asciinema-player> tag.
     */
    function has_asciinema_player_tag($content) {
        $asciinema_player_tag = '<asciinema-player>';
        if (false === strpos($content, '<')) {
            echo $content;
            return false;
        }
    
        preg_match_all('/<asciinema-player>/', $content, $matches);
        if (empty($matches))
            return false;
        else
            return true;
    }
    
    /**
     * Enqueue asciinema-player files if the asciinema-player tag is used.
     */
    function execute() {
        if (has_asciinema_player_tag(get_the_content())) {
            add_action('wp_enqueue_scripts', 'enqueue_player');
        }
    }
    
    execute();
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How do I make the plugin only execute when it is used?’ is closed to new replies.