To create a wordpress plugin navigate to the installation folder of the wordpress, then wp-content -> plugins folder create a folder with your desired name of the plugin “your-plugin”, then create a php file with same name “your-plugin.php” and add the following code in it.
<?php
/**
* Plugin Name: Your Plugin Name
* Plugin URI: https://www.yourwebsite.com/
* Description: Your Plugin Description
* Version: 1.0
* Author: Your Author Name
* Author URI: https://www.yourwebsite.com
*/
This is the basic code for a wordpress plugin. After that you can add your plugins php functionality like this. I am creating a legacy analytics plugin for the example
<?php
/**
* Plugin Name: Your Plugin Name
* Plugin URI: https://www.yourwebsite.com/
* Description: Your Plugin Description
* Version: 1.0
* Author: Your Author Name
* Author URI: https://www.yourwebsite.com
*/
add_action( 'wp_head', 'add_to_head' );
function add_to_head() {
echo '';
}
The above code should add the analytics.js to the wordpress head. Then you can add the Analytics Legacy code to the footer like this
<?php
/**
* Plugin Name: Your Plugin Name
* Plugin URI: https://www.yourwebsite.com/
* Description: Your Plugin Description
* Version: 1.0
* Author: Your Author Name
* Author URI: https://www.yourwebsite.com
*/
add_action( 'wp_head', 'add_to_head' );
function add_to_head() {
echo '';
}
add_action('wp_footer', 'add_to_footer');
function add_to_footer(){
echo "
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-Your Analytics Number', 'auto');
ga('send', 'pageview');
</script>
";
}