• I have a WooCommerce/Gravity Form plugin that uses the following line to add an options metabox to the editor page of a WooCommerce product:

    add_meta_box( 'woocommerce-gravityforms-meta', __( 'Gravity Forms Product Add-Ons', 'wc_gf_addons' ), array($this, 'new_meta_box'), 'products', 'normal', 'default' );

    I’ve added other custom post types that are now recognised as products by WooCommerce, and I’d like that metabox to be displayed for them as well. I can achieve this if I edit the plugin and duplicate that line, swapping the ‘products’ value for the name of the custom post type (E.g. ‘Speakers):

    add_meta_box( 'woocommerce-gravityforms-meta', __( 'Gravity Forms Product Add-Ons', 'wc_gf_addons' ), array($this, 'new_meta_box'), 'speakers', 'normal', 'default' );

    This, however, would obviously break as soon as the plugin updated, so I’d like to write a new plugin that just extends it to add that metabox onto the new post types.

    How could I go about doing that? I’ve tried to just use that new line in the plugin and call the original plugin with require_once, but that doesn’t seem to have any effect.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You can do that easily. You would need this sort of thing in your plugins code.

    BUT.. You will need to have a reference to the object that’s being used by the existing functions. Where the existing call uses $this, that’s where you’ll need to actual object that’s being used. That could be the tricky part of tracking that down.

    For this example, I’ll assume that the object is stored in a variable called $wc_gf_object.

    add_action ('add_meta_boxes'. 'my_metabox_function', 100);
    
    function my_metabox_function () {
        add_meta_box( 'woocommerce-gravityforms-meta', __( 'Gravity Forms Product Add-Ons', 'wc_gf_addons' ), array($wc_gf_object, 'new_meta_box'), 'speakers', 'normal', 'default' );
        add_meta_box( 'woocommerce-gravityforms-meta', __( 'Gravity Forms Product Add-Ons', 'wc_gf_addons' ), array($wc_gf_object, 'new_meta_box'), 'second_product_type', 'normal', 'default' );
        ...
    }

    Using that you could add in as many product types as you need to like this.

    Thread Starter Lsnewton

    (@lsnewton)

    Is there some variation of print($this) that I could slip into the original plugin’s function temporarily that would tell me the name?

    Not that I know of. The reason is that the object itself doesn’t know what name it’s stored under. If it was me, I’d be looking for something that was creating a new object of the class name that you’ve got that original code out of. As an example…

    class Woocommerce_GFormClass {

    That’s what the class name would be, so I’d search for

    new Woocommerce_GFormClass

    And see what came up. That should show you where the object is being created, and that will give you the variables name like this…

    $my_var = new Wooconnerce_GFormClass( 'param' );

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Repeating a function from another plugin.’ is closed to new replies.