Skip Navigation

[Resolved] Trying to Retrieve Toolset Custom Field Value for TCPDF Document Generation

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 1 year ago.

Assisted by: Waqar.

Author
Posts
#2672681

Hi.
I am working on a custom WordPress plugin where I am trying to generate a PDF using the TCPDF library. My goal is to include data from a Toolset custom field into the PDF. Specifically, I am attempting to retrieve the value from a custom field named 'kursKode' associated with a post, and then display this value in a table within the PDF.

Here's the issue I'm encountering: When I use the function get_post_meta($post_id, 'kursKode', true) to retrieve the value of the 'kursKode' field, it seems to return an empty or null value, despite the field being populated in the WordPress backend. I have also tried this code: types_render_field('kursKode', array('post_id' => $post_id));

Could you provide some insight or guidance on how to correctly fetch and display the value of a Toolset custom field in a TCPDF document? Is there a specific consideration or method I should be using when working with Toolset fields in this context?

For your reference, here is a snippet of the code I'm using:

function Last_Ned_pdf()
{
if (!wp_verify_nonce($_REQUEST['nonce'], 'Last_Ned_pdf')) {
die('Ugyldig nonce');
}

$post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : 0;
if (!$post_id) {
die('Innleggs-ID ikke oppgitt eller ugyldig');
}

// Hent tittelen og dekod eventuelle HTML-entiteter
$tittel = html_entity_decode(get_the_title($post_id), ENT_QUOTES | ENT_HTML5, 'UTF-8');
if (!$tittel) {
die('Tittelen er ikke tilgjengelig');
}

// Hent innholdet fra WordPress-innlegget
$innhold = get_post_field('post_content', $post_id);
$innhold = html_entity_decode($innhold, ENT_QUOTES | ENT_HTML5, 'UTF-8');
$innhold = nl2br($innhold);

// Hent kurskode fra Toolset-feltet
$kursKode = $kursKode = get_post_meta($post_id, 'kursKode', true);

$filTittel = preg_replace('/[^a-zA-Z0-9_\-]/', '_', $tittel);
$dato = date('Y-m-d');
$tid = date('H-i-s');
$filnavn = $filTittel . '_' . $dato . '_' . $tid . '.pdf';

$pdf = new TCPDF();
$pdf->AddPage();
$pdf->SetFont('helvetica', 'B', 14);
$pdf->Write(0, $tittel);
$pdf->Ln(10);

$pdf->SetFont('helvetica', '', 10);
$pdf->writeHTML($innhold, true, false, true, false, '');

$pdf->Ln(10); // Ekstra plass etter innholdet

// Lage en tabell og inkludere kursKode
$pdf->Cell(40, 10, 'Kurs id:', 1);
$pdf->Cell(40, 10, $kursKode, 1);
$pdf->Ln();

$pdf->Output($filnavn, 'D');
exit;
}

add_action('wp_ajax_Last_Ned_pdf', 'Last_Ned_pdf');
add_action('wp_ajax_nopriv_Last_Ned_pdf', 'Last_Ned_pdf');

Here is the direct link to a page where you can try the functionality.
Scroll all the way down to the bottom of the page and click on the button in the bottom left corner.
hidden link

I would appreciate any advice or instructions you could provide on this matter.

Truls

#2672873

Hi Truls,

Thank you for contacting us and I'd be happy to assist.

The custom fields which are added through Toolset Types, append a prefix 'wpcf-' to the slug of the custom field keys in the database.
( ref: https://toolset.com/documentation/customizing-sites-using-php/functions/ )

This means that if you're using the 'get_post_meta' function, you'll append the prefix 'wpcf-' to the custom field key, which has the slug 'kursKode':


$kursKode = get_post_meta($post_id, 'wpcf-kursKode', true); 

And, if you're using Toolset's 'types_render_field' function, you don't have to include the prefix ''wpcf-''. But the attribute to specify the target post is 'item' and not 'post_id':


$kursKode = types_render_field('kursKode', array('item' => $post_id));

I hope this helps and please let me know if you need further assistance.

regards,
Waqar

#2673065

Hi Waquar,

Thank you for your prompt response.
I have located the error, which turned out to be a human one (my own). There was a typo in the post slug.

In your opinion, is my method of generating a PDF from the respective webpage the most effective approach?

Best regards,
Truls

#2673151

Thanks for the update and glad that this is sorted.

If your goal is to generate a PDF from the data coming from the current page/posts fields, then yes this is the way to go.