3 replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
source | link

Building an API around add_actionBuilding an API around add_action: Create a custom action get_price_action in the constructor to hook the get_price function. One can call do_action( 'get_price_action', 50 ); somewhere in the theme or in another plugin, and you don't have to care about the inner workings of the class anymore.

Building an API around add_action: Create a custom action get_price_action in the constructor to hook the get_price function. One can call do_action( 'get_price_action', 50 ); somewhere in the theme or in another plugin, and you don't have to care about the inner workings of the class anymore.

Building an API around add_action: Create a custom action get_price_action in the constructor to hook the get_price function. One can call do_action( 'get_price_action', 50 ); somewhere in the theme or in another plugin, and you don't have to care about the inner workings of the class anymore.

    Tweeted twitter.com/StackWordPress/status/708117425347293184
2 Corrected the code and added a code example to help anyone with the same question.
source | link
    <?php
/**
 * Get The Car
 *
 * Gets car related stuff likes meta.
 *
 * @since   1.0.0
 * @package VRC
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * VR_Get_Car.
 *
 * Get class for the car post_type.
 *
 * @since 1.0.0
 */

if ( ! class_exists( 'VR_Get_Car' ) ) :

class VR_Get_Car {

    /**
     * The Car ID.
     *
     * @var     int
     * @since   1.0.0
     */
     public $the_car_ID;


     /**
      * The Meta Data.
      *
      * @var    array
      * @since  1.0.0
      */
      public $the_meta_data;


      /**
       * Meta Keys.
       *
       * @var   array
       * @since 1.0.0
       */
       private $meta_keys = array(
            // TAB                 : Basic Information.
            'price'                => 'vr_car_price',
            'price_postfix'        => 'vr_car_price_postfix',
        );


     /**
      * Constructor.
      *
      * Checks the car ID and assigns
      * the meta data to $the_meta_data.
      *
      * @since 1.0.0
      */
     public function __construct( $the_car_ID = NULL ) {
        // Check if there is $the_car_ID.
        if ( ! $the_car_ID ) {
            $the_car_ID = get_the_ID();
        } else {
            $the_car_ID = intval( $the_car_ID );
        }

            // Assign values to the class variables.
            if ( $the_car_ID > 0 ) {
            $this->the_car_ID    = $the_car_ID;
            $this->the_meta_data = get_post_custom( $the_car_ID );
        }
     }


     /**
      * Get Car: Meta.
      *
      * Gets the car meta_value if passed
      * a meta_key through argument.
      *
      * @since 1.0.0
      */
     public function get_car_meta( $meta_key ) {
        // If meta is set then return value else return false.
        if ( isset( $this->meta_data[>the_meta_data[ $meta_key ] ) ) {
            // Returns the value of meta.
            return $this->meta_data[>the_meta_data[ $meta_key ][0];
        } else {
            return false;
        }
     }


    /**
     * Get Car: ID.
     *
     * @since 1.0.0
     */
    public function get_car_ID() {
        return $this->$the_car_ID;
    }


    /**
     * Get Car: Price.
     *
     * @since 1.0.0
     */
    public function get_car_price() {
        // Returns false if ID is not present.
        if ( ! $this->the_car_ID ) {
            return false;
        }
        return $this->get_car_meta( $this->meta_keys['price'] );
    }


    /**
     * Get Car: Price Postfix.
     *
     * @since 1.0.0
     */
    public function get_car_price_postfix() {
        // Returns false if ID is not present.
        if ( ! $this->the_car_ID ) {
            return false;
        }
        return $this->get_car_meta( $this->meta_keys['price_postfix'] );
    }
   } // class `VR_Get_Car`  ended.

endif;

EDIT: Thanks to Alain, I figured it out. The issue was with my code inside get_car_meta() function instead of the_meta_data I was using meta_data variable. I just corrected the code above.

But from this discussion, I learned that I could use only one function to return the complete instance of this class. Which was very helpful. I am going to correct the code above and explain.

Now I have a method in my plugin which helps me instantiate the class in the theme. That method is:

/**
 * Get an object of VR_Get_Car class.
 *
 * @since 1.0.0
 */
if ( ! function_exists( 'vr_get_car_obj' ) ) {
    function vr_get_car_obj( $the_car_ID ) {
        // Bails if no ID.
        if ( ! $the_car_ID ){
            return 'No Car ID provided!';
        }

        return new VR_Get_Car( $the_car_ID );
    }
}

And inside the theme I am using the following code to interact with this class.

// Object of VR_Get_Car class.
        if ( function_exists( 'vr_get_car_obj' ) ) {

            $vr_get_car = vr_get_car_obj( get_the_ID() );

            $vr_car_price = $vr_get_car->get_price();

        }

And it works :)

    <?php
/**
 * Get The Car
 *
 * Gets car related stuff likes meta.
 *
 * @since   1.0.0
 * @package VRC
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * VR_Get_Car.
 *
 * Get class for the car post_type.
 *
 * @since 1.0.0
 */

if ( ! class_exists( 'VR_Get_Car' ) ) :

class VR_Get_Car {

    /**
     * The Car ID.
     *
     * @var     int
     * @since   1.0.0
     */
     public $the_car_ID;


     /**
      * The Meta Data.
      *
      * @var    array
      * @since  1.0.0
      */
      public $the_meta_data;


      /**
       * Meta Keys.
       *
       * @var   array
       * @since 1.0.0
       */
       private $meta_keys = array(
            // TAB                 : Basic Information.
            'price'                => 'vr_car_price',
            'price_postfix'        => 'vr_car_price_postfix',
        );


     /**
      * Constructor.
      *
      * Checks the car ID and assigns
      * the meta data to $the_meta_data.
      *
      * @since 1.0.0
      */
     public function __construct( $the_car_ID = NULL ) {
        // Check if there is $the_car_ID.
        if ( ! $the_car_ID ) {
            $the_car_ID = get_the_ID();
        } else {
            $the_car_ID = intval( $the_car_ID );
        }

            // Assign values to the class variables.
            if ( $the_car_ID > 0 ) {
            $this->the_car_ID    = $the_car_ID;
            $this->the_meta_data = get_post_custom( $the_car_ID );
        }
     }


     /**
      * Get Car: Meta.
      *
      * Gets the car meta_value if passed
      * a meta_key through argument.
      *
      * @since 1.0.0
      */
     public function get_car_meta( $meta_key ) {
        // If meta is set then return value else return false.
        if ( isset( $this->meta_data[ $meta_key ] ) ) {
            // Returns the value of meta.
            return $this->meta_data[ $meta_key ][0];
        } else {
            return false;
        }
     }


    /**
     * Get Car: ID.
     *
     * @since 1.0.0
     */
    public function get_car_ID() {
        return $this->$the_car_ID;
    }


    /**
     * Get Car: Price.
     *
     * @since 1.0.0
     */
    public function get_car_price() {
        // Returns false if ID is not present.
        if ( ! $this->the_car_ID ) {
            return false;
        }
        return $this->get_car_meta( $this->meta_keys['price'] );
    }


    /**
     * Get Car: Price Postfix.
     *
     * @since 1.0.0
     */
    public function get_car_price_postfix() {
        // Returns false if ID is not present.
        if ( ! $this->the_car_ID ) {
            return false;
        }
        return $this->get_car_meta( $this->meta_keys['price_postfix'] );
    }
   } // class `VR_Get_Car`  ended.

endif;
    <?php
/**
 * Get The Car
 *
 * Gets car related stuff likes meta.
 *
 * @since   1.0.0
 * @package VRC
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * VR_Get_Car.
 *
 * Get class for the car post_type.
 *
 * @since 1.0.0
 */

if ( ! class_exists( 'VR_Get_Car' ) ) :

class VR_Get_Car {

    /**
     * The Car ID.
     *
     * @var     int
     * @since   1.0.0
     */
     public $the_car_ID;


     /**
      * The Meta Data.
      *
      * @var    array
      * @since  1.0.0
      */
      public $the_meta_data;


      /**
       * Meta Keys.
       *
       * @var   array
       * @since 1.0.0
       */
       private $meta_keys = array(
            // TAB                 : Basic Information.
            'price'                => 'vr_car_price',
            'price_postfix'        => 'vr_car_price_postfix',
        );


     /**
      * Constructor.
      *
      * Checks the car ID and assigns
      * the meta data to $the_meta_data.
      *
      * @since 1.0.0
      */
     public function __construct( $the_car_ID = NULL ) {
        // Check if there is $the_car_ID.
        if ( ! $the_car_ID ) {
            $the_car_ID = get_the_ID();
        } else {
            $the_car_ID = intval( $the_car_ID );
        }

            // Assign values to the class variables.
            if ( $the_car_ID > 0 ) {
            $this->the_car_ID    = $the_car_ID;
            $this->the_meta_data = get_post_custom( $the_car_ID );
        }
     }


     /**
      * Get Car: Meta.
      *
      * Gets the car meta_value if passed
      * a meta_key through argument.
      *
      * @since 1.0.0
      */
     public function get_car_meta( $meta_key ) {
        // If meta is set then return value else return false.
        if ( isset( $this->the_meta_data[ $meta_key ] ) ) {
            // Returns the value of meta.
            return $this->the_meta_data[ $meta_key ][0];
        } else {
            return false;
        }
     }


    /**
     * Get Car: ID.
     *
     * @since 1.0.0
     */
    public function get_car_ID() {
        return $this->$the_car_ID;
    }


    /**
     * Get Car: Price.
     *
     * @since 1.0.0
     */
    public function get_car_price() {
        // Returns false if ID is not present.
        if ( ! $this->the_car_ID ) {
            return false;
        }
        return $this->get_car_meta( $this->meta_keys['price'] );
    }


    /**
     * Get Car: Price Postfix.
     *
     * @since 1.0.0
     */
    public function get_car_price_postfix() {
        // Returns false if ID is not present.
        if ( ! $this->the_car_ID ) {
            return false;
        }
        return $this->get_car_meta( $this->meta_keys['price_postfix'] );
    }
   } // class `VR_Get_Car`  ended.

endif;

EDIT: Thanks to Alain, I figured it out. The issue was with my code inside get_car_meta() function instead of the_meta_data I was using meta_data variable. I just corrected the code above.

But from this discussion, I learned that I could use only one function to return the complete instance of this class. Which was very helpful. I am going to correct the code above and explain.

Now I have a method in my plugin which helps me instantiate the class in the theme. That method is:

/**
 * Get an object of VR_Get_Car class.
 *
 * @since 1.0.0
 */
if ( ! function_exists( 'vr_get_car_obj' ) ) {
    function vr_get_car_obj( $the_car_ID ) {
        // Bails if no ID.
        if ( ! $the_car_ID ){
            return 'No Car ID provided!';
        }

        return new VR_Get_Car( $the_car_ID );
    }
}

And inside the theme I am using the following code to interact with this class.

// Object of VR_Get_Car class.
        if ( function_exists( 'vr_get_car_obj' ) ) {

            $vr_get_car = vr_get_car_obj( get_the_ID() );

            $vr_car_price = $vr_get_car->get_price();

        }

And it works :)

1
source | link

What is the best way to instantiate a class of a plugin in your WordPress theme?

What is the best way to instantiate a class of a plugin in your WordPress theme? To be clear, let's say that there is a class called Get_Meta_Data() and it is being used to get the metadata from DB for any post type. So, I had defined a metadata price and get_price() is a function in the Get_Meta_Data() class, which exists in our plugin. How would you invoke that class in the theme to access the price of a post inside the loop.

Solutions I found: — Classes defined in plugins are not accessible by themes so build methods in plugins outside the class to be able to use those methods in themes. While these methods in a plugin can call the class. Which is not an ideal situation.

Building an API around add_action: Create a custom action get_price_action in the constructor to hook the get_price function. One can call do_action( 'get_price_action', 50 ); somewhere in the theme or in another plugin, and you don't have to care about the inner workings of the class anymore.

If you deactivate the plugin you are still safe: WordPress just ignores unknown actions and the do_action() will do no harm. Plus, other plugins are able to remove or replace the action, so you have build a nice mini API with one add_action().

But I also want my class to take an argument of get_the_ID(). And that complicates the singleton classes.

I open to all kind of suggestions and class architectures. What do you guys do in a similar situation?

At the moment, my class looks like this and it is simply being required in the base file of a plugin.

    <?php
/**
 * Get The Car
 *
 * Gets car related stuff likes meta.
 *
 * @since   1.0.0
 * @package VRC
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * VR_Get_Car.
 *
 * Get class for the car post_type.
 *
 * @since 1.0.0
 */

if ( ! class_exists( 'VR_Get_Car' ) ) :

class VR_Get_Car {

    /**
     * The Car ID.
     *
     * @var     int
     * @since   1.0.0
     */
     public $the_car_ID;


     /**
      * The Meta Data.
      *
      * @var    array
      * @since  1.0.0
      */
      public $the_meta_data;


      /**
       * Meta Keys.
       *
       * @var   array
       * @since 1.0.0
       */
       private $meta_keys = array(
            // TAB                 : Basic Information.
            'price'                => 'vr_car_price',
            'price_postfix'        => 'vr_car_price_postfix',
        );


     /**
      * Constructor.
      *
      * Checks the car ID and assigns
      * the meta data to $the_meta_data.
      *
      * @since 1.0.0
      */
     public function __construct( $the_car_ID = NULL ) {
        // Check if there is $the_car_ID.
        if ( ! $the_car_ID ) {
            $the_car_ID = get_the_ID();
        } else {
            $the_car_ID = intval( $the_car_ID );
        }

            // Assign values to the class variables.
            if ( $the_car_ID > 0 ) {
            $this->the_car_ID    = $the_car_ID;
            $this->the_meta_data = get_post_custom( $the_car_ID );
        }
     }


     /**
      * Get Car: Meta.
      *
      * Gets the car meta_value if passed
      * a meta_key through argument.
      *
      * @since 1.0.0
      */
     public function get_car_meta( $meta_key ) {
        // If meta is set then return value else return false.
        if ( isset( $this->meta_data[ $meta_key ] ) ) {
            // Returns the value of meta.
            return $this->meta_data[ $meta_key ][0];
        } else {
            return false;
        }
     }


    /**
     * Get Car: ID.
     *
     * @since 1.0.0
     */
    public function get_car_ID() {
        return $this->$the_car_ID;
    }


    /**
     * Get Car: Price.
     *
     * @since 1.0.0
     */
    public function get_car_price() {
        // Returns false if ID is not present.
        if ( ! $this->the_car_ID ) {
            return false;
        }
        return $this->get_car_meta( $this->meta_keys['price'] );
    }


    /**
     * Get Car: Price Postfix.
     *
     * @since 1.0.0
     */
    public function get_car_price_postfix() {
        // Returns false if ID is not present.
        if ( ! $this->the_car_ID ) {
            return false;
        }
        return $this->get_car_meta( $this->meta_keys['price_postfix'] );
    }
   } // class `VR_Get_Car`  ended.

endif;

And I am trying to access the car price in my theme inside the loop like this

// Object of VR_Get_Rental class.

$vr_get_rental = new VR_Get_Car( get_the_ID() );

$vr_this_rental['price'] = $vr_get_rental->get_car_price();

And this doesn't work. Theme doesn't reconginze the class.