My alternative WordPress Plugin

Hi, all,

I’d be interested in feedback/advice. I usually limit myself to HTML and CSS and tweaking snippets of PHP and JavaScript.

But I had some particular requirements for a plugin for Pirsch on my sites so ChatGPT and I created this.

My requirements are:

  • Simple plugin so I can easily update the Pirsch code (for example, change all my sites to the latest pa.js
  • Ignore page views if the visitor is logged in
  • display a nag message if I haven’t set the Identification Code

And this is what we came up with:

 * Plugin Name: fm Pirsch
 * Plugin URI:
 * Description: Adds Pirsch Analytics code for non-logged-in visitors
 * Version: 1.00
 * Author: Jonathan Gwyer
 * Author URI:

// Add Settings Page
function fmpirsch_settings_page() {
        'fm Pirsch Settings',
        'fm Pirsch',
add_action('admin_menu', 'fmpirsch_settings_page');

function fmpirsch_settings_page_content() {
    <div class="wrap">
        <h2>fm Pirsch Settings</h2>
        <form method="post" action="options.php">
            <?php settings_fields('fmpirsch_settings_group'); ?>
            <?php do_settings_sections('fmpirsch-settings'); ?>
            <?php submit_button(); ?>

// Register Settings
function fmpirsch_register_settings() {
    register_setting('fmpirsch_settings_group', 'fmpirsch_identification_code', 'fmpirsch_sanitize_options');

        'Identification Code Settings',

        'Identification Code:',
add_action('admin_init', 'fmpirsch_register_settings');

function fmpirsch_section_callback() {
    echo '<p>Enter your Identification Code below:</p>';

function fmpirsch_identification_code_field_callback() {
    $options = get_option('fmpirsch_identification_code');
    echo '<input type="text" id="fmpirsch_identification_code_field" name="fmpirsch_identification_code[identification_code]" value="' . esc_attr($options['identification_code']) . '" />';

function fmpirsch_sanitize_options($input) {
    $sanitized_input = array();
    if (isset($input['identification_code'])) {
        $sanitized_input['identification_code'] = sanitize_text_field($input['identification_code']);
    return $sanitized_input;

// Insert JavaScript Code
function fmpirsch_insert_code() {
    $options = get_option('fmpirsch_identification_code');
    if (!is_user_logged_in() && !empty($options['identification_code'])) {
            <script defer src=""
    data-code="<?php echo $options['identification_code']; ?>"></script>
add_action('wp_head', 'fmpirsch_insert_code');

// Check if Identification Code is set
function fmpirsch_check_identification_code() {
    $options = get_option('fmpirsch_identification_code');
    if (is_user_logged_in() && empty($options['identification_code'])) {
        add_action('admin_notices', 'fmpirsch_display_identification_code_notice');
add_action('admin_init', 'fmpirsch_check_identification_code');

// Display notice if Identification Code is not set
function fmpirsch_display_identification_code_notice() {
    <div class="notice notice-error">
        <p><?php esc_html_e('fm Pirsch Identification Code is not set. Please set the Identification Code in the settings.'); ?></p>

Any comments? Want to use it yourself or tidy it up so it doesn’t look like it was generated by AI?

Be nice :slight_smile:

1 Like

Thank you for making this! I think it looks really good :slight_smile: