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