Skip to content

Instantly share code, notes, and snippets.

@gordjw
Last active August 29, 2015 14:21
Show Gist options
  • Save gordjw/c6a942a33ede1d615320 to your computer and use it in GitHub Desktop.
Save gordjw/c6a942a33ede1d615320 to your computer and use it in GitHub Desktop.
WP Piwik fixer
<?php
/*
Plugin Name: Quick and Dirty WP-Piwik Fixer
Description: Temporary WP-Piwik Fixer, tested with WP-Piwik 0.9.9.18 and Piwik 2.9.1. Fixes site ids stored in WP Option tables in case yours have become messed up.
*/
// Quick and dirty, don't install if you don't have shell or FTP access. It will takeover your admin screens.
// Commented out the line below to stop people from accidentally installing and killing wp-admin
// Uncomment to enable, recomment to disable. You won't have wp-admin access while it's enabled.
//add_action('admin_init', 'showit');
function showit() {
// Super admins only
if( ! current_user_can('manage_options') )
return;
$blogs = wp_get_sites(array('archived'=>false, 'deleted'=>false));
global $desired_ids;
echo "<table><thead><tr><th>Domain</th><th>Site ID</th><th>Current Piwik ID</th><th>Desired Piwik ID</th></tr></thead><tbody>";
foreach( $blogs as $blog ) {
$current_settings = get_blog_option( $blog['blog_id'], 'wp-piwik_settings', false );
$current_id = $current_settings['site_id'];
$current_tracking_code = $current_settings['tracking_code'];
$current_noscript_code = $current_settings['noscript_code'];
unset( $desired_id ); // Clear for every site
// Only change blogs listed in $desired_ids
if( isset($desired_ids[$blog['blog_id']]) && ! empty($desired_ids[$blog['blog_id']]) && is_int($desired_ids[$blog['blog_id']]) ) {
$desired_id = $desired_ids[$blog['blog_id']];
$new_tracking_code = str_replace($current_id, $desired_id, $current_tracking_code);
$new_noscript_code = str_replace($current_id, $desired_id, $current_noscript_code);
$new_settings = $current_settings;
$new_settings['site_id'] = $desired_id;
$new_settings['tracking_code'] = $new_tracking_code;
$new_settings['noscript_code'] = $new_noscript_code;
// Uncomment for debugging and verifying
//var_dump($current_settings);
//echo "<pre>" . htmlentities($current_tracking_code) . "</pre>";
//echo "<pre>" . htmlentities($current_noscript_code) . "</pre>";
//var_dump($new_settings);
//echo "<pre>" . htmlentities($new_tracking_code) . "</pre>";
//echo "<pre>" . htmlentities($new_noscript_code) . "</pre>";
// Uncomment the line below to make changes
//update_blog_option( $blog['blog_id'], 'wp-piwik_settings', $new_settings );
}
echo "<tr><td>{$blog['domain']}</td><td>{$blog['blog_id']}</td><td>{$current_id}</td><td>{$desired_id}</td></tr>";
}
echo "</tbody></table>";
die();
}
// Array representing the desired state
// Keys are the WP blog ID (string), and values are the Piwik site ID (int)
// You'll need to figure out what this array should be for your installation
$desired_ids = array(
// '1' => 40,
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment