Last active
July 24, 2024 21:31
-
-
Save Qubadi/dd57db29f49b152a2a6101020ee92333 to your computer and use it in GitHub Desktop.
Crocoblock JetEngine Meta Field-Enhanced PDF Management in WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Youtube tutorials: | |
https://www.youtube.com/watch?v=W6j36I57xgo | |
__________________________________________________ | |
CSS Code For Download button: | |
/* Style for Download PDF link */ | |
.YOUR CLASSNAME { | |
color: #ffffff; | |
font-size: 12px; | |
font-weight: 600; | |
text-decoration: none; | |
background-color: #164AFE; | |
border-radius: 30px; | |
padding: 8px 10px; | |
display: inline-block; | |
transition: background-color 0.3s ease; | |
} | |
.YOUR CLASSNAME:hover { | |
background-color: #06090C; | |
color: #ffffff; | |
} | |
CSS Code For View button: | |
/* Style for View PDF link */ | |
.YOUR CLASSNAME { | |
color: #ffffff; | |
font-size: 12px; | |
font-weight: 600; | |
text-decoration: none; | |
background-color: #E04E4E; | |
border-radius: 30px; | |
padding: 8px 10px; | |
display: inline-block; | |
transition: background-color 0.3s ease; | |
} | |
.YOUR CLASSNAME:hover { | |
background-color: #06090C; | |
color: #ffffff; | |
} | |
_____________________________________________________________ | |
// Add Dashboard Widget | |
function custom_dashboard_widget() { | |
wp_add_dashboard_widget('custom_dashboard_widget', 'Manage PDF Shortcodes', 'custom_dashboard_widget_display'); | |
} | |
add_action('wp_dashboard_setup', 'custom_dashboard_widget'); | |
// Display Dashboard Widget Form | |
function custom_dashboard_widget_display() { | |
$pdf_shortcodes = get_option('custom_pdf_shortcodes', []); | |
// Handle form submission for adding/editing shortcodes | |
if (isset($_POST['submit_pdf_shortcode']) && check_admin_referer('pdf_shortcode_nonce')) { | |
$index = isset($_POST['index']) ? intval($_POST['index']) : -1; | |
$action = sanitize_text_field($_POST['pdf_action']); | |
$title = sanitize_text_field($_POST['pdf_title']); | |
$meta_field = sanitize_text_field($_POST['pdf_meta_field']); | |
$shortcode_name = $action . '_' . $meta_field; | |
$shortcode_data = ['action' => $action, 'title' => $title, 'meta_field' => $meta_field, 'shortcode_name' => $shortcode_name]; | |
if ($index >= 0 && isset($pdf_shortcodes[$index])) { | |
$pdf_shortcodes[$index] = $shortcode_data; | |
} else { | |
array_unshift($pdf_shortcodes, $shortcode_data); | |
} | |
update_option('custom_pdf_shortcodes', $pdf_shortcodes); | |
} | |
// Handle remove requests | |
if (isset($_POST['remove_pdf_shortcode']) && check_admin_referer('pdf_shortcode_nonce')) { | |
$index = isset($_POST['index']) ? intval($_POST['index']) : -1; | |
if ($index >= 0 && isset($pdf_shortcodes[$index])) { | |
array_splice($pdf_shortcodes, $index, 1); | |
update_option('custom_pdf_shortcodes', $pdf_shortcodes); | |
} | |
} | |
// Handle edit requests | |
$edit_index = -1; | |
if (isset($_POST['edit_pdf_shortcode'])) { | |
$edit_index = isset($_POST['index']) ? intval($_POST['index']) : -1; | |
} | |
// Display form for adding a new shortcode or editing an existing one | |
$current_action = $edit_index >= 0 ? $pdf_shortcodes[$edit_index]['action'] : ''; | |
$current_title = $edit_index >= 0 ? $pdf_shortcodes[$edit_index]['title'] : ''; | |
$current_meta_field = $edit_index >= 0 ? $pdf_shortcodes[$edit_index]['meta_field'] : ''; | |
custom_pdf_shortcode_form($current_action, $current_title, $current_meta_field, $edit_index); | |
// Display existing shortcodes | |
echo '<h4>Existing Shortcodes:</h4>'; | |
echo '<ul style="list-style: none; padding: 0;">'; | |
foreach ($pdf_shortcodes as $index => $shortcode) { | |
echo '<li style="margin-bottom: 10px; padding: 10px; border: 1px solid #ddd; border-radius: 6px;">'; | |
echo ($index + 1) . '. Title: ' . esc_html($shortcode['title']) . ', Meta Field: ' . esc_html($shortcode['meta_field']) . '<br>'; | |
echo '<span style="margin-top: 15px; display: block;"><strong>Shortcode: [' . esc_attr($shortcode['shortcode_name']) . ']</strong></span>'; | |
echo '<form method="post" style="display:inline; margin-left: 0px;"><input type="hidden" name="index" value="' . esc_attr($index) . '">'; | |
echo '<input type="submit" name="edit_pdf_shortcode" value="Edit" style="margin-right: 5px; background-color: #0073aa; color: white; border: none; border-radius: 6px; padding: 5px 10px; margin-top: 10px;">'; | |
echo '<input type="submit" name="remove_pdf_shortcode" value="Remove" style="background-color: #dc3232; color: white; border: none; border-radius: 6px; padding: 5px 10px; margin-top: 10px;">'; | |
wp_nonce_field('pdf_shortcode_nonce'); | |
echo '</form>'; | |
echo '</li>'; | |
} | |
echo '</ul>'; | |
// Display CSS class information | |
echo '<div style="border: 2px dotted red; font-size: 16px; font-weight: 400; padding: 10px; margin-top: 20px; border-radius: 6px;">'; | |
echo '<strong>For styling, use the shortcode name as the CSS class.</strong><br>'; | |
echo 'Example: If the shortcode is [view_download_pdf], use .view_download_pdf as the CSS class.'; | |
echo '</div>'; | |
} | |
// Form for adding/editing a shortcode | |
function custom_pdf_shortcode_form($action = '', $title = '', $meta_field = '', $index = -1) { | |
?> | |
<form action="" method="post" style="margin-bottom: 20px;"> | |
<input type="hidden" name="index" value="<?php echo esc_attr($index); ?>"> | |
<p> | |
<label for="pdf_action">Action (view/download):</label> | |
<select id="pdf_action" name="pdf_action" style="width: 100%; padding: 5px; border-radius: 6px; border: 1px solid #ddd;"> | |
<option value="view" <?php selected($action, 'view'); ?>>View</option> | |
<option value="download" <?php selected($action, 'download'); ?>>Download</option> | |
</select> | |
</p> | |
<p> | |
<label for="pdf_title">Title:</label> | |
<input type="text" id="pdf_title" name="pdf_title" value="<?php echo esc_attr($title); ?>" style="width: 100%; padding: 5px; border-radius: 6px; border: 1px solid #ddd;"> | |
</p> | |
<p> | |
<label for="pdf_meta_field">Meta Field Name:</label> | |
<input type="text" id="pdf_meta_field" name="pdf_meta_field" value="<?php echo esc_attr($meta_field); ?>" style="width: 100%; padding: 5px; border-radius: 6px; border: 1px solid #ddd;"> | |
</p> | |
<?php | |
wp_nonce_field('pdf_shortcode_nonce'); | |
?> | |
<input type="submit" name="submit_pdf_shortcode" value="<?php echo $index >= 0 ? 'Save Changes' : 'Add Shortcode'; ?>" style="background-color: #0073aa; color: white; border: none; border-radius: 6px; padding: 5px 10px;"> | |
</form> | |
<?php | |
} | |
// Register Shortcodes | |
foreach (get_option('custom_pdf_shortcodes', []) as $shortcode) { | |
add_shortcode($shortcode['shortcode_name'], function($atts) use ($shortcode) { | |
return custom_pdf_shortcode_handler($shortcode['meta_field'], $shortcode['action'], $shortcode['title'], $shortcode['shortcode_name']); | |
}); | |
} | |
// Shortcode Handler | |
function custom_pdf_shortcode_handler($meta_field_name, $action, $title, $shortcode_name) { | |
$post_id = get_the_ID(); | |
$file_id = get_post_meta($post_id, $meta_field_name, true); | |
if (empty($file_id)) { | |
return ''; // Return empty if no file ID | |
} | |
$file_url = wp_get_attachment_url($file_id); | |
if (empty($file_url)) { | |
return ''; // Return empty if no file URL | |
} | |
$link_text = !empty($title) ? $title : ucfirst($action) . ' PDF'; | |
$download_attr = $action === 'download' ? ' download' : ''; | |
$target_attr = $action === 'view' ? ' target="_blank"' : ''; | |
return "<a href='" . esc_url($file_url) . "' class='" . esc_attr($shortcode_name) . "'$download_attr$target_attr>$link_text</a>"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment