Untitled
// Add the attachment button function b2bking_add_attachment_button() { ?> <button type="button" id="b2bking_attach_file" class="button" style="margin-right: 10px;"> <span class="dashicons dashicons-paperclip" style="vertical-align: middle;"></span> <?php esc_html_e('Attach File', 'b2bking'); ?> </button> <?php } add_action('b2bking_conversation_before_send_message_button', 'b2bking_add_attachment_button'); add_action('b2bking_new_conversation_my_account_after_message', 'b2bking_add_attachment_button'); // Enqueue necessary scripts function b2bking_enqueue_media_scripts() { if (is_admin()) return; // Only load on frontend wp_enqueue_media(); wp_add_inline_script('jquery', ' jQuery(document).ready(function($) { $("#b2bking_attach_file").click(function(e) { e.preventDefault(); var upload = wp.media({ title: "Select File", multiple: false }) .on("select", function() { var attachment = upload.state().get("selection").first().toJSON(); var currentText = $("#b2bking_conversation_user_new_message").val(); var newText = currentText + "\n" + attachment.url; $("#b2bking_conversation_user_new_message").val(newText); currentText = $("#b2bking_myaccount_textarea_conversation_start").val(); newText = currentText + "\n" + attachment.url; $("#b2bking_myaccount_textarea_conversation_start").val(newText); }) .open(); }); }); '); } add_action('wp_enqueue_scripts', 'b2bking_enqueue_media_scripts'); // Add upload capabilities to users function b2bking_add_upload_caps($allcaps, $caps, $args) { // Only proceed if user is logged in if (!is_user_logged_in()) { return $allcaps; } $allcaps['upload_files'] = true; $allcaps['edit_posts'] = true; return $allcaps; } add_filter('user_has_cap', 'b2bking_add_upload_caps', 10, 3); // Optional: Restrict file types function b2bking_restrict_file_types($mime_types) { // Add or remove file types as needed $allowed_types = array( 'jpg|jpeg|jpe' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png', 'pdf' => 'application/pdf', 'doc|docx' => 'application/msword', ); return $allowed_types; } add_filter('upload_mimes', 'b2bking_restrict_file_types', 10, 1); add_filter( 'ajax_query_attachments_args', 'library_own_images', 10, 1); function library_own_images( $query ) { $current_id = get_current_user_id(); $query['author'] = $current_id; return $query; } add_action('after_setup_theme', function() { if (!current_user_can('administrator')) { show_admin_bar(false); } }); add_action('init', function() { if (is_admin() && !current_user_can('administrator') && !wp_doing_ajax()) { wp_redirect(wc_get_page_permalink('myaccount')); exit; } });
Leave a Comment