Last active
August 11, 2024 14:32
-
-
Save Qubadi/5fdb2ea77e400110c2b33a5695c3e9f9 to your computer and use it in GitHub Desktop.
Jetformbuilder and Jetengine form custom configuration for WYSIWYG editor
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
UPDATED: 11-08-2024 | |
Description: | |
1. Copy the following PHP code and create a PHP snippet using your snippet plugins. Paste the code into the plugin and save it. | |
Jetformbuilder and Jetengine form custom configuration for WYSIWYG editor | |
It works for both forms, JetFormBuilder and JetEngine Form. | |
This code customizes the JetFormBuilder WYSIWYG editor by adding essential toolbar buttons and enabling media buttons | |
for enhanced functionality. It ensures that users have access to a wide range of formatting options, making content | |
creation more efficient. This setup enhances the overall user experience with improved editing capabilities. | |
_____________________________________________________ | |
// Function to configure WYSIWYG editor | |
$wysiwygConfig = function ( $wysiwyg_config ) { | |
/** | |
* List of all available plugins can be found in wp-includes/js/tinymce/plugins | |
*/ | |
$plugins = array( | |
'colorpicker', | |
'textcolor', | |
'image', // Add image plugin | |
'media', // Add media plugin for embedding videos | |
'link', // Add link plugin for hyperlinking | |
'lists' // Add lists plugin for bullet points and numbered lists | |
// Removed 'code' plugin due to loading issues | |
); | |
$toolbar_buttons = array( | |
'formatselect', // Add format select dropdown | |
'fontselect', // Add font select dropdown | |
'fontsizeselect',// Add font size select dropdown | |
'|', | |
'bold', // Add bold button | |
'italic', // Add italic button | |
'underline', // Add underline button | |
'|', | |
'forecolor', // Add text color button | |
'backcolor', // Add background color button | |
'|', | |
'alignleft', // Add align left button | |
'aligncenter', // Add align center button | |
'alignright', // Add align right button | |
'alignjustify', // Add align justify button | |
'|', | |
'bullist', // Add unordered list button | |
'numlist', // Add ordered list button | |
'|', | |
'outdent', // Add outdent button | |
'indent', // Add indent button | |
'|', | |
'link', // Add link button for hyperlinks | |
'unlink', // Add unlink button | |
'|', | |
'removeformat' // Add remove format button | |
); | |
// Ensure image and media buttons are included only for logged-in users | |
if ( is_user_logged_in() ) { | |
$toolbar_buttons = array_merge($toolbar_buttons, array( | |
'image', // Add image button for image upload | |
'media' // Add media button for embedding videos | |
)); | |
$wysiwyg_config['media_buttons'] = true; // Enable media buttons for logged-in users | |
} else { | |
$wysiwyg_config['media_buttons'] = false; // Disable media buttons for non-logged-in users | |
} | |
// Ensure no duplicate plugins | |
$existing_plugins = explode(',', $wysiwyg_config['tinymce']['plugins']); | |
$new_plugins = array_unique(array_merge($existing_plugins, $plugins)); | |
$wysiwyg_config['tinymce']['plugins'] = implode(',', $new_plugins); | |
// Ensure no duplicate toolbar buttons | |
$existing_toolbar_buttons = explode(',', $wysiwyg_config['tinymce']['toolbar1']); | |
$new_toolbar_buttons = array_unique(array_merge($existing_toolbar_buttons, $toolbar_buttons)); | |
$wysiwyg_config['tinymce']['toolbar1'] = implode(',', $new_toolbar_buttons); | |
// Remove the failed plugins 'insertdatetime', 'advlist', 'autolink', and 'code' if they exist | |
$wysiwyg_config['tinymce']['plugins'] = preg_replace('/\b(insertdatetime|advlist|autolink|code)\b/', '', $wysiwyg_config['tinymce']['plugins']); | |
$wysiwyg_config['tinymce']['plugins'] = trim($wysiwyg_config['tinymce']['plugins'], ','); | |
// Clean up duplicate commas and spaces in plugins and toolbar | |
$wysiwyg_config['tinymce']['plugins'] = preg_replace('/,{2,}/', ',', $wysiwyg_config['tinymce']['plugins']); | |
$wysiwyg_config['tinymce']['toolbar1'] = preg_replace('/,{2,}/', ',', $wysiwyg_config['tinymce']['toolbar1']); | |
return $wysiwyg_config; | |
}; | |
// Apply the configuration to JetFormBuilder | |
add_filter( 'jet-form-builder/fields/wysiwyg-field/config', $wysiwygConfig ); | |
// Apply the configuration to JetEngine Forms | |
add_filter( 'jet-engine/forms/fields/wysiwyg-field/config', $wysiwygConfig ); | |
// Function to add media upload capability to all roles | |
function add_media_upload_capability() { | |
$roles = array('contributor', 'author', 'editor', 'administrator'); | |
foreach ($roles as $role_name) { | |
$role = get_role($role_name); | |
if ($role && !$role->has_cap('upload_files')) { | |
$role->add_cap('upload_files'); | |
} | |
if ($role && !$role->has_cap('edit_posts')) { | |
$role->add_cap('edit_posts'); | |
} | |
if ($role && !$role->has_cap('read')) { | |
$role->add_cap('read'); | |
} | |
} | |
} | |
add_action('init', 'add_media_upload_capability'); | |
// Restrict media library access to only user-uploaded files | |
function restrict_media_library_to_user_uploads( $wp_query ) { | |
if ( strpos( $_SERVER['REQUEST_URI'], '/wp-admin/upload.php' ) !== false || ( isset( $_POST['action'] ) && $_POST['action'] === 'query-attachments' ) ) { | |
if ( ! current_user_can( 'manage_options' ) ) { // Allow admins to see all media | |
$user_id = get_current_user_id(); | |
$wp_query->set( 'author', $user_id ); | |
} | |
} | |
} | |
add_action( 'pre_get_posts', 'restrict_media_library_to_user_uploads' ); | |
// Allow file upload by any user for any post | |
function allow_contributor_uploads( $caps, $cap, $user_id, $args ) { | |
if ( 'edit_post' == $cap || 'edit_jet_form' == $cap || 'upload_files' == $cap ) { | |
$caps = array(); | |
} | |
return $caps; | |
} | |
add_filter( 'map_meta_cap', 'allow_contributor_uploads', 10, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment