Support » Plugin: GTM4WP » Multisite: Filter not working when network activated

  • Resolved coccoinomane

    (@coccoinomane)


    Hello,

    First of all, thank you very much for this great plugin, which I use on all my sites 🙂

    I never had issues with it since yesterday, when I tried to use the option_{$option} filter (https://developer.wordpress.org/reference/hooks/option_option/) to change the value of the plugin’s options:

    
    add_filter(
    	'gtm4wp-options',
    	function( $options ) {
    		return [ 'gtm-code' => 'GTM-M8CT4JF' ];
    	},
    );
    

    Problem is that the filter does not fire at all when the plugin is network activated.
    Weirdly enough, it does work fine if I activate the plugin on a per-site basis.
    The same happens if I use the pre_option_{$option} filter; other plugins I use do not seem to be affected by this issue.

    Do you have any idea of what might cause this behaviour?

    Thank you very much for your attention!
    Guido

    PS: I am on v1.11.2 of the plugin, v5.2 of WordPress and v7.3 of PHP.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi,

    I am not sure what you would like to achieve but GTM4WP uses the Options API within WordPress purely so if it does not work, I am not sure what could be the cause.

    Thread Starter coccoinomane

    (@coccoinomane)

    Hi Thomas, thank you for your quick reply!

    I was trying to force the same GTM-code across all sites in my network; now I have solved the issue 🙂

    The problem was that GTM4W loads its options in the main plugin file, without leveraging any action (Github link).

    As far as I have understood, this has two side effects:

    1. Other plugins cannot reliably use the Options API to filter GTM4W options, because they cannot reliably hook before the options are loaded.
    2. All options are loaded at every pageload.

    I am now relying on a must-use plugin to filter GTM4W options, and it works.

    However, it could be helpful to load GTM4W options inside an action rather than direcly in the main plugin file. On one hand, it would allow devs to extend your plugin more easily, on the other hand it might provide a performance boost.

    Thank you and have a nice day!
    Guido

    PS: I was experiencing the issue only when GTM4W was Network Activated because of an unrelaed side effect: Network Activated plugins are loaded before normal plugins which means that GTM4W loaded before my plugin.

    • This reply was modified 2 years, 1 month ago by coccoinomane.
    Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi,

    What WP action would you suggest to use for plugin option loading?

    Thread Starter coccoinomane

    (@coccoinomane)

    Hi Thomas!

    I was not sure myself, so I asked on wordpress.stackexchange.com (link) and they suggested to hook into plugins_loaded, which makes sense to me, because it is fired after all plugins have been loaded and before the init action.

    Cheers,
    Guido

    • This reply was modified 2 years, 1 month ago by coccoinomane.
    Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi,

    Thanks for sharing, could you help me testing this?
    It is quite easy to achieve the goal, you only need to edit this file:

    https://github.com/duracelltomi/gtm4wp/blob/master/duracelltomi-google-tag-manager-for-wordpress.php

    Here you can see that the gtm4wp_init() function is already executing during the plugins_loaded action.

    You need to move this line above:
    require_once GTM4WP_PATH . '/common/readoptions.php';
    … into the function gtm4wp_init()

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Multisite: Filter not working when network activated’ is closed to new replies.