Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am looking for a solution to printing a Javascript include in the footer of WordPress front-end, and I know there are options for this. However, what would allow me to do so with these in mind:

  • Will print in footer no matter what theme is used.
  • Cannot be overwritten
  • Cannot be removed/filtered
  • Will work on versions 3.0-3.8+

This will be done by a plugin.

Looking forward to your suggestions. Thank you


I understand how this need would come off as unfriendly, but this is something that will be placed on limited hosted wordpress installs and is a condition that is agreed to. So this is a legitimate need to prevent removal.

share|improve this question
    
Possible duplicate of: wordpress.stackexchange.com/questions/556/… –  Andrew Bartel Jan 25 at 22:11
1  
I am not sure there is a way to meet your conditions 100%, though I can get close. It also strikes me as potentially very unfriendly. Why do you need to do this? –  s_ha_dum Jan 25 at 22:12
    
@s_ha_dum - I have addressed your question as an update to mine. Were you going to share how to "get close" or? Thanks –  Chris Jan 25 at 23:00
    
Yes, I will post what I think is the best you can get. –  s_ha_dum Jan 26 at 0:10

2 Answers 2

Cannot be overwritten

Cannot be removed/filtered

This will be done by a plugin.

These are mutually exclusive. Such a thing can't be done by a plugin or by any other code from within WordPress. You would need to hook code in elsewhere, at the webserver or PHP level.

You could use the PHP.INI directives of auto_append_file or auto_prepend_file to force some other PHP code to always be included in a way that the user could not change. In theory, this could be used to put code into the footer, but it wouldn't be guaranteed to work with any particular theme, because it would happen outside the WordPress context.

share|improve this answer
    
Thank you for this suggestion, it is something I will look into. I was hoping to achieve within the WP/plugin. Would your suggestion change if the plugin code was encoded? –  Chris Jan 26 at 0:21
    
Encoding code just obfuscates it, it doesn't eliminate it. And code can be easily decoded. –  Otto Jan 26 at 15:28
    
@ Otto - It is not some base64... it's ion cube, and from my understanding it cannot be. –  Chris Jan 26 at 19:05
    
It runs, therefore it can be decoded. ioncube can too, it's just slightly more difficult because they put the decoder into a binary blob. –  Otto Jan 27 at 16:01

Again, as I have stated in a comment, I don't know that you can meeting all of your conditions with 100% certainty. What follows is what I believe to be about as close as you can get while working within WordPress.

  1. Use a MU-Plugin.
  2. Write your callback is such a way that is difficult, if not impossible, to remove.

First, use a must-use plugin. Create a directory in wp-content called mu-plugins. Files in that location load automatically and cannot be disabled except by deleting them, so your users would need some kind of server access-- (s)FTP or shell access. This gives you a bit more control.

Second, WordPress provides a mechanism to remove callbacks from hooks, but you need to know the name of the callback. You can use that fact to make it, at least, more difficult to remove.

To do that, you can use an anonymous class, like so:

class Hard_To_Remove {
  function __construct() {
    add_action('wp_footer',array($this,'print_to_footer'));
  }

  function print_to_footer() {
    echo 'Hello, I am a footer message';
  }
}
new Hard_To_Remove();

It can't be easily removed because there is no easy way to refer to the callback, whereas written as follows it could be removed.

class Hard_To_Remove {
  function __construct() {
    add_action('wp_footer',array($this,'print_to_footer'));
  }

  function print_to_footer() {
    echo 'Hello, I am a footer message';
  }
}
$htr = new Hard_To_Remove();

remove_action('wp_footer',array($htr,'print_to_footer'));

Closures create the same difficulty. You can't easily refer to the callback to remove it.

add_action(
  'wp_footer',
  function() {
    echo 'Hello, I am a footer message';
  }
);

However, you can get around that limitation by inspecting the global variable $wp_filter.

add_action(
  'wp_footer',
  function() {
    global $wp_filter;
    var_dump($wp_filter['wp_footer']);
  }
);

It would be a bit cludgy, but it should be possible to remove those filters.

It is always possible that a theme omits wp_footer() but that should be fairly rare.

You could use the shutdown hook, which should always run, but it would print your content outside the closing </html> tag.

share|improve this answer
    
Thank you for the suggestion. However, even if this couldn't be altered, it still depends on the theme using wp_footer action. So if the theme didn't use that action, the Javascript wouldn't be printed. I was hoping there was a hook/action that was always loaded, couldn't be read like wp_registered, or left out like wp_footer. –  Chris Jan 26 at 3:20
    
Well, your statement that "this will be done by a plugin" pretty much precludes any server-level manipulation. Like I said, you can get close but can't match those condition 100%. –  s_ha_dum Jan 26 at 3:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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