Skip to content

Instantly share code, notes, and snippets.

@ohid
Last active September 15, 2020 17:09
Show Gist options
  • Save ohid/67c7fb452102db79077dd41eeaf0d283 to your computer and use it in GitHub Desktop.
Save ohid/67c7fb452102db79077dd41eeaf0d283 to your computer and use it in GitHub Desktop.
<?php
/**
* Script Name: [Forminator Pro] Make fields read only
* Script URI: https://premium.wpmudev.org/
* Description: This is a mu-plugin that makes the selected fields not editable/read only.
* Author: Ohidul Islam @ WPMU DEV
* Version: 0.1
* Author URI: https://premium.wpmudev.org/
*
*/
if( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
return;
}
if( ! class_exists( 'MAKE_FIELDS_READ_ONLY' ) ) {
class MAKE_FIELDS_READ_ONLY {
public static $_instance = null;
public static function instance() {
if( is_null ( self::$_instance ) ) {
self::$_instance = new MAKE_FIELDS_READ_ONLY;
}
return self::$_instance;
}
public function __construct() {
add_action( 'wp_footer', array( $this, 'footer_scripts' ) );
}
public function footer_scripts() {
?>
<script>
(function($) {
//
// Make fields read only
//
const readOnlyField = $(".read-only-field");
if( readOnlyField.length > 0 ) {
$.each( readOnlyField, function(i, dom) {
$(dom).find('input').attr('disabled', true);
$(dom).find('select').attr('disabled', true);
});
}
// Remove the disabled attributes when submitting the form
$('form').bind('submit', function () {
const formSelector = $('.forminator-custom-form');
$(formSelector).find('input').prop('disabled', false);
$(formSelector).find('select').prop('disabled', false);
});
})(jQuery)
</script>
<?php
}
}
if( ! function_exists( 'MAKE_FIELDS_READ_ONLY' ) ) {
function make_fields_read_only() {
return MAKE_FIELDS_READ_ONLY::instance();
}
$make_fields_read_only = make_fields_read_only();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment