Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

It seems that plugin_url() cannot be used in classes. This causes an error.

<?php
/*
 * Plugin Name: plugins_url
 * Description: This demonstrates plugins_url() causes an error used in a class definition.
 * Version: 1.0
 * Author: teno
 */

class SampleClass {
    $url = plugins_url('test.php', __FILE__); 
}
?>

I need to define the file url in a class. The file is located in the plugin folder. Any solution?

share|improve this question
why don't you define it outside the class ? – amit Aug 19 '12 at 8:24
do you mean create a global variable? Is that clean? – Teno Aug 19 '12 at 8:27
This is a problem of php. stackoverflow.com/questions/320009/… Functions defined outside a class cannot be used from inside the class. – Teno Aug 19 '12 at 8:45
This is wrong, you can use plugins_url() in any class. The question you linked to has nothing to do with your problem. Your code is just a syntax error: you cannot use a function for a variable definition. – toscho Aug 19 '12 at 22:47
I think it's possible to use a function for a variable definition. Properties 'in a class' cannot be defined with a function call. Then it's a problem of php syntax. – Teno Aug 20 '12 at 3:16

closed as too localized by toscho Aug 19 '12 at 22:49

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

Browse other questions tagged or ask your own question.