Adding PHP code to the Divi Theme

This post may contain referral links which may earn a commission for this site

Some of my fixes for the Divi theme require PHP code to be added to make the required change to the theme. This post briefly covers the various options and their various advantages.

A Quick Warning

First, a word of caution. PHP code added to Divi effectively runs as part of WordPress itself (which is also written in PHP). This means that errors in the PHP code or how it is added can break your site and prevent it from loading. Be sure to take a backup of any files you intend to modify, so that you can restore the old copy if anything goes wrong. Also make sure you have a way of restoring the files which doesn't rely on the WordPress dashboard and editor which may not be accessible if something goes wrong (use FTP, SFTP or your web host control panel instead).

Where to Add PHP Code to Divi

  • Add PHP code to the Divi theme functions.php file – The traditional way to add PHP code to a theme has been to add it directly into the theme's functions.php file. For Divi this is typically located at /wp-content/themes/Divi/functions.php. Where you place the code doesn't usually matter, so you can just place it at the end (but see the section below on PHP tags). The major downside with this method is that if you update the theme the functions.php file will be overwritten and your changes lost.
  • Add PHP code to a Divi child theme – To solve the above problem, child themes were invented. A child theme loads the main theme (Divi) and enhances it with the contents of its own files. For instance, PHP code in the child theme's functions.php file will be automatically "added" to the main Divi theme's functions.php file. The great thing about this is that code placed in the child theme is not overwritten when the main theme is overwritten.

There are other ways to add PHP to the Divi theme, which I hope to expand on in the future, but for now these are the two main options.

Understanding PHP Tags

While you don't need to know PHP code to copy in code from the tips I post, you do need to understand PHP tags to make sure you are placing the code in the right place. Placing the code incorrectly could cause errors (see the warning above) or simply cause the code not to work.

A PHP file has two modes: PHP mode and HTML mode. By default anything placed in a PHP file will be processed in HTML mode meaning, more or less, that it will be output straight to the browser without being executed. However, many files (such as functions.php) need to actually have code executed to do something useful. To achieve this, they switch into PHP mode by including an opening PHP tag (often at the very start of the file).

Once in PHP mode, anything placed in the file will be treated as PHP code and executed. It is possible to switch back to HTML mode at any time by including the closing tag, but for a file like functions.php this generally won't happen until right at the end of the file (and sometimes there will be no switch back to HTML).

When adding PHP code to a functions.php file, the main thing to ensure is that the code is added in a place which is operating in PHP mode (so somewhere between an opening and a closing tag). The best place is usually just before the final closing tag, or right at the end of the file if there is no final closing tag.

Hundreds of new features for Divi
in one easy-to-use plugin

35 Comments

  1. Hi

    Any tips about adding the php file to enable we courseware to be configured with the divi builder (or any other tips about using WP courseware with Divi to get the best out of it?😜)

    Thanks

    Reply
    • Hi Steven, I haven't tested it specifically with WP Courseware, but this general method of enabling the Divi Builder on custom post types (such as those used by WP Courseware) should work:

      https://divibooster.com/enable-divi-builder-on-custom-post-types/

      If you add the code manually (such as in a child theme's functions.php file), or install Divi Booster which will add the code for you, then that should enable the Divi Builder on WP Courseware. If it doesn't help, or you spot any conflicts with WP Courseware, let me know and I'll try to get it working for you. Thanks!

      Reply
  2. Hello, I'm Brazilian and I need help. I added the PHP code to the Divi theme file functions.php directly and now my site went offline. What do I do? Please help me.

    Reply
    • Hi Lucas, sorry to hear this. This can happen if there is a mistake in the PHP or if it is added incorrectly. One way to fix it is to delete the code you added (being careful to delete exactly the code you added and nothing else) and resave the functions.php. Another is to replace the functions.php with the original copy of functions.php from the same version of Divi that you're using. So if you are on the latest version of Divi you could re-download Divi and copy the functions.php file out of it. If you are on an older copy of Divi then you might not be able to get hold of the correct functions.php version. In that case I'd recommend downloading the latest version of Divi and uploading it (by FTP) to the themes directory of WordPress, overwriting the existing directory (take a backup of the existing directory first). Let me know if none of these fix it for you.

      Reply
  3. I'm working with a DIVI support advisor and he is telling me I have to update my php to 5.7 and to let my hosting company know. It's currently running 5.52. Is this going to be the answer for some issues I have? I doubt the hosting company is in a position to simply upgrade my php….is that something they can do easily and will do?

    Reply
    • Hi Peter, it's certainly possible that updating php would solve some issues. A plugin, for example, that has been programmed using the newer features of php might cause errors when run on an older version of php. Whether php will solve your specific issues I can't say without knowing the details – did your advisor say why he thinks updating to php is necessary?

      Your hosting company may in fact be in a position to simply upgrade your php. Mine for instance (just a regular hosting plan) offers a range of php versions for my account and even gives me the ability to change it myself from my account control panel.

      It's important to be aware that updating php can itself introduce other issues (e.g. if you're running an older plugin which relies on an older version of php). To minimise any issues, you may want to backup your site first, apply the latest plugin updates and perhaps temporarily disable any not-so-important plugins. Also, do check if your host gives you the ability to change the php version yourself as this would let you try out the new version and then easily revert back to the old version if need be (your host could revert for you – it might just take a bit longer).

      Hopefully that helps, but if you like feel free to give me some details of the issues you've been having and I'll try to advise you on (what I think is) the best course of action.

      Reply
  4. Hi Dan,

    What to do if you want to REPLACE some code in the functions.php using a child theme?

    Reply
    • Hey Jonas, some of the functions in Divi's functions.php are "pluggable". These functions start with a check like:

      if ( ! function_exists( 'et_divi_fonts_url' ) )
      

      This means your child theme's functions.php can define these functions and your definitions will be used instead of Divi's. Start by copying the entire function into your functions.php, then make the changes you want there.

      Note that this is not ideal, as any improvements / changes to the function made in future versions of Divi won't be reflected in your site.

      Is there something in particular you are trying to do? Sometimes it will be possible to come up with a workaround (using hooks and filters, etc) that ends up having the same effect as directly replacing the code.

      Reply
  5. Hi I want to modify the way divi shows when you access an authors page. Now it only appears the lastest posts of the author. I just want to add the photo and description. I saw some papers about creating an author_page.php but only related to other themes. Maybe you know some tutorial or something to make this possible in Divi? Thank you

    Reply
    • Hi Galo, I couldn't find any Divi-specific tutorials for this, so I've started my own. It's a bit rough, but hopefully it gives you a bit of a start:

      https://divibooster.com/customizing-divi-author-pages/

      As detailed in the post, it's possible to a variant of the author_page.php method you mention. There's also a plugin that lets you use the Divi Builder on the author pages.

      Reply
  6. Hi – I'm trying to add a subtitle to the paget title that will show on category archive pages. I've added php to my functions.php as shown here and it's inserting almost exactly like I want it to. My problem is that I want to insert before and after my insert and it's showing in title like this: My Subtitle Text so I can read the html in the title. Any idea why?

    Reply
    • Code examples were removed in the about question when I posted. For clarity, I just want to have span tags to add a class to my text insert. The html is showing in the subtitle as if it were text and not code. Not sure how to address this.

      Reply
      • Hi dpeschio, I think I get what you're saying. It sounds like the span tags are being "html entity encoded" which just means they are being converted from HTML into plain text that will display as is on the page. The solution is to avoid the html entity encoding of the span tags, but how you do this will depend on what you have in the functions.php file. I've just sent you an email. If you'd like to reply to that with a copy of the code you're adding, I should be able to tell you what needs to be done. Cheers.

        Reply
  7. I want to add a snippet to the DIVI Theme functions.php but it has no closing tags like this ?>

    So I don't know if I can paste the snippet just at the end of th page .. weird.

    Reply
    • Hi petra, it might be helpful to understand what exactly the php tags do. Basically, they switch the language used in the .php file between HTML and PHP. Starting from the top of the file, any code is intially treated as HTML. However, when an opening <?php tag is encountered, the file switches into PHP mode and subsequent code is treated as php code. Then, if a closing tag ?> is encountered, the file switches back to HTML mode. This can happen repeatedly in a .php file.

      So when adding code, you need to be aware of what mode the file is in at the point you paste your code. If it is in HTML mode, and you want to insert PHP code, you need to surround the PHP code in opening and closing tags, so that it switches to PHP mode for your code and then returns to HTML mode once done.

      In most cases, things are quite simple in the functions.php file. As this file isn't usually involved in outputting HTML, it is usually in PHP mode from start to finish. As a result, you can usually paste in PHP code without either the opening or closing tag. So probably you'll be able to paste your code at the end of the page (before any existing final closing tag), without including either the opening / closing tags.

      If you're still uncertain, as long as you are able to reverse your changes (e.g. by keeping saving a copy of the functions.php file before you start), you can probably just try putting in your code and seeing what happens. If you get something wrong, you'll almost certainly know about it as the site will most likely break. Hence why you need to be able to reverse your changes!

      Reply
  8. Hello, I do not see why the full code in php.footer

    Reply
    • Hi Paul, I'm not sure I understand what you're asking, sorry. Is there any chance you can explain in a bit more detail what you're asking? Feel free to write in another language if this is easier – I'll translate. Cheers!

      Reply
  9. I want to add a small chunk of code to a page. Something like:

    display() ;
    ?>

    Is that possible?

    Reply
    • I guess it didn't like what I put:

      include( "displayClass.php" );
      $addressBook = new displayData( "addressBook" ) ;
      $addressBook->display() ;

      Reply
      • Hi Jeff. In theory, yes, you should be able to add this code. However there are a couple of things to be aware of. First is that it tries to load the displayClass.php file from the same directory. That means wherever you use the code, you'll probably want to have the displayClass.php file in the same folder. For example, if you were to put the code into one of the .php files in the root folder your child theme, you probably would need to put a copy of displayClass.php into the child theme root folder as well. Second, I'm guessing that the code outputs html to display the address book, and that it does so at the time it is run. This means that the place where you put this code is important – as that's where the address book will show up. This means, for example, that you couldn't just add it into the functions.php file (or it would be displayed before anything else in the page). Depending on what you need, you may find it useful to use a plugin such as this:

        https://wordpress.org/plugins/insert-php/

        It lets you add PHP anywhere you'd normally be able to use a shortcode. Note that if you do, you may have problems with the location of the displayClass.php file. Probably the way to solve that would be to change the include() line to specify the full file path for the displayClass.php file, rather than just the file name.

        Hopefully that helps a bit!

        Reply
    • Hi Richard, if you're using a child theme, it will usually be in the main directory of the child theme and called functions.php. Otherwise, Divi's own functions.php file is usually found at /wp-content/themes/Divi/functions.php within your WordPress installation directory.

      Reply
  10. Can i create own module inside divi builder??

    Reply
    • It's technically possible, but not at all easy. The way to do it would be to copy the code for one of the existing Divi modules (located in /includes/builder/main-modules.php) and modify it to suit your needs.

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *