WordPress
Tutorial

How to Create a Custom WordPress Plugin

Need to add custom functionality like analytics or session replay to your WordPress site? Learn how to build a simple plugin to do exactly that, the right way.

8 min read
By LogNroll Team

WordPress powers over 40% of the web, and its plugin architecture is a big reason why. Writing a plugin is often the cleanest way to add third-party integrations, ensuring your changes persist even if you switch themes.

In this guide, we'll walk through creating a basic WordPress plugin. As a practical example, we will build a plugin that integrates the LogNroll Session Replay script into your website. This same pattern can be used for Google Analytics, Facebook Pixel, or any other JavaScript snippet.

Why Create a Plugin?

You might be tempted to edit your theme's header.php file directly to add scripts. However, creating a plugin has significant advantages:

  • Theme Independence: If you change your theme, your scripts disappear if they were in header.php. A plugin keeps them active.
  • Safe Updates: Updating a theme overwrites manual changes. Plugins live separately.
  • Portability: You can easily reuse your plugin on other websites.

Step 1: Create the Plugin Folder

Navigate to your WordPress installation directory. You need to access/wp-content/plugins/.

Create a new folder named lognroll-integration.

Step 2: Create the Plugin File

Inside that folder, create a PHP file with the same name: lognroll-integration.php.

Open this file in your text editor. Every WordPress plugin starts with a standard comment header that tells WordPress about the plugin.

<?php
/**
 * Plugin Name: LogNroll High Performance Replay
 * Plugin URI: https://lognroll.com
 * Description: Seamlessly integrates LogNroll session replay into your WordPress site.
 * Version: 1.0
 * Author: Your Name
 * License: GPL2
 */

Step 3: Inject the Script

To add a script to the <head> of your website, we use the wp_head action hook. We will define a PHP function that outputs our JavaScript code, and then attach that function to the hook.

Here is the complete code for our LogNroll integration plugin:

lognroll-integration.php
<?php
/**
 * Plugin Name: LogNroll High Performance Replay
 * Description: Seamlessly integrates LogNroll session replay into your WordPress site.
 * Version: 1.0
 * Author: LogNroll Fan
 */

// Prevent direct access to this file
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

function add_lognroll_head_script() {
    ?>
    <!-- LogNroll Integration Start -->
    <script src="https://logger.lognroll.com/jslib.lnr.1.0.1.js"></script>
    <script>
        // Replace 'YOUR_COMPANY_ID' with your actual Project ID from LogNroll dashboard
        window.lnr.initSession('YOUR_COMPANY_ID', {});
        
        // Optional: Identify user if logged in (WordPress specific example)
        <?php if ( is_user_logged_in() ): ?>
            <?php 
                $current_user = wp_get_current_user();
                $user_data = array(
                    'name' => $current_user->display_name,
                    'email' => $current_user->user_email
                );
            ?>
            window.lnr.identifyUser('<?php echo esc_js($current_user->ID); ?>', <?php echo json_encode($user_data); ?>);
        <?php endif; ?>
    </script>
    <!-- LogNroll Integration End -->
    <?php
}

// Hook into the 'wp_head' action to insert script in the <head> section
add_action('wp_head', 'add_lognroll_head_script');

Step 4: Activate and Test

  1. Save your lognroll-integration.php file.
  2. Log in to your WordPress Admin Dashboard.
  3. Go to Plugins > Installed Plugins.
  4. You should see "LogNroll High Performance Replay" in the list.
  5. Click Activate.

That's it! Visit your website and verify the installation. You can inspect the page source (Right Click > View Page Source) and search for "lognroll" to confirm the script is loading.

Additional Resources

Want to dive deeper? Check out these official resources to master WordPress plugin development:

Conclusion

Creating a WordPress plugin is a powerful skill that allows you to extend your site safely and cleanly. Whether you are adding analytics, custom post types, or complex functionality, a plugin is usually the best place for your code.

Ready to track your WordPress users?