I have the below code in my plugin. Admin is meant to be able to insert funding\_amount, funding\_deadline, and funding\_multimedia\_field from the dashboard if the resource category chosen is funding. funding\_amount, and funding\_deadline work as expected but the file upload is not working as no file is saved in my WordPress media gallery. custom\_resources\_handle\_file\_upload is the function to handle the upload. Can someone please, help me check this code and proffer a solution? Thank you.
<?php
/*
Plugin Name: Custom Resources Plugin
Description: A custom WordPress plugin for managing and displaying resources.
Version: 1.0
*/
// Define categories and their associated fields with field types
$category_fields = array(
‘job’ => array(
‘job_location’ => array(
‘label’ => ‘Job Location’,
‘type’ => ‘text’,
// Use ‘text’ field type for text input
),
‘job_type’ => array(
‘label’ => ‘Job Type’,
‘type’ => ‘select’,
// Use ‘select’ field type for dropdown select
‘options’ => array(
‘full_time’ => ‘Full Time’,
‘part_time’ => ‘Part Time’,
),
),
),
‘internship’ => array(
‘internship_location’ => array(
‘label’ => ‘Internship Location’,
‘type’ => ‘text’,
),
‘internship_duration’ => array(
‘label’ => ‘Internship Duration (in weeks)’,
‘type’ => ‘number’,
// Use ‘number’ field type for numeric input
),
),
‘funding’ => array(
‘funding_amount’ => array(
‘label’ => ‘Funding Amount’,
‘type’ => ‘text’,
),
‘funding_deadline’ => array(
‘label’ => ‘Funding Deadline’,
‘type’ => ‘date’,
// Use ‘date’ field type for date input
),
‘funding_multimedia_field’ => array(
‘label’ => ‘Funding Multimedia Field’,
‘type’ => ‘file’,
// Use ‘file’ field type for multimedia input
),
),
);
// Register custom post type
function custom_resources_register_post_type()
{
register_post_type(
‘resources’,
array(
‘labels’ => array(
‘name’ => ‘Resources’,
‘singular_name’ => ‘Resource’,
),
‘public’ => true,
‘supports’ => array(‘title’, ‘thumbnail’),
// ‘supports’ => array(‘title’, ‘editor’, ‘thumbnail’),
)
);
wp_enqueue_script(‘custom-resources’, plugin_dir_url(__FILE__) . ‘js/custom-resources.js’, array(‘jquery’), null, true);
}
add_action(‘init’, ‘custom_resources_register_post_type’);
// Register custom taxonomies based on the structure of $category_fields
// Register custom taxonomy
function custom_resources_register_taxonomy()
{
$category_labels = array(
‘name’ => ‘Resource Categories’,
‘singular_name’ => ‘Resource Category’,
‘menu_name’ => ‘Resource Categories’,
‘all_items’ => ‘All Categories’,
‘edit_item’ => ‘Edit Category’,
‘view_item’ => ‘View Category’,
‘update_item’ => ‘Update Category’,
// ‘add_new_item’ => ‘Add New Category’,
‘new_item_name’ => ‘New Category Name’,
‘search_items’ => ‘Search Categories’,
‘popular_items’ => ‘Popular Categories’,
‘not_found’ => ‘No Categories found’,
‘no_terms’ => ‘No Categories’,
‘items_list’ => ‘Categories list’,
‘items_list_navigation’ => ‘Categories list navigation’,
);
$category_args = array(
‘labels’ => $category_labels,
‘hierarchical’ => true,
‘show_ui’ => true,
‘show_admin_column’ => true,
‘query_var’ => true,
‘rewrite’ => array(‘slug’ => ‘resource-category’),
);
register_taxonomy(‘resource_category’, ‘resources’, $category_args);
// Assign terms to the custom taxonomy
global $category_fields;
foreach ($category_fields as $category => $fields) {
// Check if the term already exists
$term_exists = term_exists($category, ‘resource_category’);
if (!$term_exists) {
// If the term doesn’t exist, add it
wp_insert_term($category, ‘resource_category’);
}
}
}
add_action(‘init’, ‘custom_resources_register_taxonomy’);
// Add custom fields based on category
function custom_resources_add_custom_fields()
{
add_meta_box(‘resource_category’, ‘Resource Category’, ‘custom_resources_render_category’, ‘resources’, ‘normal’, ‘high’);
}
add_action(‘add_meta_boxes’, ‘custom_resources_add_custom_fields’);
// Render category dropdown and dynamic fields
function custom_resources_render_category($post)
{
// Retrieve the selected category
$selected_category = get_post_meta($post->ID, ‘resource_category’, true);
// Get the list of categories and fields
global $category_fields;
echo ‘<label for=”resource_category”>Category:</label>’;
echo ‘<select name=”resource_category” id=”resource_category-select”>’;
echo ‘<option value=””>Select Category</option>’;
foreach ($category_fields as $category => $fields) {
$selected = ($selected_category === $category) ? ‘selected’ : ”;
echo ‘<option value=”‘ . esc_attr($category) . ‘” ‘ . $selected . ‘>’ . esc_html(ucfirst($category)) . ‘</option>’;
}
echo ‘</select>’;
// Display the fields based on the selected category
echo ‘<div id=”custom-fields-container”>’;
foreach ($category_fields as $category => $fields) {
$style = ($selected_category === $category) ? ‘style=”display: block;”‘ : ‘style=”display: none;”‘;
echo ‘<div class=”custom-fields” data-category=”‘ . esc_attr($category) . ‘” ‘ . $style . ‘>’;
foreach ($fields as $field_key => $field_settings) {
$field_value = get_post_meta($post->ID, $field_key, true);
$field_type = $field_settings[‘type’];
$field_label = $field_settings[‘label’];
echo ‘<label for=”‘ . esc_attr($field_key) . ‘”>’ . esc_html($field_label) . ‘:</label>’;
if ($field_type === ‘select’) {
echo ‘<select name=”‘ . esc_attr($field_key) . ‘” id=”‘ . esc_attr($field_key) . ‘”>’;
foreach ($field_settings[‘options’] as $option_value => $option_label) {
$selected = ($field_value === $option_value) ? ‘selected’ : ”;
echo ‘<option value=”‘ . esc_attr($option_value) . ‘” ‘ . $selected . ‘>’ . esc_html($option_label) . ‘</option>’;
}
echo ‘</select>’;
} elseif ($field_type === ‘number’) {
echo ‘<input type=”number” name=”‘ . esc_attr($field_key) . ‘” id=”‘ . esc_attr($field_key) . ‘” value=”‘ . esc_attr($field_value) . ‘”>’;
} elseif ($field_type === ‘date’) {
echo ‘<input type=”date” name=”‘ . esc_attr($field_key) . ‘” id=”‘ . esc_attr($field_key) . ‘” value=”‘ . esc_attr($field_value) . ‘”>’;
} elseif ($field_type === ‘file’) {
echo ‘<input type=”file” name=”‘ . esc_attr($field_key) . ‘” id=”‘ . esc_attr($field_key) . ‘”>’;
if ($field_value) {
echo ‘<p><a href=”‘ . esc_url(wp_get_attachment_url($field_value)) . ‘”>View File</a></p>’;
}
} else {
echo ‘<input type=”text” name=”‘ . esc_attr($field_key) . ‘” id=”‘ . esc_attr($field_key) . ‘” value=”‘ . esc_attr($field_value) . ‘”>’;
}
}
echo ‘</div>’;
}
echo ‘</div>’;
}
// Save custom fields
function custom_resources_save_custom_fields($post_id)
{
// Save the selected category
if (isset($_POST[‘resource_category’])) {
update_post_meta($post_id, ‘resource_category’, sanitize_text_field($_POST[‘resource_category’]));
}
// Save the fields based on the selected category
$selected_category = get_post_meta($post_id, ‘resource_category’, true);
global $category_fields;
if (array_key_exists($selected_category, $category_fields)) {
$fields = $category_fields[$selected_category];
foreach ($fields as $field_key => $field_settings) {
if (isset($_POST[$field_key])) {
$field_type = $field_settings[‘type’];
$field_value = $_POST[$field_key];
// Sanitize and handle specific field types
if ($field_type === ‘select’) {
// Ensure the selected option is one of the predefined options
if (isset($field_settings[‘options’][$field_value])) {
update_post_meta($post_id, $field_key, sanitize_text_field($field_value));
}
} elseif ($field_type === ‘number’) {
// Ensure the input is a numeric value
if (is_numeric($field_value)) {
update_post_meta($post_id, $field_key, sanitize_text_field($field_value));
}
} elseif ($field_type === ‘date’) {
// Ensure the input is a valid date
if (strtotime($field_value)) {
update_post_meta($post_id, $field_key, sanitize_text_field($field_value));
}
} elseif ($field_type === ‘file’) {
// Handle file uploads
$attachment_id = custom_resources_handle_file_upload($field_key);
if ($attachment_id) {
update_post_meta($post_id, $field_key, $attachment_id);
}
} else {
// For text input, sanitize as text
update_post_meta($post_id, $field_key, sanitize_text_field($field_value));
}
}
}
}
}
add_action(‘save_post’, ‘custom_resources_save_custom_fields’);
// Helper function to handle file uploads
// Helper function to handle file uploads
function custom_resources_handle_file_upload($field_key)
{
error_log(‘$_FILES contents: ‘ . print_r($_FILES, true));
if (isset($_FILES[$field_key])) {
$file = $_FILES[$field_key];
$upload_overrides = array(‘test_form’ => false);
// Upload the file
$file_info = wp_handle_upload($file, $upload_overrides);
if ($file_info && !isset($file_info[‘error’])) {
// Create an attachment post
$attachment = array(
‘post_mime_type’ => $file_info[‘type’],
‘post_title’ => sanitize_file_name($file_info[‘file’]),
‘post_content’ => ”,
‘post_status’ => ‘inherit’,
);
$attachment_id = wp_insert_attachment($attachment, $file_info[‘file’]);
if (!is_wp_error($attachment_id)) {
require_once(ABSPATH . ‘wp-admin/includes/image.php’);
$attachment_data = wp_generate_attachment_metadata($attachment_id, $file_info[‘file’]);
wp_update_attachment_metadata($attachment_id, $attachment_data);
return $attachment_id;
}
}
}
return false;
}
​
What I observed is that the “save_post” action is not being triggered when the publish or update post button is clicked