I need to capture a value displayed on the website to the user, and then store it in a variable. Ideally, I'd like to do this in functions.php.

More specifically, we use dynamic phone number replacement on the sites, and we’d like to know which phone number is displayed to the user when they are submitting a contact form (via Gravity Forms).

I can wrap the phone number in a class or ID, but am struggling to understand how to write a function to pull this value into a variable.

I have submitted my question to Gravity Forms, but they will not assist. Thanks.

  • 1
    I had a similar task with Contact Form 7, my approach was to add a hidden field to the form in CF7 and then write some jQuery to assign the value to the hidden field when the page was ready.
    – Daniel
    Aug 21, 2017 at 18:06
  • the jquery code is what I need, I suppose. I have the hidden field in the form, and don't know how to get the phone number there...
    – Erin M.
    Aug 21, 2017 at 19:31

2 Answers 2


I had a similar task with Contact Form 7, my approach was to add a hidden field to the form in CF7 and then write some jQuery to assign the value to the hidden field when the page was ready.

        document.getElementById('hiddeninputID').value = document.getElementById('cphone').value;

Using the above script is the simplest way to do what you want to achieve, using PHP will work but that may require you to update the PHP code along with the regular Gravity forms updates. The script won't impact on page speed and should always be compatible. Replace hiddeninputID to the ID of the input field that you've added


To take into account the example provided:

    jQuery(document).ready(function() {
        var mmPhone = document.getElementById("mm-cphone").innerHTML;

  • Thank you. I'll try to implement this. Did you place the script on the page with the CF7 form, or did you add this to a separate file to include in your theme? I have very limited php/javascript/jquery experience.
    – Erin M.
    Aug 22, 2017 at 13:23
  • I added it within the form itself, CF7 uses shortcode to add fields so the code is placed just before the submit button
    – Daniel
    Aug 22, 2017 at 14:01
  • Thank you! This was very helpful. I was able to add some Javascript code above the submit button on my form in an HTML block field. It will successfully output the desired number during testing. When I pass the value to the hidden form field, it changes back to the hardcoded value though so I've still got work to do here.
    – Erin M.
    Aug 24, 2017 at 11:33
  • I'm happy to help, can you share a URL? It could just be that the assignment of the values to each other could be the wrong way around e.g. a= b when it should be b=a
    – Daniel
    Aug 24, 2017 at 11:34
  • Thank you! Below is a URL on the development site. The 844-259-6965 number in the top left of the header is the dynamically replaced number that we want to pass to the "Phone Number Displayed to Visitor" field. As you will see it is showing 888-979-2140, which is incorrect. 888-979-2140 is the hardcoded number before it is replaced. The HTML block is showing the number I want to pass to the field above it. ww.maddserver.net/forms-test/?st-t=social
    – Erin M.
    Aug 24, 2017 at 15:16

I don't know how are you displaying the phone number but I think you can store needed value using gform_pre_submission hook from gravity form. First add some hidden input and than you can change the field value directly in the functions.php. Example from docs:

add_action( 'gform_pre_submission', 'pre_submission_handler' );
function pre_submission_handler( $form ) {
    $_POST['input_14'] = 'new value for field 14';

Update: You can't grab the phone number directly to php. You need some jQuery code to work on it. Something like var phone.

var phone = $('#cphone').text();
$('<some id of the hidden field>').val(phone);

Would be better to use data-attributes to store additional data for a DOM elements.

  • The phone number is wrapped in an id, for example: <span id="cphone">(888) 800-0000</span>. I need the code for functions.php that will get the phone number and store it in a variable, which I can then pass as a parameter in Gravity Forms. I have the following code, and can pass 'boom' to the form, but I'd like that to pull the (888) 800-0000 in this case. add_filter( 'gform_field_value_mm-cphone', 'my_custom_population_function' ); function my_custom_population_function( $value ) { return 'boom!'; }
    – Erin M.
    Aug 21, 2017 at 19:28
  • Updated answer.
    – Kort
    Aug 21, 2017 at 19:54
  • Thanks, Kort. Forgive my ignorance but where do I add jQuery code? I do not have the experience with this type of coding at all.
    – Erin M.
    Aug 21, 2017 at 20:05
  • You can add this in a main.js file in your theme. Also you should get some experience on the learning recourses because if it's hard to understand what I answered you probably do not correct changes. It's very basic things and I'm sure that you will understand very quickly.
    – Kort
    Aug 21, 2017 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.