• Hi, I have multisite and I want set global favicon from blog_id = 1
    However is not possible write to .htaccess file, because WordPress with multisite enabled dont allow it.
    So plugin in mu-plugins

    multisite-global-favicon.php

    <?php
    
    if ( ! defined( 'ABSPATH' ) ) {
        die( 'Cannot access pages directly.' );
    }
    
    if ( !is_multisite() )
    	return;
    
    add_action( 'update_option_rewrite_rules', 'core_multisite_favicon', 10, 2 );
    function core_multisite_favicon( $old, $new ) {
    
    	if( !class_exists('Favicon_By_RealFaviconGenerator_Common' ) )
    		return;
    
    	if ( get_current_blog_id() != 1 )
    		return;
    
    	global $wp_rewrite;
    	$working_dir = Favicon_By_RealFaviconGenerator_Common::get_files_dir();
    
    	$path = parse_url( home_url(), PHP_URL_PATH );
    	$wp_in_root = ( ($path == NULL) || (strlen( $path ) == 0) );
    
    	$rewrite = ( $wp_in_root && $wp_rewrite->using_permalinks() );
    	if ( ! $rewrite )
    		return;
    
    	// Ensure get_home_path() is declared.
    	require_once ABSPATH . 'wp-admin/includes/file.php';
    
    	$home_path     = get_home_path();
    	$htaccess_file = $home_path . '.htaccess';
    
    	if ( ( ! file_exists( $htaccess_file ) ) || is_writable( $htaccess_file ) ) {
    		foreach ( scandir( $working_dir ) as $file ) {
    			if ( ! is_dir( $working_dir . DIRECTORY_SEPARATOR . $file ) ) {
    				add_rewrite_rule( str_replace( '.', '\.', $file ), trim( substr( Favicon_By_RealFaviconGenerator_Common::get_files_url(), strlen( trailingslashit( home_url() ) ) ), '/' ) . '/' . $file, 'top' );
    			}
    		}
    	}
    
    	if ( ( ! file_exists( $htaccess_file ) && is_writable( $home_path ) && $wp_rewrite->using_mod_rewrite_permalinks() ) || is_writable( $htaccess_file ) ) {
    		if ( got_mod_rewrite() ) {
    			$rules = explode( "\n", $wp_rewrite->mod_rewrite_rules() );
    			return insert_with_markers( $htaccess_file, 'WordPress', $rules );
    		}
    	}
    }
    
  • The topic ‘multisite-global-favicon.php’ is closed to new replies.