Include central function that applies for all my Wordpress themes

I have a Wordpress network with a big amount of different themes and even more pages using those themes.

Now, I have a PHP function (for some tracking) that I want to run every time a page from my network is accessed. I don't want to edit all my themes, so I am looking for a central file where I can have this new function.

First thing in my mind was the index.php in the root folder of the WP installation. This would probalby work but does not seem the finest solution.

All my themes are using the wp_head function maybe this can help somehow?

Is there any other place where I can add my tracking code and make sure that it is run on every page access?

Solutions

You should create a plugin to do this. In that plugin you will have that function and add the action to the wp_head hook. This will keep it working with all of your themes and easy to change without having to go into all your themes.

The code will look something like this

/**
* Plugin Name: Tracking Function
* Description: Lets me track my code
*/

function tracking_function(){
//enter code here
}

add_action("wp_head","tracking_function");

Then, because you have a network of sites, you will have to go into the Network Plugin manager, and activate the plugin for all networks and then you can activate or deactivate it for each specific site in your network.

The two options I can think of would be to use a plugin that loads "custom functions" for the sites that it is included on - much like functions.php in your theme, but independent of each theme. I wrote a plugin that does this which you can find here: http://justinsilver.com/technology/wordpress/wordpress-plugins/wordpress-plugin-custom-functions-php/

The other option would be to put a PHP file with your code into the wp-content/mu-plugins (Must-Use Plugins) that will be loaded before all other plugins on every site in your network. A bit more straightforward than the first option, but you can't selectively disable it should you not want on it on one of your network sites.

Tags: PHP / Wordpress

Similar questions

WordPress custom permalink /%postname%.html only applies to posts, not pages?
When I change my permalinks to custom: My posts are all redrawn correctly to post-title.html, however, my pages do not get the .html extension (they just appear as if I had used /%postname%/. Anyone know why and what I can do as a workaround?
WordPress Customizer Opacity Slider applies opacity to the whole page, not just the background image
I'm building a custom WP Theme and am working on adding to The Customizer. I previously added a background image setting, and am trying to also implement an opacity slider. I looked up how to use the :before or :after pseudoclass to make opacity apply correctly to my background-image, but the children are still affected by the opacity setting. Is t...
How to execute a simple php script in WP (I don't think wp_enque_script applies here)
I have a wordpress page where I want to include a budget calculation tool. This budget calculation tool is of course tied to a php script that executes the fields in the budget calculation tool. This php script lives in the file "view.php" My question is: how can I execute this php script in Wordpress Here is the script working in a simple HTML/CSS...
I have a plugin that applies a Google translation to my page--how does the browser know which language to display?
My site uses a plugin (Google Language Translator) to apply a Google translation of the text (see page here--the flags at the top of the page switch the language). If I change to another language, close the browser, re-open the browser, and re-open my page, the same language is displayed. How does the browser know to display in the same language as...
Replacing image attributes (data-src) also applies to backend
I want to apply lozad.js script to my wordpress/woocommerce frontend. For this to work, I had to create a new attribute which is data-src and place 'loading.gif' in the normal src attribute. The code is working fine but it also applies to the backend but I'm not loading the lozad.js script in the backend so the images in my product list view are no...
How would one modify the filtering Gutenberg applies to pasted content?
Note: I'm adding information I discover that seems to be leading to a resolution in the note. When pasting content from a source external to Gutenberg into Gutenberg some HTML/CSS formatting is lost.[1] While Gutenberg retains most HTML (semantic) elements it drops CSS (styling/non-semantic) elements. This means that properties such as font size, t...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.