How To Add PHP Code To Elementor

blue elephant plush toy on black laptop computer
Image source - Pexels.com

Custom Functions in Child Theme's functions.php:

 If you are familiar with coding and WordPress development, you can create a child theme (if you haven’t already) and add your custom PHP functions in the child theme’s functions.php file. These functions can then be called from Elementor’s widgets or pages using shortcodes.

In this example we create a function for auto date in footer of website

				
					// Shortcode to output custom PHP in Elementor
function wpc_elementor_shortcode( $atts ) {
   echo  "Copyright © Imaginations Hub". date("Y-m-d");
}
add_shortcode( 'auto_date', 'wpc_elementor_shortcode');
				
			

after creating this function in function.php file your just need to add first perameter name in your shortcode elementer where you want to display the output of your php code.

in our case it is “auto_date” so our shortcode is “[auto_date]”  write in square brackets [].

you can still add PHP functionality to your Elementor-powered website by following these steps:

    1. Using a Custom Code Widget: Elementor provides a “Custom Code” widget that allows you to insert custom HTML, CSS, and JavaScript. While it doesn’t directly support PHP, you can use this widget to add PHP code indirectly by embedding PHP inside HTML or JavaScript snippets.
    2. Code Snippets Plugin: Install and activate a plugin like “Code Snippets” (https://wordpress.org/plugins/code-snippets/), which allows you to add PHP code snippets to your WordPress website. Once you’ve added your PHP snippet through the plugin, you can then use a shortcode to display its output on an Elementor page or widget.
    3. Using a Plugin with PHP Functionality: There are several WordPress plugins available that add specific PHP functionalities. For example, some plugins allow you to run PHP code within shortcode blocks, which can then be used within Elementor.

       

Please remember that adding PHP code directly to your website comes with certain risks, as incorrect or poorly coded PHP can lead to errors or security vulnerabilities. If you are not familiar with PHP, it’s always a good idea to seek assistance from a professional developer to ensure your code is safe and effective.

Keep in mind that the Elementor platform may have evolved or introduced new features beyond my last update. So, I recommend checking Elementor’s official documentation and community forums for the latest information on adding PHP code or custom functionality to your Elementor pag

Related articles

You may also be interested in