0

I know this is going to be a little strange because it's seemingly counter-intuitive. How do I add code to my WP multisite without adding a new plugin, and keeping it out of functions.php?

Purpose: I run a WP multisite instance that uses some custom code for sending emails using a self-signed certificate. I don't want anyone to know the code even exists. I don't want it to show in a menu, and I don't want it to be deleted during an update.

What I've Tried: I tried removing the header from the custom plugin I'm using now for this code, but that just deactivated the plugin.

I tried moving the file out of it's directory and placing it directly in the plugins folder, but that doesn't load it. I know because I lose email function.

I want to do the same for some custom shortcodes I have ready to go.

I know you might be asking why? It's easy to add the plugins and limit who can see them. I know this as well, but it would be even easier to add code without the need for creating a plugin. It also adds the benefit of completely hiding the code from customer/admin eyes. Sometimes admins can be a bigger problem than the customers.

For example, the email plugin I use has more lines of code in the header for the plugin, than the functional code itself.

Being able to add code snippets without the need for creating plugins would make my life much easier.

What I would like for a solution: A sub-directory within wp-content/ that will parse any code files included in this sub-directory.

Is WP presently capable of this?

Thank you.

4
  • Are you familiar with MU-Plugins (Must Use Plugins) and would that be a possible solution?
    – Howdy_McGee
    Commented Aug 17, 2017 at 18:25
  • I am, but it's the same problem. Must Use Plugins will still show, and they require the overhead of extra coding to make them usable. My goal is to be able to provide the bare minimum of code, get the functionality I'm looking for, and forget about it. When plugins/functions are visible, they can make for more questions and headaches. Commented Aug 17, 2017 at 18:30
  • why can't it be in functions.php? you can avoid losing changes by using a child theme
    – inarilo
    Commented Aug 17, 2017 at 20:02
  • I understand the child themes, but the functions.php file can be viewed by an admin. I'm looking for a hidden solution that avoids any prying eyes. I'm surprised this is not a function within WP already, but then again, maybe it is and I'm not finding it. Commented Aug 17, 2017 at 20:07

2 Answers 2

0

In your case (I have done this on a number of sites of my own), I would use a "dummy" must-use plugin.

This plugin, which doesn't really need to say all that much, simply has a require statement to include the php code that I want to include from another location on the server.

MU plugins are active on all sites on the network, and the "edit plugin" interface does not interact with them. MU plugins don't even need headers, and with no headers present, all that is shown is the filename -- which again, you can make to say anything you want.


Another option is to add a custom require line within the wp-config file. This is very early on in the WP process, so you may be limited with what your custom code can do.

5
  • I like your idea about using the require line in the wp-config file, and I've been experimenting with it. Unfortunately, I've had no luck. I don't have a PHP program to debug issues, but I like your idea the best. I thought I could do like I have with my wp-content/uploads folder. I symlinked this folder to a dedicated drive. I thought I could just reference the file, and then it would work. However, a plugin must be activated, and this is failing. Ideally I don't want WordPress to see my custom code as a plugin, but at the moment, I can't get it to parse my files. Any ideas? Commented Aug 19, 2017 at 12:56
  • I think one catch is that if your code needs to reference any actions/hooks in WP or interface with the DB, wp-config is too early. I don't think the action/filter hooks are set up and the connection to the DB happens right after wp-config is loaded (since it has your DB info in it). I know what you are asking is possible (the DesktopServer software does this and adds plugins without my knowledge of where the code is for these plugins), but I don't know how it does it. Commented Aug 19, 2017 at 22:29
  • I did figure this out, and it solves my problem. I added a reference in my functions.php file to the file I want WP to run. Like this: Commented Sep 25, 2017 at 22:47
  • include( WP_CONTENT_DIR . '/custom-shortcodes.php' ); This allows for the functionality of a plugin, and nothing shows up in the WordPress Admin. While I didn't test it, you should be able to symlink to the file and put it above wp-content. I accepted your answer because it pointed me in the right direction. Thank you. Commented Sep 25, 2017 at 22:56
  • You're welcome. Glad to help! Commented Sep 26, 2017 at 2:58
0

I can only think of these options:

  1. You could make PHP code not related to WP but that have access to the database and add some triggers, that will depend on what your custom code does and it will be complex too (WP db knowledge), Example: look at the posts table for new entries and send an email about it; it will be like creating a small php program.
  2. another posibility its to do a normal plugin and restrict it to a specific user (superadmin or by ID), file permissions too so only a specific user can open the file, the thing with that is they will know something is there, a dummy interface can be used too for this.

and yeah like you said is counter-intuitive, use WP but without using it, that will be the database way, you will use the data WP creates but you will not use WP code.

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.