1

I'm receiving this error: [27-Jun-2012 18:22:39 UTC] PHP Fatal error: Call to undefined function plugin_basename() in /XXXX/public_html/wp-content/plugins/price-update/price-update.php on line 68:

            //We set the plugin basename here, could manually set it, but this is for demonstration purposes
            $this->plugin_basename = plugin_basename(__FILE__);

in this block of code:

class PriceUpdate extends mtekk_admin
{
        protected $version = '0.0.1';
        protected $full_name = 'Price Update';
        protected $short_name = 'Price Update';
        protected $access_level = 'manage_options';
        protected $identifier = 'pr_upate';
        protected $unique_prefix = 'prud';
        protected $plugin_basename = 'price-update/price-update.php';
        protected $opt = array();
        /**
         * __construct()
         *
         * Class default constructor
         */
        function __construct()
        {
                //We set the plugin basename here, could manually set it, but this is for demonstration purposes
                $this->plugin_basename = plugin_basename(__FILE__);
                register_deactivation_hook(__FILE__, array($this, 'deactivate'));
                add_action($this->unique_prefix . '_cron_hook', array($this,'cron_handle'));
                //Register some of our custom taxonomies
                add_action('init', array($this, 'wp_init'), 0);
                add_action('wp_footer', array($this, 'footer'));
                //We're going to make sure we load the parent's constructor
                parent::__construct();
        }

However, its defined above:

        protected $plugin_basename = 'price-update/price-update.php';

any ideas?

2
  • 1
    The plugin_basename function does exist, and it is in wp-includes/plugin.php, and has been around since version 1.5 or so. If you're getting an undefined error, then you have a broken or incomplete copy of WordPress somehow, or you're calling your plugin incorrectly in some manner. Either way, the answer is not capable of being reached from the limited amount of code you've posted here.
    – Otto
    Jul 1, 2012 at 11:49
  • pastebin.com/UmCkUrZp is the complete file Jul 1, 2012 at 20:24

2 Answers 2

2

plugin_basename is a WordPress function that gets your plugins file name by simply passing it the directory.

When you call $this->plugin_basename PHP is looking for a function plugin_basename defined within YOUR class. No function plugin_basename() in your class == PHP Fatal Error.

If you simply want to define a variable that contains the plugins file name do this:

var $plugin_filename = plugin_basename( __FILE__);

1
  • Not quite correct. When you reference $this->plugin_basename it is looking for a member variable that is $plugin_basename (which exists). If there was a call to $this->plugin_basename() then PHP would be looking for the member function plugin_basename()
    – mtekk
    Jul 4, 2012 at 0:47
0

Perhaps you'd be better off using plugin_dir_path(__FILE__);?

Example:

<?php
define( 'MYPLUGINNAME_PATH', plugin_dir_path(__FILE__) );
require MYPLUGINNAME_PATH . 'includes/class-metabox.php';
require MYPLUGINNAME_PATH . 'includes/class-widget.php';
?>

[EDIT - 06/29/2012]

I believe you have your PHP OOP logic incorrect, but I could be wrong. You're defining a property in a child class which extends a parent class. You then call the parent constructor inside the child constructor.

The code you provided shows that you assign the child class property $plugin_basename, but then you don't do anything with it after that.

I'm assuming you're trying to set a property $plugin_basename for the parent class to handle. However, if that's the case, then you need to either define the property within the parent class, OR you need to call the parent method which makes use the property $plugin_basename.

If that's NOT the case, then you need to be sure you make use of the property $plugin_basename, within the child class, AFTER it's been assigned like so: $this->child_class_method_name($this->plugin_basename);

You could access the parent class method which handles the property $plugin_basename, and pass the property assigned by the child class to the parent class method like so: $this->parent_class_method_name($this->plugin_basename);

I'm not really too certain how you have your plugin setup, given the minimal amount of code you provided.

If you could provide more of an idea of what you do with the property $plugin_basename, after it's been assigned, I could possibly be of more assistance.

3
  • I changed it to: $this->plugin_basename = plugin_dir_path(__FILE__); and still getting the same type error: [28-Jun-2012 15:01:43 UTC] PHP Fatal error: Call to undefined function plugin_dir_path() in /home/whatsthe/public_html/wp-content/plugins/price-update/price-update.php on line 68 Jun 28, 2012 at 15:07
  • @whatsthebigidea Please read my updated response, regarding your question. Jun 29, 2012 at 20:20
  • The mtekk_admin class, an older version of this: github.com/mtekk/Breadcrumb-NavXT/blob/master/includes/… does have the basename property, and the basename is used in the various areas that the WP API needs it (some hooks for example).
    – mtekk
    Jul 4, 2012 at 0:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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