Working with WordPress plugins can often be an experiment akin to finding your way in the dark. Many plugins are hastily cobbled together, and more are just badly documented.
I recently found myself faced with installing and using a plugin that was intended to send tracking data to an external server. To do so, the plugin places JavaScript within the code of our WordPress site’s pages that matches the code snippets provided by the maker of the tracking service. Within this JavaScript code, there is a variable that the tracking code uses to identify our site in its dashboards; the plugin writes it out, setting it with what it thinks is the domain that our site lives on.
The server dashboard, where I could look at the aggregated data that was sent, insisted on organizing the data views by domain – not a bad idea, considering we have multiple servers such as blogs.ourcompany.com
and ourcompany.com
presenting a consolidated site experience to our visitors – but even though the tracking service allowed different subdomains to send data to the same dashboard “domain,” the requirement to do so was that we have our WordPress site’s pages “lie” and have the plugin output JavaScript with the top-level domain we wanted all our data to roll-up to instead of the default domain that was being used by the plugin.
I set about to find some documentation about the plugin. All I could find was the tracking service’s documentation for its JavaScript API – which made explicit what variable was to be overridden, but didn’t even connect to the page that described the plugin.
No Documentation? No Problem!
After some exploration, it turns out that all I needed to do was a quick search on the text apply_filters
in the plugin’s PHP files. Anything that is set using the apply_filters()
method can actually be overriden in your theme’s functions.php
file using a call to the add_filter()
method! The best part? This trick generalizes to all plugins that use the apply_filters()
method to obtain data… as all well-written plugins should!
An example:
Let’s say our third-party plugin’s main PHP file, plugin-name.phph
, contains the following:
$domain = apply_filters( 'plugin-name_config_domain', plugin-name_get_display_url (get_option('home')) );
If that resulted in the output of the value blogs.hbr.org
in the plugin’s generated JavaScript, we can easily add the following code to our theme’s functions.php
file, overriding the default domain:
add_filter( 'plugin-name_config_domain', function( $domain) {
return 'ourdomain.com';
} );
Comments are closed.