WordPress.org

Ready to get started?Download WordPress

Forums

[Plugin: WordPress MU Domain Mapping] Excessive SQL Queries (8 posts)

  1. Scott
    Member
    Posted 5 years ago #

    This plugin is generating a very large number of plugins. Literally a 100+ per page/post
    SELECT domain FROM wp_domain_mapping WHERE blog_id = '20'

    does anyone know who to limit this?

  2. Ovidiu
    Member
    Posted 5 years ago #

    there was a long thread about this on the wpmu forums, go have a look.

  3. Scott
    Member
    Posted 5 years ago #

    I saw that. No body seemed to have a solution from what I can tell.

  4. Jason Judge
    Member
    Posted 5 years ago #

    I updated the 'domain_mapping_siteurl()' function in domain_mapping.php to try and solve this problem. TBH the queries will be well indexed anyway, but this change will save the results of the query so it does not need to be executed for every link generated on the page.

    function domain_mapping_siteurl( $setting ) {
    	global $wpdb, $current_blog;
    
    	// To reduce the number of database queries, save the results the first time we encounter each blog ID.
    	static $return_url = array();
    
    	if (!isset($return_url[$wpdb->blogid])) {
    		$s = $wpdb->suppress_errors();
    
    		// Try matching on the current URL domain and blog first. This will take priorty.
    		$dm_domain = $wpdb->escape( preg_replace( "/^www\./", "", $_SERVER[ 'HTTP_HOST' ] ) );
    		$domain = $wpdb->get_var( "SELECT domain FROM {$wpdb->dmtable} WHERE domain = '{$dm_domain}' AND blog_id = '{$wpdb->blogid}' LIMIT 1" );
    
    		// If no match, then try against the blog ID alone (which we get, without a 'preferred domain' setting,
    		// will be a matter of luck.
    		if (empty($domain)) {
    			$domain = $wpdb->get_var( "SELECT domain FROM {$wpdb->dmtable} WHERE blog_id = '{$wpdb->blogid}'" );
    		}
    
    		$wpdb->suppress_errors( $s );
    		$protocol = ( 'on' == strtolower($_SERVER['HTTPS']) ) ? 'https://' : 'http://';
    		if ($domain) {
    			$return_url[$wpdb->blogid] = untrailingslashit( $protocol . $domain . $current_blog->path );
    			$setting = $return_url[$wpdb->blogid];
    		} else {
    			$return_url[$wpdb->blogid] = false;
    		}
    	} elseif ($return_url[$wpdb->blogid] !== FALSE) {
    		$setting = $return_url[$wpdb->blogid];
    	}
    
    	return $setting;
    }
    add_action( 'pre_option_siteurl', 'domain_mapping_siteurl' );
    add_action( 'pre_option_home', 'domain_mapping_siteurl' );

    As a bonus, this also allows you to add several domains to a single blog and access the blog through any of those domains. Although you would not want to make all those domains public, it does help to still be able to access the blogs using their sub-domains, for the administration screens at least.

  5. Donncha O Caoimh
    Member
    Posted 5 years ago #

    Thanks Jason for that patch. I've just applied it in trunk if you want to give it a go: http://svn.wp-plugins.org/wordpress-mu-domain-mapping/trunk/domain_mapping.php

  6. Jason Judge
    Member
    Posted 5 years ago #

    Cool - will do.

    There is something that I would like to be able to do with this plugin: I would like to be able to support, on a blog-by-blog basis, how the blog is accessed. This would include by unique domain, by sub-domain, or by path on specified domains. Having that flexibility would be great.

    I believe it is technically possible, but I think there may be clashes if one site using a sub-domain happens to have a page with the same name as a blog using paths (e.g. blog1 has a page called '/blog2' and blog2 uses the path '/blog2' to identify itself. I guess so long as they are on different domains, it probably does not matter, but anyway, you get the idea. I'm just wondering whether that has already been done by anyone, or whether it is an idea that has been requested before?

    Edit: maybe the options should be set on a domain-by-domain basis, rather than blog-by-blog?

    Edit: Oh, and some other things I would like to do, is to declare some of the domains and paths as 'preferred' so that a redirect happens if any of the others are accessed, with perhaps a different preferred admin and non-admin domain. That way all the blogs could be administered from a single domain, using paths, so the administrator remains logged in to them all, while still providing public access through an alternate domain.

    -- Jason

  7. Jason Judge
    Member
    Posted 5 years ago #

    donncha - the SVN update worked. I had to merge it all with my own changes, but it looks good. Just FYI, one thing I changed was the namespace for the functions - I made sure they all had the same prefix on (mu_domain_mapping_) so there was less chance of clashing with other plugins.

    If it is possible to put all these functions into a static class, I think that would help even more wrt namespaces. Of course, it may not be a problem to anyone like it is now, but I'm always aware how much work it can be to integrate PHP applications when function and variable names are not suitably prefixed to keep them out of each other's way.

    -- Jason

  8. torre
    Member
    Posted 5 years ago #

    the updated plugin works fine an reduce the SQL Queries, but i have one problem:

    I use a plugin with the following code to display content from another blog to my users:

    echo '<div class="wrap">';
    switch_to_blog('38');
    $post_id = 162;
    query_posts('page_id=' . $post_id); // post-ID
    while (have_posts()) : the_post(); // starte the_loop
    	_e('<h3>') . the_title() . _e('</h3>') . "\n";
    	the_content();
    endwhile;
    restore_current_blog();
      echo '</div>';

    After aktivated the "new" Version of the plugin, the code above doesen't work anymore an the plugin displays ohne the title, but not the_content - only "NA".

    Any Idea ?

    Thx

    torre

Topic Closed

This topic has been closed to new replies.

About this Topic