• Resolved 352digital

    (@352digital)


    Hi,

    I have 3 forms, each for different categories of custom post (leaderboard_entry) – they’re scores for 3 offline puzzle box games.

    Rather than have the user choose the category, i want to set the category when the CF7 is sent.

    In each CF7 form i have this changing box1, box2 and box3:

    [select your-category class:hidden-select value=”box1″]

    Because it needs a select for taxonomies, i am just passing one value but it’s not being picked up so i’m trying to set the category after.

    // Function to update the category of a post
    add_action('CF7_2_POST_FORM_POSTED', 'update_leaderboard_entry_category', 10, 3);
    function update_leaderboard_entry_category($cf7_key, $submitted_data, $submitted_files){
        // Get the ID of the post that's being created by the form submission
        $post_id = isset($submitted_data['cf7_2_post_id']) ? intval($submitted_data['cf7_2_post_id']) : null;
    
        // Make sure we have a valid post ID
        if($post_id) {
            // Set the category of the post
            $term = 'box1';  // replace with your category slug
            $taxonomy = 'box';  // replace with your taxonomy name
            wp_set_object_terms($post_id, $term, $taxonomy, true);
        }
    }
    

    Is this the way? ?? After quite a few hours on it, i’m hoping you have a better idea to achieve this!

    Thanks in advance for any help you can give!

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Aurovrata Venet

    (@aurovrata)

    Rather than have the user choose the category, i want to set the category when the CF7 is sent.

    what do you mean by before it is sent:

    • you mean before the form is displayed – you can achieve this with the prefill functinoality of the plugin, see the helper filter #7 in screenshot #8, read FAQ #9 also.
    • or after the form is submitted – in this case you can achieve this by mapping your post field to a “Hook with filter” and programmatically saving the term value you need. (See screenshot #10)

    Thread Starter 352digital

    (@352digital)

    Hi – maybe i didn’t explain well. What i mean is, i’m trying to pass the category to post my cf7 form using this in the cf7 form:

    [select your-category class:hidden-select value=”box1″]

    This correctly passes box1 as the category but Post my cf7 Form is not registering it when it creates the post. The posts are saved with no category.

    If i put box1, box2, box3 as options in the select it works perfect but i do not want users to choose the category and it should be set per form.

    Looking at this in the settings now, i do not see the name of the taxonomy “boxes” “box”, https://snipboard.io/c2hKfG.jpg but think it should be there?

    Plugin Author Aurovrata Venet

    (@aurovrata)

    This correctly passes box1 as the category but Post my cf7 Form is not registering it when it creates the post. The posts are saved with no category.

    the plugin uses the std $_POST object submitted by the form submit request, so your field is not being set properly and its value missing form the form submit request.

    If i put box1, box2, box3 as options in the select it works perfect but i do not want users to choose the category and it should be set per form.

    you need at least 1 option, this is the reason the value is not being set properly. Try setting option box1 to match your preset value. Alternatively you can set all options and disable the ones not to be used.

    Looking at this in the settings now, i do not see the name of the taxonomy “boxes” “box”, https://snipboard.io/c2hKfG.jpg but think it should be there?

    well if it is a public taxonomy with existing values it will show up. However, note that the plugin will then automatically fill your select field with all the current terms in the taxonomy….this is what the mapping does.

    What you want to do is a little different, so I suggest you programmatically set your post term by hooking the action at the end of the post creation/saving process.

    Thread Starter 352digital

    (@352digital)

    Thanks for your reply – you said:

    programmatically set your post term by hooking the action at the end of the post creation/saving process.

    That is what my php at the top of the ticket is about – when i’m doing that using CF7_2_POST_FORM_POSTED it doesn’t seem to find the post id so doesn’t work.

    Using this in the CF7 form seems to be the same too and looks like box 1 is passed to the nest step just not as a category perhaps:

    [select your-category class:hidden-select default:1 “box1” “box2” “box3”]

    In the email sent to the user:

    When i do the mapping, initially i see the taxonomy info and click Save:

    I connect it to your-category here and Save the form:

    Then i see this and Boxes has disappeared like it’s not connected anymore –

    Boxes is not in the dropdown list like you see in the first screenshot i sent BUT box is filled in the slug field…:

    Is perhaps this not functioning like it should the cause of the issue and the code at the top should be working to save the category after the form is sent?

    Thanks again for your help! This is the last piece of a puzzle and i’ve been scratching my head for days about it!

    • This reply was modified 1 year, 4 months ago by 352digital. Reason: added screenshots
    • This reply was modified 1 year, 4 months ago by 352digital.
    Thread Starter 352digital

    (@352digital)

    // Save the your_category in transient
    add_action('wpcf7_before_send_mail', 'save_your_category_in_transient');
    function save_your_category_in_transient($contact_form){
        $submission = WPCF7_Submission::get_instance();
        if ($submission) {
            $posted_data = $submission->get_posted_data();
            
            $your_category = sanitize_text_field($posted_data['your-category']);
            
            // Save the your_category in the transient for 1 hour
            set_transient( 'your_category', $your_category, HOUR_IN_SECONDS );
        }
    }
    // Function to update the category of a post
    add_action('save_post_leaderboard_entry', 'assign_category_to_post', 10, 3);
    function assign_category_to_post( $post_id ) {
        // Get the your_category from the transient
        $your_category = get_transient('your_category');
    
        // If your_category is not empty, get term and set it for the post
        if (!empty($your_category)) {
            // Get term by slug
            $term = get_term_by( 'slug', $your_category, 'box' );
    
            // If term exists, set it for the post
            if ($term) {
                wp_set_object_terms( $post_id, $term->term_id, 'box' );
            }
    
            // Delete the your_category transient
            delete_transient('your_category');
        }
    }

    In the end i managed it with this above and in my form

    [select your-category class:hidden-select default:1 "box1" "box2" "box3"]

    Setting category as a transient with wpcf7_before_send_mail from the form and updating the custom taxonomy after. Couldn’t work out how to use the plugin to set a category instead of making the user choose it.

    Plugin Author Aurovrata Venet

    (@aurovrata)

    add_action('cf7_2_post_form_submitted_to_my-custom-form', 'new_my_custom_form_mapped',10,4);
      /**
      * Function to take further action once form has been submitted and saved as a post.  Note this action is only fired for submission which has been submitted as opposed to saved as drafts.
      * @param string $post_id new post ID to which submission was saved.
      * @param array $cf7_form_data complete set of data submitted in the form as an array of field-name=>value pairs.
      * @param string $cf7form_key unique key to identify your form.
      * @param array $submitted_files array of files submitted in the form, if any file fields are present.
      */
      function new_tabbed_form_mapped($post_id, $cf7_form_data, $cf7form_key, $submitted_files){
        //do something.
      }

    Not sure where you got that code from, but this is the action you want to hook. NOTE: this is from my test server My Custom Form so you will need to get your own helper code.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Set category instead of asking users to choose’ is closed to new replies.