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.

Useful Shortcode Examples to Use with WPBakery Visual Composer

As an added bonus we have included a list of useful shortcodes you can use throughout your WordPress website that can help you display a list full of dynamically populated data.

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

// WP Codeus Shortcode - Returns the site title.

add_shortcode( 'site-title', 'wpc_shortcode_site_title' );
function wpc_shortcode_site_title() {
    $blogname = get_option( 'blogname' );
    return $blogname;
}


// Wordpress Web Developement and Design Experts Shortcode - Returns the site description.

add_shortcode( 'site-description', 'wpc_shortcode_site_description' );
function wpc_shortcode_site_description() {
    $blogdescription = get_option( 'blogdescription' );
    return $blogdescription;
}


// https://wpcodeus.com Shortcode - Returns the site url.

add_shortcode( 'url', 'wpc_shortcode_site_url' );
function wpc_shortcode_site_url() {
    $siteurl = get_option( 'siteurl' );
    return $siteurl;
}


// contact@wpcodeus.com Shortcode - Returns the site admin email.

add_shortcode( 'admin-email', 'wpc_shortcode_site_admin_email' );
function wpc_shortcode_site_admin_email() {
    $admin_email = get_option( 'admin_email' );
    return $admin_email;
}


//  Shortcode - Returns the current users username.

add_shortcode( 'username', 'wpc_shortcode_username' );
function wpc_shortcode_username() {
    $current_user = wp_get_current_user();
    $username = $current_user->user_login;
    return $username;
}


//  Shortcode - Returns the current users email.

add_shortcode( 'email', 'wpc_shortcode_user_email' );
function wpc_shortcode_user_email() {
    $current_user = wp_get_current_user();
    $user_email = $current_user->user_email;
    return $user_email;
}


//  Shortcode - Returns the current users first name.

add_shortcode( 'first-name', 'wpc_shortcode_user_first_name' );
function wpc_shortcode_user_first_name() {
    $current_user = wp_get_current_user();
    $user_firstname = $current_user->user_firstname;
    return $user_firstname;
}


//  Shortcode - Returns the current users last name.

add_shortcode( 'last-name', 'wpc_shortcode_user_last_name' );
function wpc_shortcode_user_last_name() {
    $current_user = wp_get_current_user();
    $user_lastname = $current_user->user_lastname;
    return $user_lastname;
}


//  Shortcode - Returns the current users display name.

add_shortcode( 'display-name', 'wpc_shortcode_user_display_name' );
function wpc_shortcode_user_display_name() {
    $current_user = wp_get_current_user();
    $displayname = $current_user->display_name;
    return $displayname;
}


// 2024 Shortcode - Returns the Current Year as a string in four digits.

add_shortcode( 'year', 'wpc_shortcode_year' );
function wpc_shortcode_year() {
    $date = getdate();
    return $date['year'];
}


// April Shortcode - Returns the Current Month as a text string containing the month name.

add_shortcode( 'month', 'wpc_shortcode_month' );
function wpc_shortcode_month() {
    $date = getdate();
    return $date['month'];
}


// 25 Shortcode - Returns the Current Day as a text string containing the day of the month.

add_shortcode( 'day', 'wpc_shortcode_day' );
function wpc_shortcode_day() {
    $date = getdate();
    return $date['mday'];
}


// © Shortcode - Returns the Copyright Symbol.

add_shortcode( 'copyright', 'wpc_shortcode_copyright' );
function wpc_shortcode_copyright() {
        return '©';
}


// ® Shortcode - Returns the Registered Symbol.

add_shortcode( 'registered', 'wpc_shortcode_registered' );
function wpc_shortcode_registered() {
        return '®';
}


// ™ Shortcode - Returns the Trademark Symbol.

add_shortcode( 'trademark', 'wpc_shortcode_trademark' );
function wpc_shortcode_trademark() {
        return '™';
}

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!