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.

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?

2 Answers
2

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.

Leave a Comment