• hey there,

    I’m working on my first plugin and ran into a problem. I need to include the following on every page:


    require_once('class.compressor.php');
    $compressor = new compressor('css,javascript,page');

    I figured the following should do the trick:

    function wp_compressor_header() {
    require_once(dirname(__FILE__) . '/class.compressor.php');
    $compressor = new compressor('css,javascript,page');
    }

    add_action('wp_head', 'wp_compressor_header');

    However, I’m stuck with “syntax error, unexpected T_VARIABLE” and similar errors.

    My question: how to include the require_once in wp_head properly and how to call the class properly?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Why are you using dirname(__FILE__)? Where is the file actually located?

    Thread Starter spucktier

    (@spucktier)

    It’s located in the same directory as the pluginfile and I was stupid 🙂
    Running with the full path to the file works great, however that’s just for testing. I also had allow_url_include=Off which led to the initial errors. This is how it looks now:

    function wp_compressor_header() {
    require_once('http://www.bastelbude.com/wp-content/plugins/wp_compressor/class.compressor.php');
    $compressor = new compressor('css,javascript,page');
    }

    however, I get an error that tells me the class “compressor” can’t be found. This is what the head of the class in the second php file looks like:

    class compressor {

    /**
    * Constructor
    * Sets the options and defines the gzip headers
    **/
    function compressor($options=null) {
    [...]

    and I get:
    Fatal error: Class 'compressor' not found in [...]/wp_compressor.php on line 56
    which is:
    $compressor = new compressor('css,javascript,page');

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Whoa, whoa.. Do not do this:
    require_once('http://www.bastelbude.com/wp-content/plugins/wp_compressor/class.compressor.php');

    That’s wrong, wrong, wrong and will *not* work on most hosts. require/include need actual local paths, not URLs. What’s more, referencing it as a URL will cause it to be executed in a different context, which is not what you want anyway.

    If the class.compressor.php is in the same directory as the plugin that is doing this require, then all you need is this:
    require_once('class.compressor.php');

    Same directory is assumed.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘require_once called by wp_head?’ is closed to new replies.