Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Add a custom heading based on referrer URL and fetch title
- function wbcom_add_custom_heading_based_on_referrer($form) {
- // Get the referrer URL
- $referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
- // Define the default heading
- $default_heading = 'Please navigate to the event page to click on the Buy Ticket button.';
- // Fetch the post title and ID from the referrer URL
- $post_data = wbcom_fetch_post_data_from_url($referrer, $default_heading);
- $headingToDisplay = $post_data['title'];
- $post_id = $post_data['id'];
- // Add the heading to the form description
- $form['description'] = '<h2>' . esc_html($headingToDisplay) . '</h2>' . $form['description'];
- // Populate hidden fields with post title and post ID
- foreach ($form['fields'] as &$field) {
- if ($field->type == 'hidden') {
- if ($field->id == 15) { // field_4_15
- $field->defaultValue = esc_attr($headingToDisplay);
- } elseif ($field->id == 16) { // field_4_16
- $field->defaultValue = esc_attr($post_id);
- }
- }
- }
- return $form;
- }
- // Function to fetch post data from URL using WordPress REST API
- function wbcom_fetch_post_data_from_url($url, $default) {
- if (empty($url)) {
- return array('title' => $default, 'id' => '');
- }
- // Parse the URL to get the path
- $path = parse_url($url, PHP_URL_PATH);
- if (empty($path)) {
- return array('title' => $default, 'id' => '');
- }
- // Remove leading and trailing slashes
- $path = trim($path, '/');
- // Get the last part of the path, assuming it's the post name (slug)
- $post_name = basename($path);
- // Fetch the post by name
- $post = get_page_by_path($post_name, OBJECT, 'tribe_events');
- // If post is found, return the title and ID, otherwise return the default
- if ($post) {
- return array('title' => $post->post_title, 'id' => $post->ID);
- } else {
- return array('title' => $default, 'id' => '');
- }
- }
- // Apply the custom heading function to the specific form using gform_pre_render
- add_filter('gform_pre_render_4', 'wbcom_add_custom_heading_based_on_referrer'); // Form ID 4
- add_filter('gform_pre_process_4', 'wbcom_add_custom_heading_based_on_referrer'); // Form ID 4
- add_filter('gform_pre_validation_4', 'wbcom_add_custom_heading_based_on_referrer'); // Form ID 4
- add_filter('gform_pre_submission_filter_4', 'wbcom_add_custom_heading_based_on_referrer'); // Form ID 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement