Advertisement
vapvarun

Add a custom heading based on referrer URL and fetch title

Jun 7th, 2024
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.52 KB | None | 0 0
  1. // Add a custom heading based on referrer URL and fetch title
  2. function wbcom_add_custom_heading_based_on_referrer($form) {
  3.     // Get the referrer URL
  4.     $referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
  5.  
  6.     // Define the default heading
  7.     $default_heading = 'Please navigate to the event page to click on the Buy Ticket button.';
  8.  
  9.     // Fetch the post title and ID from the referrer URL
  10.     $post_data = wbcom_fetch_post_data_from_url($referrer, $default_heading);
  11.     $headingToDisplay = $post_data['title'];
  12.     $post_id = $post_data['id'];
  13.  
  14.     // Add the heading to the form description
  15.     $form['description'] = '<h2>' . esc_html($headingToDisplay) . '</h2>' . $form['description'];
  16.  
  17.     // Populate hidden fields with post title and post ID
  18.     foreach ($form['fields'] as &$field) {
  19.         if ($field->type == 'hidden') {
  20.             if ($field->id == 15) { // field_4_15
  21.                 $field->defaultValue = esc_attr($headingToDisplay);
  22.             } elseif ($field->id == 16) { // field_4_16
  23.                 $field->defaultValue = esc_attr($post_id);
  24.             }
  25.         }
  26.     }
  27.  
  28.     return $form;
  29. }
  30.  
  31. // Function to fetch post data from URL using WordPress REST API
  32. function wbcom_fetch_post_data_from_url($url, $default) {
  33.     if (empty($url)) {
  34.         return array('title' => $default, 'id' => '');
  35.     }
  36.  
  37.     // Parse the URL to get the path
  38.     $path = parse_url($url, PHP_URL_PATH);
  39.     if (empty($path)) {
  40.         return array('title' => $default, 'id' => '');
  41.     }
  42.  
  43.     // Remove leading and trailing slashes
  44.     $path = trim($path, '/');
  45.  
  46.     // Get the last part of the path, assuming it's the post name (slug)
  47.     $post_name = basename($path);
  48.  
  49.     // Fetch the post by name
  50.     $post = get_page_by_path($post_name, OBJECT, 'tribe_events');
  51.  
  52.     // If post is found, return the title and ID, otherwise return the default
  53.     if ($post) {
  54.         return array('title' => $post->post_title, 'id' => $post->ID);
  55.     } else {
  56.         return array('title' => $default, 'id' => '');
  57.     }
  58. }
  59.  
  60. // Apply the custom heading function to the specific form using gform_pre_render
  61. add_filter('gform_pre_render_4', 'wbcom_add_custom_heading_based_on_referrer'); // Form ID 4
  62. add_filter('gform_pre_process_4', 'wbcom_add_custom_heading_based_on_referrer'); // Form ID 4
  63. add_filter('gform_pre_validation_4', 'wbcom_add_custom_heading_based_on_referrer'); // Form ID 4
  64. add_filter('gform_pre_submission_filter_4', 'wbcom_add_custom_heading_based_on_referrer'); // Form ID 4
  65.  
Tags: Gravity from
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement