Shared functions.php across multiple WordPress websites

I find myself using many of the same snippets of code for my functions.php file in new websites.

Is there a method I can use to create a few different shared functions.php files to use across my websites? I am not referring to a WPMU installation, but completely separate installations of WordPress with separate databases, possibly on different hosts.

Read More

For example, if there are functions I want to add to a specific site, I would just add them to the site’s own functions.php, but also have a shared functions.php file for all my multi-author sites and one for all my single author sites. Another situation would be to group together all similar functions into separate functions.php files.

UPDATE

Here is an update with an example I found in a premium theme’s functions.php. This is the only code in the functions.php, so I assume all the functions are pulled from other files.

$themename = 'CoolTheme';
$shortname = 'cooltheme';

include_once TEMPLATEPATH.'/cool_framework/plugins/pagination.php';
include_once TEMPLATEPATH.'/cool_framework/plugins/cool_widgets.php';
include_once TEMPLATEPATH.'/cool_framework/plugins/sidebar_generator.php';
include_once TEMPLATEPATH.'/cool_framework/cool_comments.php';
include_once TEMPLATEPATH.'/cool_framework/cool_functions.php';
include_once TEMPLATEPATH.'/cool_framework/cool_navigation.php';
include_once TEMPLATEPATH.'/cool_framework/cool_panel/cool_panel.php';
include_once TEMPLATEPATH.'/cool_framework/cool_shortcodes/cool_shortcodes.php';

Is something like this possible, except calling the functions from a completely separate domain or cloud storage? If so, what are the issues, if any, I can expect?

Related posts

Leave a Reply

2 comments

  1. If your functions can go in a single file (no images, or linked js/css files), you could add them to mu-plugin file. See the Must Use Plugins page on Codex for more information.

    EXAMPLE:

    <?php
    /*
    Plugin Name: CommonCodes
    Plugin URI: http://you.com/
    Description: A common use file
    Version: 1.0
    Author: You
    Author URI: http://you.com/
    */
    
    function remove_dashboard_widgets() {
        global $wp_meta_boxes;
        unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
        unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
        unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
        unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
        unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_drafts']);
        unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
        unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
        unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
    }
    if (!current_user_can('manage_options')) {
        add_action('wp_dashboard_setup', 'remove_dashboard_widgets' );
    }
    
    function all_settings_link() {
        add_options_page(__('All Settings'), __('All Settings'), 'administrator', 'options.php');
    }
        add_action('admin_menu', 'all_settings_link');
    
    ?>
    

    You would then just save it as commonfunctions.php or whatever. Create a folder in the wp-content directory called mu-plugins and place the single PHP file in that folder. It will auto-activate, and can’t be removed from the WP-Admin area.

    Any issues you might face would depend on the plug-ins or themes that could possibly re-use the same functions causing a conflict.

  2. I would take a look at this article about how to update a self-hosted plugin. This would allow you to setup your functions.php as a functionality plugin and then host it in one place and push updates to all sites. You’d still have to go to each site and update it, but I imagine this would still be an improvement over your current setup.

    You also might consider creating a set of options for the plugin that turns on and off sets of functions. Then you could just have a single plugin with all your functions that you then turn on and off per-site.

    =====

    EDIT: It turns out that there’s even a plugin to help deal with self-hosting plugins. It’s called…wait for it… Self Hosted Plugins. I haven’t used it, so I can’t vouch for it, but it could be worth checking out if the above tutorial doesn’t pan out.