WordPress.org

Ready to get started?Download WordPress

Forums

require_once called by wp_head? (4 posts)

  1. spucktier
    Member
    Posted 6 years ago #

    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?

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

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

  3. spucktier
    Member
    Posted 6 years ago #

    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');

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic