File: /var/www/html/wpcurated/wp-content/themes/curatedbkk/css/functions.php
<?php
function create_blog_post_type() {
register_post_type('blog', array(
'labels' => array(
'name' => __('Blogs'),
'singular_name' => __('Blog'),
'add_new_item' => __('Add New Blog Post'),
'edit_item' => __('Edit Blog Post'),
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'blog'),
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'),
));
}
add_action('init', 'create_blog_post_type');
add_theme_support('post-thumbnails');
function create_blog_taxonomy() {
register_taxonomy(
'blog_category', // Taxonomy slug
'blog', // Post type slug
array(
'hierarchical' => true, // True for categories (hierarchical)
'labels' => array(
'name' => __('Blog Categories'),
'singular_name' => __('Blog Category'),
'search_items' => __('Search Blog Categories'),
'all_items' => __('All Blog Categories'),
'parent_item' => __('Parent Blog Category'),
'parent_item_colon' => __('Parent Blog Category:'),
'edit_item' => __('Edit Blog Category'),
'update_item' => __('Update Blog Category'),
'add_new_item' => __('Add New Blog Category'),
'new_item_name' => __('New Blog Category Name'),
'menu_name' => __('Blog Categories'),
),
'rewrite' => array(
'slug' => 'blog-category', // URL slug for categories
'with_front' => true,
'hierarchical' => true,
),
)
);
}
add_action('init', 'create_blog_taxonomy');
//---------------------------Crate a custome post career------------------------------------
function create_career_post_type() {
$labels = array(
'name' => _x('Careers', 'post type general name'),
'singular_name' => _x('Career', 'post type singular name'),
'menu_name' => _x('Careers', 'admin menu'),
'name_admin_bar' => _x('Career', 'add new on admin bar'),
'add_new' => _x('Add New', 'career'),
'add_new_item' => __('Add New Career'),
'new_item' => __('New Career'),
'edit_item' => __('Edit Career'),
'view_item' => __('View Career'),
'all_items' => __('All Careers'),
'search_items' => __('Search Careers'),
'parent_item_colon' => __('Parent Careers:'),
'not_found' => __('No careers found.'),
'not_found_in_trash' => __('No careers found in Trash.')
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'career'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'thumbnail', 'custom-fields')
);
register_post_type('career', $args);
}
add_action('init', 'create_career_post_type');
//--------------------------Add a URL field-----------------------
function add_blog_url_meta_box() {
add_meta_box(
'blog_url_meta_box', // Meta box ID
__('Media Blog URL'), // Title of the meta box
'blog_url_meta_box_callback', // Callback function to render the meta box
'blog', // Post type
'normal', // Context (normal, side, advanced)
'high' // Priority
);
}
add_action('add_meta_boxes', 'add_blog_url_meta_box');
function blog_url_meta_box_callback($post) {
// Retrieve the saved value, if available
$blog_url = get_post_meta($post->ID, '_blog_url', true);
// Add a nonce field for security
wp_nonce_field('save_blog_url_meta_box', 'blog_url_meta_box_nonce');
// Input field for the URL
?>
<label for="blog_url"><?php _e('Enter the Media Blog URL:', 'textdomain'); ?></label><br>
<input type="url" name="blog_url" id="blog_url" value="<?php echo esc_attr($blog_url); ?>" style="width:100%;" />
<?php
}
function save_blog_url_meta_box($post_id) {
// Verify the nonce for security
if (!isset($_POST['blog_url_meta_box_nonce']) || !wp_verify_nonce($_POST['blog_url_meta_box_nonce'], 'save_blog_url_meta_box')) {
return;
}
// Check user permissions
if (!current_user_can('edit_post', $post_id)) {
return;
}
// Check if the URL field is set and save the value
if (isset($_POST['blog_url'])) {
$blog_url = sanitize_text_field($_POST['blog_url']);
update_post_meta($post_id, '_blog_url', $blog_url);
} else {
delete_post_meta($post_id, '_blog_url'); // Remove the meta field if the URL is empty
}
}
add_action('save_post', 'save_blog_url_meta_box');
function enqueue_ajax_form_script() {
wp_enqueue_script('form-handler', get_template_directory_uri() . '/js/custom.js', array('jquery'), null, true);
wp_localize_script('form-handler', 'ajaxurl', admin_url('admin-ajax.php')); // Pass AJAX URL to the script
}
add_action('wp_enqueue_scripts', 'enqueue_ajax_form_script');
function handle_ajax_form_submission() {
// Sanitize input
$name = sanitize_text_field($_POST['name']);
$email = sanitize_email($_POST['email']);
$message = sanitize_textarea_field($_POST['message']);
// Validate input
if (empty($name) || empty($email) || empty($message)) {
wp_send_json_error(array('message' => 'All fields are required.'));
}
if (!is_email($email)) {
wp_send_json_error(array('message' => 'Invalid email address.'));
}
// Process the form data (e.g., send email or save to the database)
$admin_email = get_option('admin_email'); // Admin email to receive the message
$subject = 'New Contact Form Submission';
$body = "Name: $name\nEmail: $email\nMessage: $message";
$headers = array('Content-Type: text/plain; charset=UTF-8');
// Send email
if (wp_mail($admin_email, $subject, $body, $headers)) {
wp_send_json_success(array('message' => 'Your message has been sent successfully.'));
} else {
wp_send_json_error(array('message' => 'Failed to send your message. Please try again.'));
}
}
add_action('wp_ajax_submit_ajax_form', 'handle_ajax_form_submission');
add_action('wp_ajax_nopriv_submit_ajax_form', 'handle_ajax_form_submission');
//---------------------------------------------------------------------------------------------------
function handle_intake_form_submission() {
// check_ajax_referer('form_submission_nonce', 'security');
// Sanitize and validate form data
$full_name = sanitize_text_field($_POST['full_name']);
$contact_number = sanitize_text_field($_POST['contact_number']);
$mail_id = sanitize_email($_POST['mail_id']);
$reason = sanitize_text_field($_POST['reason']);
$description = sanitize_textarea_field($_POST['description']);
// Perform additional validations if needed
// if (empty($full_name) || empty($contact_number) || empty($mail_id)) {
// wp_send_json_error(array('message' => 'Required fields are missing.'));
// }
// Prepare the email
$to = 'admin@example.com'; // Replace with your recipient email address
$subject = 'New Intake Form Submission';
$headers = array('Content-Type: text/html; charset=UTF-8');
$message = "
<h2>New Intake Form Submission</h2>
<p><strong>Full Name:</strong> {$full_name}</p>
<p><strong>Contact Number:</strong> {$contact_number}</p>
<p><strong>Email:</strong> {$mail_id}</p>
<p><strong>Reason for Referral:</strong> {$reason}</p>
<p><strong>Description:</strong> {$description}</p>
";
// Send the email
$email_sent = wp_mail($to, $subject, $message, $headers);
if (!$email_sent) {
wp_send_json_error(array('message' => 'Failed to send email.'));
}
// Respond with success
wp_send_json_success(array('message' => 'Form submitted successfully! Email sent.'));
}
add_action('wp_ajax_submit_intake_form', 'handle_intake_form_submission');
add_action('wp_ajax_nopriv_submit_intake_form', 'handle_intake_form_submission');
//----------------------------------------------------------------------------------------------------
add_action('wp_ajax_send_form_email', 'send_form_email');
add_action('wp_ajax_nopriv_send_form_email', 'send_form_email');
function send_form_email() {
parse_str($_POST['form_data'], $form_data);
$first_name = sanitize_text_field($form_data['first_name']);
$last_name = sanitize_text_field($form_data['last_name']);
$email = sanitize_email($form_data['email']);
$phone_number = sanitize_text_field($form_data['phone_number']);
// Prepare email
$to = get_option('admin_email'); // Or a specific email
$subject = "New Form Submission";
$message = "You have received a new submission:\n\n";
$message .= "First Name: $first_name\n";
$message .= "Last Name: $last_name\n";
$message .= "Email: $email\n";
$message .= "Phone Number: $phone_number\n";
$headers = ['Content-Type: text/plain; charset=UTF-8'];
if (wp_mail($to, $subject, $message, $headers)) {
wp_send_json_success(['message' => 'Form submitted successfully.']);
} else {
wp_send_json_error(['message' => 'Failed to send email.']);
}
}