• Resolved akewizjg

    (@akewizjg)


    Hi there,

    I need a shortcode for a dynamic text form tag, who can reveal an ID of custom post types (used CPT UI plugin) similar to post_title ( [dynamictext inputname “CF7_get_post_var key=’post_title'”] ).

    I tried with this:

    [dynamictext inputname “CF7_get_post_var key=’ID'”]

    …… it works, but it doesn’t give me the correct ID, shows the one automatically assigned by WordPress.

    I have CPT named ‘Jobs’ with a custom taxonomy named ‘Jobs ID’ ( id_jobs ->taxonomy slug ) for every Job/ post, which I want to use in the field. So I tried to use the slug as the value key=’id_jobs’ here [CF7_get_current_var key=”<insert key>”]

    [dynamictext inputname “CF7_get_post_var key=’id_jobs'”]

    It shows an empty string, because the key value is not correct or it’s not on the allow list.

    Anyone with an idea how to dynamically autofill the ID field (like post_title) to include the custom ID of the current post? Thanks in advance.

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

Viewing 8 replies - 1 through 8 (of 8 total)
  • Following!

    Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    Hi there! So if I’m understanding correctly, you want the term id that is currently assigned, using a custom post type and custom taxonomy? I think we can do that ??

    function akewizjg_job_id() {

    // Use helper function to get the current post id
    $post_id = wpcf7dtx_get_post_id(false);

    if(!$post_id) return ''; // Bail on failure

    // For simplicity's sake, define custom taxonomy here
    $taxonomy = 'jobs-id'; // I'm assuming this is your custom taxonomy's slug

    // Get the terms of the current post
    $terms = get_the_terms($post_id, $taxonomy);

    if(!is_array($terms) || !count($terms)) return ''; // Bail on failure/empty

    return $terms[0]->term_id; // Return the term id of the first (and probably only) term object
    }
    add_shortcode('akewizjg_job_id', 'akewizjg_job_id');

    Then in the mail template, use this for the form tag:

    [dynamic_text job_id "akewizjg_job_id"]

    Hope that’s what you needed, please let me know if I misunderstood ??

    Thread Starter akewizjg

    (@akewizjg)

    Hi Tessa ?? ,

    Thank you so much for your response – I really appreciate it. But something strange happens and it shows another ‘ID number’ than the custom taxonomy term ID I want or the one added by WordPress.

    I tested with the code you gave just replaced the taxonomy’s slug ‘jobs-id‘ here:

     // For simplicity's sake, define custom taxonomy here    
    $taxonomy = 'jobs-id'; // I'm assuming this is your custom taxonomy's slug

    with the one based on my setup in CPT UI – ‘id_jobs‘. taxonomy slug

    Here you can see screenshot of the mail template: form template

    … and what the test page with the contact form looks like: screenshot of the page See the correct Dynamic ID JOB: 20029 and the wrong one “32”, which I have no idea where it generated from. Any ideas what’s wrong and where it’s coming from?

    Thank you. ??

    p.s. I also tested a code that generated a chat GPT based on the one you give and it says: “The following function retrieves the term ID of your custom taxonomy id_jobs and returns it for use in Contact Form 7. Key Changes:

    • get_the_ID() replaces wpcf7dtx_get_post_id(false) to retrieve the post ID, which is more reliable unless you specifically need the helper function.
    • update the dynamic text field in your Contact Form 7 form to use the new shortcode:[dynamictext job_id "CF7_get_post_var key='akewizjg_job_id'"]
    function akewizjg_job_id() {
    // Get the current post ID using WordPress function (in case the other one isn't available)
    $post_id = get_the_ID();

    if (!$post_id) return ''; // Bail on failure

    // Define the custom taxonomy slug here
    $taxonomy = 'id_jobs'; // Ensure this is the correct slug from CPT UI (replace 'id_jobs' with the actual slug if different)

    // Get the terms associated with the current post for the custom taxonomy
    $terms = get_the_terms($post_id, $taxonomy);

    // Bail if no terms are found or if the result is not an array
    if (!is_array($terms) || !count($terms)) return '';

    // Return the term ID of the first term
    return $terms[0]->term_id; // Use term_id to return the custom taxonomy term ID
    }
    add_shortcode('akewizjg_job_id', 'akewizjg_job_id');

    I had to try…. however, nothing changed and the field remained empty. ??

    • This reply was modified 1 month ago by akewizjg.
    Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    Since the field is remaining empty, that means it’s hitting one of the fail states (probably). In that case, begin debugging by replacing the empty text with error messages:

    if (!$post_id) return 'No post ID found'; // Bail on failure
    if (!is_array($terms)) return "Terms did not return an array for {$taxonomy} for post {$post_id}";

    if (!count($terms)) return "No terms found assigned to post {$post_id}";

    var_dump('Assigned Terms', $terms); // Dump the variable to the page in your test environment to see what it is

    If you see the “Assigned Terms” object on the page, screenshot or copy/paste that here

    Thread Starter akewizjg

    (@akewizjg)

    Ok, sorry for the slow response ?? If I understood correctly, the code should look like this:

    function akewizjg_job_id() {
    // Get the current post ID using WordPress function (in case the other one isn't available)
    $post_id = get_the_ID();

    // Check if the post ID is valid
    if (!$post_id) return 'No post ID found'; // Bail on failure with an error message

    // Define the custom taxonomy slug here
    $taxonomy = 'id_jobs'; // Ensure this is the correct slug from CPT UI (replace 'id_jobs' with the actual slug if different)

    // Get the terms associated with the current post for the custom taxonomy
    $terms = get_the_terms($post_id, $taxonomy);

    // Check if terms are returned and if they are an array
    if (!is_array($terms)) return "Terms did not return an array for {$taxonomy} for post {$post_id}";

    // Check if the array of terms has any entries
    if (!count($terms)) return "No terms found assigned to post {$post_id}";

    // Use var_dump to output terms for debugging in the test environment
    var_dump('Assigned Terms', $terms); // Dump the terms variable to the page for inspection

    // Return the term ID of the first term
    return $terms[0]->term_id; // Use term_id to return the custom taxonomy term ID
    }
    add_shortcode('akewizjg_job_id', 'akewizjg_job_id');

    There is no change in the fields of the contact form, but you most likely want to see the code that appears under the header of the page –“Assigned Terms” . I will copy it here for clarity:

    string(14) "Assigned Terms" array(1) { [0]=> object(WP_Term)#1885 (10) { ["term_id"]=> int(32) ["name"]=> string(5) "20029" ["slug"]=> string(5) "20029" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(32) ["taxonomy"]=> string(7) "id_jobs" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(1) ["filter"]=> string(3) "raw" } } string(14) "Assigned Terms" array(1) { [0]=> object(WP_Term)#1888 (10) { ["term_id"]=> int(32) ["name"]=> string(5) "20029" ["slug"]=> string(5) "20029" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(32) ["taxonomy"]=> string(7) "id_jobs" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(1) ["filter"]=> string(3) "raw" } }

    Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    Ahhh, I see now. You named the term after the id number you wanted! I had incorrectly assumed you wanted the term id, which is assigned by WordPress when the term is created. In this case, just change that last line from

    // Return the term ID of the first term
    return $terms[0]->term_id; // Use term_id to return the custom taxonomy term ID

    to

    // Return the name of the first term
    return $terms[0]->name; // Use name to return the custom taxonomy term name

    and it should output the 20029 number you’re looking for!

    Also remove the var_dump() code and change those error messages back to empty when it’s all sorted ??

    Thread Starter akewizjg

    (@akewizjg)

    Yes, that’s right ?? ,

    I guessed from the codes in “Assigned Terms”, I tested and things happened, I have the exact ID [“name”]=> string(5) “20029” , which I wanted to autofill, just wanted to test it “live on the site” if everything works before confirming here. It even works perfectly with a form tag ‘Dynamic Hidden’

    I’m just wondering which of the two shortcodes is more correct, why both give the same result and if there is any significance? I mean id_jobs and job_id ….does it matter or whatever is used is ok ?

    [dynamictext id_jobs "akewizjg_job_id"]

    [dynamic_text job_id "akewizjg_job_id"]
    Sincere thanks for your help, I couldn’t have done it without the var_dump() code really.

    We can mark it as a resolved case ?? Thanks again. ?? ?? ??

    • This reply was modified 3 weeks ago by akewizjg.
    Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    why both give the same?result?and if there is any significance?

    They both give the same result because they are both calling the same shortcode within the quotes ("akewizjg_job_id") so the function that gets the job id is the same for both

    if there is any significance?

    The first significant difference is the form tag’s name. That name is what you would use in the mail template if sending emails. If you’re using some kind of contact form storage plugin like Flamingo, it’ll likely use that value as its reference key. It’s something you choose, so just pick the one you want to use ??

    The second significant difference is the form control element. The top uses dynamictext (or dynamic_text) which outputs a visible text field. If you want the person who is submitting the form to be able to see the form id, this is a good choice. I’d also recommend adding the readonly attribute so the user cannot edit it. The bottom one (from your screenshot) uses dynamic_hidden which puts the job id in a hidden field. This is perfect if you’re only using the job id for internal usage.

    Glad it’s all working!

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.