4

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

6

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.

4
  • As mentioned, this is NOT for a MU site. I am not referring to a WPMU installation, but completely separate installations of WordPress with separate databases, possibly on different hosts. Commented May 7, 2012 at 0:34
  • 2
    @TravisPflanz The directory mu-plugins is for must use plugins, not for multi-site. These plugins are not very different from regular plugins, they just run without an explicit activation.
    – fuxia
    Commented May 7, 2012 at 2:08
  • Oh. Never heard of that before. Would you mind editing the answer with some solid references? Commented May 7, 2012 at 2:15
  • @toscho Thanks for updating the answer. I've never actually seen the codex page. I never knew the mu-plugins loaded before normal plugins, or that activation hooks are not executed. Commented May 7, 2012 at 5:29
4

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.

2
  • I added an update to the question. I prefer not to write a complete plugin, as I am not a coder. Commented May 7, 2012 at 1:14
  • 4
    Plugins aren't all that complicated. A functions.php file is practically a plugin, it's just missing the standard plugin header information (codex.wordpress.org/Writing_a_Plugin#File_Headers). Also, I suspect that any securely-configured server would block you from loading a file from an external server in the way you mentioned. Hence, I'd still consider looking in to this answer.
    – mrwweb
    Commented May 7, 2012 at 1:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.