Search code examples
phpwordpressclassoopwpbakery

Add Variable to Class Name


I am currently developing an element for WPBakery in Wordpress.

Because I want to nest the elements into each other I have to append a unique ID to the class of the element. The Variable $uniqueID shoud be appended to the Class "WPBakeryShortCode_vc_BootstrapCol"

But I can't do that in this way:

if ( class_exists( 'WPBakeryShortCodesContainer' ) ) {
        class WPBakeryShortCode_vc_BootstrapCol . $uniqueID extends WPBakeryShortCodesContainer {
    }
}

Can someone give me a hint on how to do this?

If I write the id behind it myself it works. Like this:

if ( class_exists( 'WPBakeryShortCodesContainer' ) ) {
        class WPBakeryShortCode_vc_BootstrapCol9346f805fc69ff0bb9f83511df30b1a9 extends WPBakeryShortCodesContainer {
    }
}

Solution

  • You can use eval()

    if ( class_exists( 'WPBakeryShortCodesContainer' ) ) {
        eval("class WPBakeryShortCode_vc_BootstrapCol" . (int)$uniqueID . " extends WPBakeryShortCodesContainer {}");
    }
    

    Although this seems strange to me. Make sure you have a good reason to do that.