How to Create a Simple Browser Game with a WordPress Plugin and Security Measures

In this article, we will introduce how to use ChatGPT to create a simple browser game as a WordPress plugin. We will also provide a detailed explanation of security considerations and countermeasures.


目次

Basic Plugin Structure

A WordPress plugin can be created by making a folder within the wp-content/plugins/ directory and placing PHP files inside it.

/wp-content/plugins/my-game-plugin/
    ├── my-game-plugin.php
    ├── assets/
    │   ├── game.js
    │   ├── style.css
    │   └── index.html

my-game-plugin.php (Main Plugin File)

<?php
/*
Plugin Name: My Game Plugin
Description: A plugin to embed a simple browser game
Version: 1.0
Author: Your Name
*/

function my_game_enqueue_scripts() {
    wp_enqueue_script('my-game-script', plugins_url('assets/game.js', __FILE__), array(), null, true);
    wp_enqueue_style('my-game-style', plugins_url('assets/style.css', __FILE__));
}
add_action('wp_enqueue_scripts', 'my_game_enqueue_scripts');

function my_game_shortcode() {
    ob_start();
    ?>
    <div id="game-container">
        <canvas id="gameCanvas"></canvas>
    </div>
    <?php
    return ob_get_clean();
}
add_shortcode('my_game', 'my_game_shortcode');
?>

Activate this plugin and add [my_game] to a post to display the game.


Creating a Simple Clicking Game with JavaScript

The code for this game was generated using ChatGPT. Below is the basic JavaScript code for the game.

assets/game.js

document.addEventListener("DOMContentLoaded", function() {
    const canvas = document.getElementById("gameCanvas");
    const ctx = canvas.getContext("2d");

    canvas.width = 300;
    canvas.height = 300;

    let score = 0;
    let targetX = Math.random() * canvas.width;
    let targetY = Math.random() * canvas.height;
    const targetRadius = 20;

    function drawScore() {
        ctx.fillStyle = "black";
        ctx.font = "20px Arial";
        ctx.fillText("Score: " + score, 10, 30);
    }

    function drawTarget() {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.fillStyle = "red";
        ctx.beginPath();
        ctx.arc(targetX, targetY, targetRadius, 0, Math.PI * 2);
        ctx.fill();
        drawScore();
    }

    function checkClick(event) {
        const rect = canvas.getBoundingClientRect();
        const x = event.clientX - rect.left;
        const y = event.clientY - rect.top;
        const distance = Math.sqrt((x - targetX) ** 2 + (y - targetY) ** 2);

        if (distance < targetRadius) {
            score++;
            targetX = Math.random() * canvas.width;
            targetY = Math.random() * canvas.height;
        }

        drawTarget();
    }

    canvas.addEventListener("click", checkClick);
    drawTarget();
});

assets/style.css

#game-container {
    text-align: center;
    margin: 20px auto;
}
canvas {
    border: 2px solid black;
}

Now the simple clicking game is operational.

[my_game]


Security Considerations and Countermeasures

Even when generating code using ChatGPT, you must pay close attention to security.

XSS (Cross-Site Scripting) Countermeasures

  • Avoid using innerHTML; instead, manipulate the DOM using textContent or setAttribute.
  • If saving scores to the server, sanitize them properly using esc_html() or sanitize_text_field().

Clickjacking Countermeasures

  • Set X-Frame-Options to DENY or SAMEORIGIN.
  • Control it in WordPress using the send_headers hook:
    function my_game_set_headers() {
        header('X-Frame-Options: SAMEORIGIN');
    }
    add_action('send_headers', 'my_game_set_headers');

Score Tampering Prevention

  • There is a possibility that scores could be tampered with via developer tools (e.g., entering score = 9999;).
  • When sending scores to the server, use HMAC or digital signatures to prevent unauthorized data transmission.

Plugin Tampering Prevention

  • Add define('DISALLOW_FILE_EDIT', true); to wp-config.php.
  • Restrict access to the plugin’s assets/ directory using .htaccess:
    <FilesMatch "\.(js|css|html)$">
        Require all denied
    </FilesMatch>

Conclusion

With this approach, we were able to leverage ChatGPT to integrate a simple browser game into WordPress. However, it is important to understand the security risks and implement appropriate measures.

If you plan to expand the game—such as saving scores or adding a ranking system—be sure to pay close attention to server-side security as well!