• Hi,
    Im trying to get an email notification any time a subsite user uploads media via the wp media page. I have 40 sub-domains in a sub-domain based Multisite – when i run the plugin /function from the main domain functions.php file it runs fine so i know it works; but of course i need it to be detected from All subsites; not the main domain. The code is thus:

    function otz_new_attachment_email( $att_id )
    {
    	$blogid = get_current_blog_id();
    
    	// ensure we only operate on a subsite and not the main site (blog-id #1)
    	if( !empty($blogid) && $blogid!=1 )
    	{
    		$super_email = null;
    		$usrdata     = get_user_by('id', 1);
    		
    		if($usrdata != false)
    			$super_email = $usrdata->user_email;
        
    		if( empty($super_email) ) die("No super admin data found");
    	
    		$headers  = "From:$super_email\r\n";
    		$headers .= "Reply-To:$super_email\r\n";
    		$headers .= "X-Mailer: PHP/".phpversion()."\r\n";
    		$headers .= "content-type: text/html";
    		
    		$subject  = 'Media Upload Detected';
    		$body     = '<p>New client media uploaded with an attachement ID of : '.$att_id.'.</p>'. wp_get_attachment_link( $att_id,'',true,false,'click here to view this attachment' );
    		
    		@wp_mail( $super_email, $subject, $body, $headers );
    	}
    	
    	 
    }
    add_action('add_attachment','otz_new_attachment_email', 10, 1);

    Could it be that my chosen hook (‘add_attachment’) cannot run inside a MU-Plugin or could it be any of the other standard wp functions? get_current_blog_id(), get_user_by() Or as i fear maybe because im trying to reference super-admin details from a subsite? i really don’t know??

    Research shows hat the load order for WP is:

    1. the WordPress core code
    2. mu-plugins
    3. plugins
    4. functions.php
    5. the theme code for the specific template being displayed

    I can run a test, simple Hello World script so know my MU-Plugins are capable of running on my installation – any ideas anyone

    thanks

    • This topic was modified 5 years, 8 months ago by girl_number_5.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    I placed your code into my own MU plugin and it worked fine on my test sub-site. This is a sub-directory network, but I don’t see how that could make a difference in your code.

    Maybe check your error log to see if there’s some site specific issue? Or reduce any chance of conflicts by isolating your code in its own plugin (if not already so). Deactivate all plugins on the site (rename the .php extension of any other MU plugin files) and switch the site to twentytwenty theme. You want a bog standard site with your code being the only exception. It ought to work then. Start restoring your normal site configuration, one module at a time, testing after each in order to determine which module is causing a conflict.

    Thread Starter girl_number_5

    (@trutherone)

    Hi bcworkz,
    Sorry for the late reply didnt know i had a response – i de-activated each plugin one by one; no joy – i can’t get a bog standard install since my project relies on the themes it uses and functionality – disabling it all negates the project.

    I’ve put it down to either the themes which are used on ALL subsites; Astra Starter Themes or stupid as it sounds; i haven’t read any evidence that mu-plugins work with sub-domain based multisites. The little info i’ve read about mu-plugins online and you yourself are using a directory-based multisite. I see no info to state whether mu-plugins are for a directory-based feature only or not.

    I’m experimenting with a plugin which i may be able to hack … ill keep you updated.

    Moderator bcworkz

    (@bcworkz)

    The same MU plugin loading process is used for all WP types, it does not discriminate against subdomain networks. I’ll wager subdomain networks rely more heavily on MU plugins than most. The only way I can imagine valid plugin code failing is either theme/plugin conflict or WP has trouble accessing the mu-plugins file system so that it cannot read the files within. Have you double checked folder and file permissions on the server?

    FYI, MU plugins are loaded in wp-settings.php here:
    https://core.trac.wordpress.org/browser/trunk/src/wp-settings.php#L306

    The source code for wp_get_mu_plugins() that’s called above.

    You can assure yourself that the plugin was loaded by placing an error_log() call at the top of the plugin file. After loading a network page, check the error log for confirmation.

    Thread Starter girl_number_5

    (@trutherone)

    I’ve tested the functioning of MU-Plugins with a couple of simple scripts, ‘hello world’ and a simple echo ‘notification’. I thought it might be plugins that use SMTP like wp-forms, wp-smtp, wp-forms for elementor etc. I’m going to create a clean install multisite, as you suggest; if successful i’ll install each plugin and theme one at a time until i find whats interfering with my mu-plugin function. My money is on Astra Themes which all my subsites rely upon heavily. Since Astra requires its own ‘base’ plugin which the themes load into; distinctly different to standard themes which comprise of html & css.

    I’ll keep this post updated for anyone else who may encounter this issue …

    Cheers

    Moderator bcworkz

    (@bcworkz)

    FWIW, generating output like “Hello world” when a plugin loads is inadvisable. And if you manage to do so without causing further issues, the output isn’t always readily apparent unless you check HTML source. Perhaps you’ve accounted for that and are sending output from a much later hook, IDK. IMO error logging is a more preferable and reliable way to do debug output. Assuming one has ready access to the log. Lacking that, a crude but effective check is to insert a wp_die() call 🙂

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Must Use-Plugin not running’ is closed to new replies.