Get 50% off all new Gravity Forms licenses today!

Buy Now
Blog -

How to Troubleshoot WordPress Code Snippets in Gravity Forms

Morgan Kay By Morgan Kay Published February 22, 2022

Troubleshoot WordPress Code

One of the strengths of Gravity Forms is that it is very extensible and customizable. Sometimes, to do the customization you want, you need to either write some code, or use an existing code snippet. Our documentation and community forums are full of code snippets that can make your forms do all sorts of amazing things.

If you’re not an experienced developer, though, those code snippets can be daunting. If you paste a code snippet into your site and it doesn’t work, you’re stuck.

In this article, I will go over some basic troubleshooting steps you can use to figure out why that code snippet you copied and pasted from our documentation isn’t working on your site.

Note: I am only going to cover troubleshooting techniques for PHP code snippets. For JavaScript and CSS, the concepts are the same, but the code is different.

Let’s get started…

Using Logging Messages

I’m going to focus on how to use logging messages to debug your code. That is, as the code runs, we’re going to write some statements to a log, and then we can check the logs and see what’s happening. There are lots of different ways to log what’s happening in PHP, but Gravity Forms has its own built-in logging system.

To use Gravity Forms’ logging, go to the Gravity Forms settings page and enable logging. Then you’ll need to open up your log file. The easiest way to do this is to go to the Gravity Forms settings page and click on Logging in the left-hand menu. You can open the log file in a browser, or you can find the file in your uploads directory and open it with a text editor.

Troubleshoot WordPress Code Snippets

The simplest way to add a logging statement is just to add this to your code:

GFCommon::log_debug( ‘the message you put in quotes here will show up in the log’);

 

Caveat

Any time you put custom code on a site, you could end up causing fatal errors on the site. If at all possible, you should test code snippets on a staging site or a development site so that you don’t impact your site visitors.

Make sure the code is running

If you pasted a code snippet into your site and nothing happened, the first step is to make sure that the code snippet is actually running. It’s possible that you’ve pasted it somewhere where it will never run. For instance, if you are putting the code in your theme’s functions.php file, and you have multiple themes installed on your site, it’s easy to accidentally put the code in the wrong theme.

So to make sure your site is even trying to use your code, try adding a logging message right before the code snippet:

GFCommon::log_debug( 'code here is running');

Now you can visit a page on your site, and then check your log. If you’re viewing the log in the browser, you’ll need to refresh the page. At the bottom of the log, you should see something like this:

2022-01-27 15:42:17.250071 – DEBUG –> (): code here is running.

Debug - Code is Running

If you don’t see that message, that means that you’ve put the code somewhere where will not be executed, and you need to put it somewhere else. If this is the problem, using a code snippet plugin instead of putting your code in a PHP file might solve the problem.

If you do see the message, then that means that there is some other reason why your code isn’t being executed. In that case, the next step is to put a logging method inside your code snippet to see if the code snippet is running.

For example, if you’ve pasted this code from the documentation:

 add_action( 'gform_after_submission', 'set_post_content', 10, 2 );
function set_post_content( $entry, $form ) {
 
    //getting post
    $post = get_post( $entry['post_id'] );
 
    //changing post content
    $post->post_content = 'Blender Version:' . rgar( $entry, '7' ) . "<br/> <img src='" . rgar( $entry, '8' ) . "'> <br/> <br/> " . rgar( $entry, '13' ) . " <br/> <img src='" . rgar( $entry, '5' ) . "'>";
 
    //updating post
    wp_update_post( $post );
}

Here is how you would add a logging statement inside it:

 add_action( 'gform_after_submission', 'set_post_content', 10, 2 );
function set_post_content( $entry, $form ) 
{ GFCommon::log_debug( __METHOD__. '(): running.');
 
    //getting post
    $post = get_post( $entry['post_id'] );
 
    //changing post content
    $post->post_content = 'Blender Version:' . rgar( $entry, '7' ) . "<br/> <img src='" . rgar( $entry, '8' ) . "'> <br/> <br/> " . rgar( $entry, '13' ) . " <br/> <img src='" . rgar( $entry, '5' ) . "'>";
 
    //updating post
    wp_update_post( $post );
}

 

This particular code snippet is supposed to run after you submit any form, so after adding the logging message, you can submit a form, and then check your log. You should see something like this:

2022-01-27 15:35:41.164642 – DEBUG –> set_post_content(): running.

If you don’t, then your code isn’t running. There could be several reasons for this, but the most likely is that you are using the wrong action. In that first line, you see “add_action( ‘gform_after_submission’ ). That means this code will run after a form submission. Check the documentation for the action you’re using to make sure it is the action you actually want.

If you have the correct action, the next step is to check if the action ends in a number. With Gravity Forms actions, you can use ‘gform_after_submission’ to do this action for all forms, or you can add a form ID to the end of it to run it on only one form. So if you only want to do this on the form with an ID of 5, you would use this:

add_action( 'gform_after_submission_5', 'set_post_content', 10, 2 );

If the code isn’t running when you submit a form, make sure you are using the correct form ID, or remove the ID from the action so that it runs on all forms.

Code runs, but doesn’t work as expected

If your code is running, but doesn’t work the way you think it should, you can use logging messages to check the values of all the variables in your code. For instance, in the code snippet we’ve been looking at, we know that we want to change the post content. Let’s start by making sure that we are getting the right post:

 add_action( 'gform_after_submission', 'set_post_content', 10, 2 );
function set_post_content( $entry, $form ) {
 
    //getting post
    $post = get_post( $entry['post_id'] );
GFCommon::log_debug( __METHOD__ . '(): post is: ' . print_r( $post, true ) );
 
    //changing post content
    $post->post_content = 'Blender Version:' . rgar( $entry, '7' ) . "<br/> <img src='" . rgar( $entry, '8' ) . "'> <br/> <br/> " . rgar( $entry, '13' ) . " <br/> <img src='" . rgar( $entry, '5' ) . "'>";
 
    //updating post
    wp_update_post( $post );
}

Any time you want to know the value of a variable, you can put print_r( $variable, true ) in your debug message, just like I’ve demonstrated here. If you check your debug log, you should see something like this:

Debug

That is all of the information about the post we are updating, so you can make sure it is the post you expected. This snippet is using values from the form entry to populate the post content, so another useful log message might be this:

 add_action( 'gform_after_submission', 'set_post_content', 10, 2 );
function set_post_content( $entry, $form ) {
 
    //getting post
    $post = get_post( $entry['post_id'] );
 
    //changing post content
    $post->post_content = 'Blender Version:' . rgar( $entry, '7' ) . "<br/> <img src='" . rgar( $entry, '8' ) . "'> <br/> <br/> " . rgar( $entry, '13' ) . " <br/> <img src='" . rgar( $entry, '5' ) . "'>";
GFCommon::log_debug( __METHOD__ . '(): entry is: ' . print_r( $entry, true ) );
 
    //updating post
    wp_update_post( $post );
}

By examining the details of the entry variable in your log, you might see that you’re using the values for the wrong field in your code snippet.

Conclusion

Logging is a really powerful way to understand what is happening in your code and figure out why it isn’t working. If you want to learn more about logging and writing your own logging messages, you can find out more in our documentation.

 

Gravity Forms Newsletter

If you want to keep up-to-date with what’s happening on the blog sign up for the Gravity Forms newsletter!