Introduction
Would you like to retrieve the link from Conversational Forms and use this link inside a PHP template from your theme? Using a small PHP snippet you can easily grab this permalink so that you can use a PHP echo statement to display this link inside one of your theme’s PHP templates. In this tutorial, we’ll walk you through each step on how to retrieve this permalink and then echo the link out inside a PHP template.
Creating your form
You’ll first need to create your conversational form. If you need any help with this, please review this documentation.
Getting the link from the conversational form
Now we need to get the link that is generated from the conversational form. To do this, you’ll need to add this snippet to your site.
If you need any help in adding snippets to your site, please see this tutorial.
/** * Get permalink from conversational forms. * * @link https://wpforms.com/developers/how-to-retrieve-and-use-the-link-from-conversational-forms */ function get_the_conversational_form_link( $form_id ) { // Form ID should be numeric. // Permalinks should be configured. if ( ! is_numeric( $form_id ) || empty( get_option( 'permalink_structure' ) ) ) { return; } $form = wpforms()->form->get( $form_id ); $form_data = wpforms_decode( $form->post_content ); // Check if Conversational Form Mode is enabled. if ( empty( $form_data[ 'settings' ][ 'conversational_forms_enable' ] ) ) { return; } return esc_url( home_url( isset( $form->post_name ) ? $form->post_name : '' ) ); }
This function is a stand-alone function that will take the $form_id that gets assigned when calling the function and will retrieve the link to the conversational form for that form.
When you use the echo get_the_conversational_form_link( $form_id );
function inside any of your PHP templates, the function will get the permalink for that particular form ID and echo the link inside your template.
Using the function
Now it’s time to use this newly created function inside one of our theme templates.
We never recommend making any changes to your themes PHP files without first creating a child theme to do so. If you need help in creating a child theme, please review this tutorial from our friends at WPBeginner.
For the purpose of this documentation, we’ve already created our child theme and we’re going to edit the template using our favorite editor.
So after our main content, we’re going to call the function to display a link to the conversational form we’ve already created in a previous step so that it appears at the very bottom of every post page we have on our site. In order for this to work, we’ll call the new function from our child theme’s single.php template.
<?php /** Time to get the permalink from the conversation form **/ // Define the $form_id variable with the form ID we want to use $form_id = 718; ?> <p>Have you tried this recipe before? Let us know what you though! <a href="<?php echo get_the_conversational_form_link( $form_id ); ?>" title="Cajun Steak Bites Recipe" target="_blank">Click here to leave a review for this recipe.</a></p>
Now that we’ve placed our new function and text inside the PHP template, we can now see it in action on every single page of our blog.
And that’s all you need in order to retrieve and use the link from conversational forms. Would you like to also add your own styling to these pages? Check out our tutorial on How to Enqueue a Stylesheet for Conversational Forms.