• I have wp-piwik installed as a Network Plugin where it takes the “central approach”.

    I have a javascript file that needs access to the site id that wp-piwik creates for sites on the network. It would be super easy if the piwik site id were the same id as the multisite site id in wp. In my functions.php file, I could have just gone:

    wp_localize_script( 'js-config', 'ajaxInfo',
    
    			array(
    
    			    'site_id'            => get_current_blog_id(),
    			    'site_name'         => $siteName,
                    'logo'               =>  get_header_image()
    
    			)
    		);

    And then used the id in my js file like:

    someFunctionThatTakesTheSiteId(ajaxInfo.site_id);

    but they’re not the same and i’m not sure how to get access to the current piwik site id. Do you have any functions or anything I can use to localize the id for my scripts?

    https://www.ads-software.com/plugins/wp-piwik/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author braekling

    (@braekling)

    I’m not able to test this now, but you should be able to get the current site’s Piwik ID using this method:

    $GLOBALS ['wp-piwik']->getOption ( 'site_id' )

    WP-Piwik has to be loaded, of course.

    Thread Starter friendlyfire

    (@friendlyfire)

    Sorry it’s been a bit – was working on other things – but I just tested this and it didn’t work.

    When you say “piwik has to be loaded” you mean activated on the site I’m trying to get the id from, yes? because it is in this case.

    Any other ideas? This is my last remaining item so appreciate any guidance.

    Thread Starter friendlyfire

    (@friendlyfire)

    update: I tried this too:

    $piwik_site_id = $GLOBALS ['wp-piwik']->settings->getOption( 'site_id' );

    but got :

    Cannot access private property WP_Piwik::$settings

    Plugin Author braekling

    (@braekling)

    I tested it on my personal blog,

    `
    $GLOBALS [‘wp-piwik’]->getOption ( ‘site_id’ )
    `

    is working as expected and delivers the Piwik site ID.

    When I say “WP-Piwik has to be loaded” I mean the plugin has to be loaded before $GLOBALS ['wp-piwik']->getOption can be called. This is related to the execution sequence of different WordPress components. Maybe it will help if you call wp_localize_scriptusing the `plugins_loaded’ action: https://codex.www.ads-software.com/Plugin_API/Action_Reference/plugins_loaded

    Thread Starter friendlyfire

    (@friendlyfire)

    OK, I set all that up but now I’m really confused.

    on my local install, when I alert the site_id from javascript, I keep seeing the id of ‘1’. This happens regardless of the site I’m looking at.

    When I go to the database and look at wp_options, I see “wp-piwik-site_id” is set to 1 for every site on the network.

    Just to test it again, I made a completely new install with piwik and multisite with wp-piwik installed as the “central approach”. I set up a bunch of sites , configured them and made sure to log visits and checked to make sure they show up in piwik (they all do).

    In most cases, the site_id is totally blank in the db even though it’s showing in the wp-admin panel when I look at the sites under wp-piwik.

    For example:
    site 35’s db: https://drive.google.com/open?id=0B-r5llGpXi8xWVJXdHFuczRuYzA
    site 35 in the admin: https://drive.google.com/open?id=0B-r5llGpXi8xeGZfbjJZZmJiRTg

    The really weird part is in some other site’s wp_options “wp-piwik-site_id”, I see the number 2 (similar to the local install problem above).

    Any help is appreciated. This one completely has me stumped.

    Thread Starter friendlyfire

    (@friendlyfire)

    Also, could I just change the db’s site_id manually to match the one in the wp-admin for now until we get it sorted out? Would that cause any issues? I’d like to start testing some work that requires the id.

    Plugin Author braekling

    (@braekling)

    Are you using some kind of a caching plugin? Seems like WP-Piwik is able to get the correct IDs, but not to store them in WordPress’ database?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘how can I get the current wp-piwik site id?’ is closed to new replies.