/**
* Auto-add https:// to links in the Classic Editor via JavaScript
* Triggers when the user clicks 'Apply', hits 'Enter', or leaves the input field.
*/
function auto_add_https_classic_editor_js() {
// Only run this script on admin pages where the editor might be present
$screen = get_current_screen();
if ( ! $screen || ! in_array( $screen->base, array( 'post' ) ) ) {
return;
}
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
// Function to check and prepend https://
function fixLinkFormat(inputElement) {
var input = $(inputElement);
if (input.length === 0) return;
var val = input.val().trim();
// If not empty, doesn't start with standard prefixes, and contains a dot (looks like a domain)
if (val && !/^(https?:\/\/|mailto:|tel:|#|\/)/i.test(val) && val.indexOf('.') !== -1) {
input.val('https://' + val);
}
}
// 1. Intercept clicks on the "Apply" / "Add Link" buttons
// Targets the Advanced Dialog (#wp-link-submit) and Inline Toolbar (.wp-link-apply)
$(document).on('mousedown click', '#wp-link-submit, .wp-link-apply', function() {
fixLinkFormat('#wp-link-url'); // Advanced Dialog input
fixLinkFormat('.wp-link-input input'); // Inline Toolbar input
});
// 2. Intercept the 'Enter' key inside the link input fields
$(document).on('keydown', '#wp-link-url, .wp-link-input input', function(e) {
if (e.which === 13) { // 13 is the Enter key
fixLinkFormat(this);
}
});
// 3. Fallback: Also format when the user clicks out of the input box (blur)
$(document).on('blur', '#wp-link-url, .wp-link-input input', function() {
fixLinkFormat(this);
});
});
</script>
<?php
}
// Hook into the admin footer to output the script
add_action( 'admin_footer', 'auto_add_https_classic_editor_js' );
function custom_classic_editor_styles( $init_array ) {
// Define your custom CSS here.
// In this example, we are changing all <h2> tags to red.
$custom_css = '#tinymce a { color: #3858E9 !important; } #tinymce a:hover { text-decoration: underline !important; }';
// Inject the CSS into TinyMCE's content_style array
if ( isset( $init_array['content_style'] ) ) {
$init_array['content_style'] .= ' ' . $custom_css;
} else {
$init_array['content_style'] = $custom_css;
}
return $init_array;
}
// Hook into TinyMCE initialization
add_filter( 'tiny_mce_before_init', 'custom_classic_editor_styles' );