Post Title Not Rendering or Showing in PDF
-
Hi Jake Jackson @blue-liquid-designs
Issue with Rendering Post Title in Gravity PDF
I’m experiencing a challenge with rendering the Post Title in the PDF generated by Gravity PDF in my WordPress setup. I have implemented a custom filter to merge the tag for the Post Title, but it still does not appear in the final PDF output. The default {post_title} is not working.// Add custom filter for Post Title
add_filter('gform_custom_merge_tags', 'custom_post_title_merge_tag', 10, 4);
function custom_post_title_merge_tag($merge_tags, $form_id, $fields, $element_id) {
// Add custom merge tag for Post Title
$merge_tags[] = array(
'label' => 'Post Title',
'tag' => '{post_title}',
);
return $merge_tags;
}
// Add filter to modify Post Title value
add_filter('gform_replace_merge_tags', 'replace_post_title_merge_tag', 10, 7);
function replace_post_title_merge_tag($text, $form, $lead, $url_encode, $esc_html, $nl2br, $format) {
// Check if our custom merge tag exists in the text
if (strpos($text, '{post_title}') !== false) {
// Get the current post title
$post_title = get_the_title();
// Replace the merge tag with the post title
$text = str_replace('{post_title}', $post_title, $text);
}
return $text;
}Then, I used the following custom filter after above code failed to render
add_filter('gravitypdf/template/replace', function($template, $form, $entry) {
$template['post_title'] = get_the_title($entry['post_id']);
return $template;
}, 10, 3);I tried replacing
form_title
since it renders correctly, but it still isn’t working. I have attempted many custom filters, but none of them have helped render the post title..// Gravity Form custom merge tag for post title for Gravity PDF
add_filter('gform_pre_replace_merge_tags', function($text, $form, $entry, $url_encode, $esc_html, $nl2br, $format) {
$merge_tag = '{form_title}';
// Return original text if merge tag not found or if form is empty
if (strpos($text, $merge_tag) === false || empty($form)) {
return $text;
}
// Method 1: Get current post ID first
$current_post_id = get_queried_object_id();
$current_post_title = get_the_title($current_post_id);
// Alt Method 2: If first method returns empty, try getting from global post
if (empty($current_post_title)) {
global $post;
if ($post && $post->ID) {
$current_post_title = get_the_title($post->ID);
}
}
// Alt Method 3: Fallback - try to get from URL
if (empty($current_post_title)) {
$post_id = url_to_postid($_SERVER['REQUEST_URI']);
if ($post_id) {
$current_post_title = get_the_title($post_id);
}
}
// Debug: Log the title and post ID
error_log('Post ID: ' . $current_post_id);
error_log('Post Title: ' . $current_post_title);
// Only replace if we actually got a title
if (!empty($current_post_title)) {
return str_replace($merge_tag, $current_post_title, $text);
}
return $text;
}, 10, 7);
// Hook to change Form title to Post Tite
add_filter('gform_form_settings', function($form) {
// Store the current post title in the form settings
$form['title'] = get_the_title();
return $form;
}, 10, 1);
// Render Post Title
add_filter('gform_pre_render', function($form) {
// Get current post ID
$current_post_id = get_the_ID();
if ($current_post_id) {
// Get the post title
$post_title = get_the_title($current_post_id);
// Update form title
if ($post_title) {
$form['title'] = wp_kses_post($post_title);
}
}
return $form;
}, 10, 1);
add_filter('gform_pre_render', function($form) {
// Debug: Log current context
$debug = array(
'post_id' => get_the_ID(),
'is_single' => is_single(),
'current_post' => get_post(),
'in_the_loop' => in_the_loop()
);
error_log('Gravity Form Context: ' . print_r($debug, true));// Method 1: Using get_queried_object()
$queried_object = get_queried_object();
if ($queried_object instanceof WP_Post) {
$form['title'] = wp_kses_post($queried_object->post_title);
error_log('Using queried object title: ' . $queried_object->post_title);
}
// Alt Method 2: Using global post
global $post;
if ($post instanceof WP_Post) {
$form['title'] = wp_kses_post($post->post_title);
error_log('Using global post title: ' . $post->post_title);
}
return $form;
}, 10, 1);Please suggest how to make Post Title render in the PDF.
Thanks in Advance
- You must be logged in to reply to this topic.