WordPress Multisite – add function to functions.php to only affect 1 site

I am running a wordpress multisite with 2 blogs ( site1.com and site2.com )

The entire site is sharing the same theme as well as the functions.php file.

Read More

I have the following filter that I need to put into the function.php file, but I need this filter to only affect 1 blog – site2.com

The filter is as follows :

add_filter( 'get_manager_nav', 'set_manager_nav' );

function set_manager_nav( $urls ) {
    unset($urls['voucher']);
    return $urls;
} 

Is there a way to apply this filter only to 1 site ??

What I have done instead is that I have created a new plugin, I have added the function to the plugin and activated the plugin only on site2.com.

It is working great, but I suppose using a simple snippet is much better than using a plugin, so is there a way to do this using a snippet ?

Related posts

Leave a Reply

4 comments

  1. In my opinion the best way to target one subsite within multisite is to use the get_current_blog_id() function within the function called by the filter. Here’s an example of some code that would work:

    <?php
    function function_to_call(){
    
        if( get_current_blog_id() === 1 ){
            // Return something if the site ID matches the number one...
        }
    
        // Return something if the site ID does not match the number 1
    }
    add_filter( 'filter_name', 'function_to_call' );
    ?>
    

    You can grab the ID of the site you want to target by going to https://example.org/wp-admin/network/sites.php where https://example.org is replaced with your domain.

    There are a few benefits to this approach over others listed here.

    1. This will work if you have multiple versions of your multisite installation using different domains. For example, you might have http://localhost/site1, site1.example.org and site1.staging.example.org. If you checked for the domain the filter would break on every site except production. If you use the ID, it should work across the board.
    2. Other options like creating a child theme or a plugin do work, but they tend to be overkill when you’re talking about running a function on a single hook.
  2. This is a bit tricky. You might have to check for the site name or id before adding the filter.

    Your function remains as is but the filter part becomes:

    $current_site = get_current_site();
    if($current_site->domain == 'site2.com')
       add_filter( 'get_manager_nav', 'set_manager_nav' );
    

    You can read more about get_current_site() here.

  3. I know this was from a while ago but I’ve just been trying to add a function to only one multisite too.
    The method is a little confusing at first as you need to jump from the multisite admin to the main network admin.
    The way to do this is to create a child theme via the main network admin (is used a child theme creator plugin. There’s quite a few).
    Whilst still in the network admin, select the main ‘themes’ heading then ‘editor’ to show current theme then ‘select the theme to edit’ at the top right & select the child theme from the dropdown.
    Add the new code to the functions.php
    Once you’ve added what you need, switch back to the dashboard for the multisite you needed the function for and activate the child theme.
    If you need another function for a different multisite, go back to the network admin & create another child theme (I currently have twentyseventeen as my main theme then child1 for multisite 1 and child2 for multisite 2
    It’s a little convoluted but once you understand that these changes must be done from the network admin, it’s pretty easy.
    I hope that helps.

  4. Did you make this work on only site? because I have a similar problem, different function – but after trigger – it works on targeted website, but other sites not rendering content at all.

    My code:

    add_filter('the_content', 'wpse_ad_content');
    
    function wpse_ad_content($content)
    {
        if ( 2 !== get_current_blog_id() )
            return;
        
        if (!is_single()) return $content;
        $paragraphAfter = 2; //Enter number of paragraphs to display ad after.
        $content = explode("</p>", $content);
        $new_content = '';
        for ($i = 0; $i < count($content); $i++) {
            if ($i == $paragraphAfter) {
                $new_content.= '<div style="width: 300px; height: 250px; padding: 6px 6px 6px 0; float: left; margin-left: 0; margin-right: 18px;">';
                $new_content.= 'Enter your ad code here....';
                $new_content.= '</div>';
            }
    
            $new_content.= $content[$i] . "</p>";
        }
    
        return $new_content;
    }