akewizjg
Forum Replies Created
-
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 4 weeks ago by 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" } }
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 slugwith 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()
replaceswpcf7dtx_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, 1 week ago by akewizjg.