Save url slug to hidden field with Fluent Forms

tdrayson

In this short tutorial, I am going to show you how to get the slug from a url.

1. Create the fields

We need to create two fields:

  • Text field with name attribute – plugin_url
  • Hidden field with the name attribute – plugin_slug
Click here

2. Adding the code

We need to then hook into one of the many Fluent Form hooks that exist.

The hook we need is fluentform_insert_response_data

PHP

        <?php

add_filter('fluentform_insert_response_data', 'tct_get_url_slug', 10, 3);

function tct_get_url_slug($formData, $formId, $inputConfigs)
{
   if($formId != 47) { // Replace with your Form ID
      return $formData;
   }

    $formData['plugin_slug'] = basename($formData['plugin_url']);
   
    return $formData;
}
      
Copy

What we are doing in this hook is getting the input from the user. In this case it’s plugin_url and then usin g the built-in basename PHP function to extract the trailing name. (Or last part after the /)

Final output

As you can see from the image below, the function pulls the slug from the end of both url’s and saves them in the hidden field we created.

screenshot 2022 09 27 at 20.35.17

Note: Make sure to replace your form ID

👋🏻 Weekly Tutorial Digest

I send out a weekly newsletter with links to new tutorials written in the last week, you can subscribe below.

Newsletter

🔒I won't send you spam, I promise