• Resolved rawahapool

    (@rawahapool)


    Hi, I have a form that is printing checkboxes, what I need is I need to map the options of a checkbox as the $post_title variable, that’s it,

    so the output should be :

    ✓ post1

    ✓post2

    ✓post 3

    etc

    Here is my current code :

    // Fetch published 'checklist' posts
    
    function checklist_fetch_posts()
    
    {
    
        $args = array(
    
            'post_type' => 'checklist',
    
            'post_status' => 'publish',
    
            'posts_per_page' => -1, // Retrieve all posts
    
        );
    
        $checklist_posts = new WP_Query($args);
    
        echo '<form method="post" action="process_form.php">';
    
        if ($checklist_posts->have_posts()) {
    
            while ($checklist_posts->have_posts()) {
    
                $checklist_posts->the_post();
    
                // Access post data here
    
                $post_title = get_the_title();
    
                // $post_content = get_the_content();
    
                // Output the post information as desired
    
                // echo '<h2>' . $post_title . '</h2>';
    
                echo '<label>
    
                <input type="checkbox" name="' . $post_title . '" value="' . $post_title . '">
    
                ' . $post_title . '
    
                </label><br>';
    
            }
    
            echo '<input type="submit" name="submit" value="Submit">';
    
            // Restore original post data
    
            wp_reset_postdata();
    
        } else {
    
            // No posts found
    
            echo 'No checklists found.';
    
        }
    
        echo '</form>';
    
    }

    Thanks,

    let me know if any detail needed

Viewing 1 replies (of 1 total)
  • Thread Starter rawahapool

    (@rawahapool)

    Ok , so this code worked for me :

    add_filter('wpcf7_form_elements', 'custom_wpcf7_form_elements');
    
    function custom_wpcf7_form_elements($form)
    
    {
    
      global $post;
    
      global $wpdb;
    
      if ($post && 'product' === $post->post_type) {
    
        $productID = $post->ID;
    
      }
    
      $selection_name = get_post_meta(
    
        $productID,
    
        'checklist_selection',
    
        true
    
      );
    
      $database_table_name = $wpdb->prefix . 'selected_posts';
    
      // Prepare the query with proper variable escaping
    
      $selected_row = $wpdb->get_row(
    
        $wpdb->prepare(
    
          "SELECT * FROM $database_table_name WHERE selection_name = %s LIMIT 1",
    
          $selection_name
    
        )
    
      );
    
      if ($selected_row) {
    
        $chosen_items = $selected_row->selected_post_id;
    
        $chosen_ids_array = explode(',', $chosen_items); // Convert the string to an array of IDs
    
        $checklist_posts = array(
    
          'post_type' => 'checklist',
    
          'post_status' => 'publish',
    
          'posts_per_page' => -1, // Retrieve all posts
    
          'post__in' => $chosen_ids_array, // Filter by the chosen IDs
    
        );
    
        $checklist_query = new WP_Query($checklist_posts);
    
        $options = array();
    
        if ($checklist_query->have_posts()) {
    
          while ($checklist_query->have_posts()) {
    
            $checklist_query->the_post();
    
            $title = get_the_title();
    
            $options[] = $title;
    
          }
    
        }
    
        // Restore the global post data
    
        wp_reset_postdata();
    
      }
    
      // Create a new DOMDocument object
    
      $dom = new DOMDocument();
    
      $dom->loadHTML($form);
    
      // Find the specific HTML code block using XPath
    
      $xpath = new DOMXPath($dom);
    
      $elements = $xpath->query('//span[@class="wpcf7-list-item first last"]/label/span[text()="option 1"]/parent::*/parent::*');
    
      // Remove the found elements
    
      foreach ($elements as $element) {
    
        $element->parentNode->removeChild($element);
    
      }
    
      // Get the updated form HTML code
    
      $form = $dom->saveHTML();
    
      $itemToReplace = '<span class="wpcf7-form-control-wrap" data-name="checkbox-591"><span class="wpcf7-form-control wpcf7-checkbox" id="hidden-item"></span></span>';
    
      $form = str_replace($itemToReplace, $itemToReplace, $form);
    
      foreach ($options as $option) {
    
        $itemToConcatenate = '<span class="wpcf7-list-item"><input type="checkbox" name="checkbox-591[]" value="' . $option . '"><label><span class="wpcf7-list-item-label">' . $option . '</span></label></span><br>';
    
        $form = str_replace($itemToReplace, $itemToReplace . $itemToConcatenate, $form);
    
      }
    
      return $form;
    
    }
Viewing 1 replies (of 1 total)

The topic ‘Convert Normal PHP form to CF7 Form’ is closed to new replies.