I recently encountered a WordPress project where I needed to add some specific PHP code to a certain part of a page template. Obviously this is relatively straight forward, depending on the code, with usual WordPress theme files; this build however, was using the page builder plugin “Visual Composer” to construct all of it’s pages.

What’s a Page Builder? And What’s Visual Composer?

Page builder plugins are majorly popular in the WordPress community right now, especially with non-development minded users. In short they enable users with little or no coding experience to create more complicated layouts. Visual Composer is, at the time of writing, probably the most popular.

How to Insert Custom PHP into the Visual Composer Flow

I explored a few options. It’s not the best idea to add PHP directly into text editors for security reasons, so that was out. I couldn’t go into the page template files themselves and start dropping PHP/HTML in there as it wouldn’t appear in the Visual Composer layout order.

I thought about using the built in Visual Composer API to create a custom layout block, but my output was so simple I considered this to be overkill; plus I like to prevent “lock-in” if at all possible. For instance, if it were decided that the project wouldn’t use the Visual Composer plugin anymore, I’d like my code to still work using standard WordPress features.

Eventually I settled on building a custom shortcode to output my PHP code, meaning that anywhere you can use a shortcode in WordPress (including a Visual Composer Text Block) you can output custom PHP and HTML. Here’s a step by step guide for getting lines of PHP to output where you want them in any Visual Composer page layout:

Build a Custom Shortcode to Output the PHP

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

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.

Insert Into the Page using Visual Composer

Now that your shortcode is registered it can be called into Visual Composer using a “text block“. In the WordPress admin area, go to Pages and find the one you’d like to drop your custom PHP in to.

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

Visual Composer pop up to add Text Block

Visual Composer pop up to add Text Block

Then drag and drop this text block to the specific area you’d like the custom PHP from your shortcode to be displayed:

Custom shortcode displaying in full width Visual Composer column

Custom shortcode displaying in full width Visual Composer column

The above shows the shortcode in position using a full width Visual Composer column. It’s also possible to place the shortcode in more complicated layout types, like within one of the four adjacent columns:

Custom shortcode displaying within the four adjacent Visual Composer columns

Custom shortcode displaying within the four adjacent Visual Composer columns

Check PHP/HTML Output on the Page

Now that the shortcode is in your Visual Composer page layout, check the results of this on the page to ensure the output is as expected:

Custom PHP output positioned in full width column

Custom PHP output positioned in full width column

Custom PHP output positioned within four adjacent columns

Custom PHP output positioned within four adjacent columns

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.

I hope this quick guide helps other people facing the same issue; trying to add your own PHP into a page laid out with Visual Composer can be tricky, especially if you’re used to building bespoke WordPress templates as I am.

Buy Visual Composer Page Builder for just $34!

Posted by Tom Hirst

Consultant WordPress developer with 10 years experience freelancing for agencies and businesses globally. Available for hire.

Don't miss out!

Subscribe to The Remote Dev mailing list and get new posts sent to your inbox as soon as they drop:


  1. grate thank you so much


  2. Hi Tom, nice tutorial. How about putting an order number in a shortcode. We use custom thankyou pages and would like to output the order number. The code for the ordernumber is get_order_number(); ?>

    Is it possible to create a shortcode for this using your method?



    1. Hi Anne,

      Thanks for the kind words. Yep this should be possible as long as the get_order_number function works on the page it’s being called on via the shortcode.



  3. Hi Tom, great info! 🙂 Do you know if it is possible to display custom fields data within VC elements?

    I’m using ACF and trying to get some basic text displayed in VC element that don’t have a wysiwyg editor –I’ve tried it via ACF shortcodes.

    I’m aiming to make it so clients can edit texts once the website is fully developed and designed without messing with any VC element.



  4. Just what I was looking for – thank you!


  5. Hello and thank you for the post. I’m facing a similar need for this however the shortcode has to work inside an HTML form input field so am trying to assign it to value=”[my_shortcode]”. Not only does this not work but the WordPress page does not even save the text block correctly and the dashboard ends up on the edit Post area (??) after the save completes (i.e., it’s very erratic behavior as if the PHP code in the html form causes internal problems). Are there known problems when trying to insert the shortcode like this into a form field, or have you done this type of thing successfully with Visual Composer? Basically trying to store the input fields of a form and then echo them back out to the user, so need PHP to echo the vars in the form. This would normally be easy in a pure PHP page/script, but it is the merging of this functionality with the VC visual components (which I need for the layout) that seems to pose a unique hurdle. Any info or direction is appreciated.


  6. Just what I was looking for, thanks!


  7. Hey Tom,

    Any chance you know of a good plugin that is compatible with Visual Composer that will allow me to just insert raw PHP into and work with that PHP out of the box?

    Thanks for your time.



  8. Fantastic! I’ve been looking for a way to use PHP (and other code) in Visual Composer. This is great – thank you so much. Keep up the good work : )


  9. Hello i am adding custom form in page builder and trying to fecth details but its not fetching it showing me that php file not found. Can you please help to solve this issue. Below i mentioned my website you can check when click on submit button.


  10. Thanks for this! Well played, sir.


Leave a reply

Your e-mail address will not be published. Required fields are marked *