How To Add PHP Code to WPbakery Visual Composer

Wordpress Tutorial: Add PHP Code to WPbakery Visual Composer

How To Add PHP Code to WPbakery Visual Composer

In this tutorial we are going to show you how to add PHP Code to WPbakery Visual Composer.

At some point you may find yourself needing to add some PHP code to a certain part of a page template that is built using WPBakery Visual Composer. The default WPBakery Visual Composer however only has elements to add HTML and Javascript and doesn’t allow the adding of php code into any of those elements or the text block. So today we are going to show you how to add custom php code to any WPBakery Visual Composer page by creating a shortcode in your themes functions.php.

Don't Want To Touch Any Code?

Have one of our WordPress Experts add the following code snippets for you. Chill out while we do the hard work.

Get Help

Create a Shortcode to add PHP Code to WPbakery Visual Composer

Using the WordPress shortcode API guidelines, in your functions.php file (or other include depending on your theme setup) add the following code:

// Shortcode to output custom PHP in Visual Composer
function wpc_vc_shortcode( $atts ) {
    echo "This is my custom PHP output in Visual Composer!";
add_shortcode( 'my_vc_php_output', 'wpc_vc_shortcode');

Be sure to name the function and the shortcode something relevant to what you’re trying to output on the page, this is only a simple example.

If you need to output multiple custom php functions that is separate from the first you will need to copy the code above and make sure you change the name of the shortcode. You can not have the same name for two different shortcodes.

Add The Shortcode to a Page Using WPBakery Visual Composer

Now that your shortcode has been added and registered it can be called into WPBakery Visual Composer using a “text block”. Navigate over the the page that you are wanting to add the custom php to.

From there, add a WPBakery Visual Composer text block with your newly created shortcode called within it. For this guide the shortcode is [my_vc_php_output]

Add The Shortcode Into Page Using WPBakery Visual Composer

Now that the shortcode is in your WPBakery Visual Composer page layout, check the results by previewing or publishing the page to ensure the output is as expected.

In Summary

Obviously, there are more possibilities than returning a header tag using this solution; it can be expanded to serve any functionality possible with the WordPress Shortcode API, such as shortcodes that handle attributes.

We here at WP Codeus hope this quick guide helps other people facing the same issue; trying to add your own PHP code to any Visual Composer page.

Need Help with your Website?

Our Experts can handle any customization or developement at a reasonable price.

Get a Quote

Looking for more?

Here are some other articles that we think you will be interested in!