Last active
March 31, 2026 02:55
-
-
Save khoipro/cab5b356344ffe69dce69320a03ccc6c to your computer and use it in GitHub Desktop.
WOW - The Maintenance contains a few snippets to help you easy to stay with a website
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
| <?php | |
| /** | |
| * Maintenance Hooks for WordPress | |
| * @author CODE TOT <khoi@codetot.com> | |
| * @link https://baotriweb.codetot.vn | |
| **/ | |
| // Reject comments containing URLs in the comment text or website field | |
| function codetot_maintenance_reject_comments_with_urls($commentdata) { | |
| // Check the comment content for URLs | |
| if (preg_match('/https?:\/\/|www\./i', $commentdata['comment_content'])) { | |
| wp_die('Comments containing URLs are not allowed.'); | |
| } | |
| // Check the website field for URLs | |
| if (!empty($commentdata['comment_author_url']) && preg_match('/https?:\/\/|www\./i', $commentdata['comment_author_url'])) { | |
| wp_die('Website URLs are not allowed in comments.'); | |
| } | |
| return $commentdata; | |
| } | |
| add_filter('preprocess_comment', 'codetot_maintenance_reject_comments_with_urls'); | |
| // Prevent empty Gravity Form submissions | |
| function codetot_maintenance_prevent_gravity_form_blank_entry( $validation_result ) { | |
| $form = $validation_result['form']; | |
| $is_empty = true; | |
| // Loop through submitted fields | |
| foreach ( $form['fields'] as $field ) { | |
| $field_id = $field->id; | |
| $value = rgpost( "input_{$field_id}" ); | |
| // If any field has a non-empty value, mark as not empty | |
| if ( !empty( $value ) ) { | |
| $is_empty = false; | |
| break; | |
| } | |
| } | |
| // If all fields are empty, fail validation | |
| if ( $is_empty ) { | |
| $validation_result['is_valid'] = false; | |
| // Add a general form error message | |
| $form['validation_message'] = 'You must fill out at least one field before submitting.'; | |
| $validation_result['form'] = $form; | |
| } | |
| return $validation_result; | |
| } | |
| add_filter( 'gform_validation', 'codetot_maintenance_prevent_gravity_form_blank_entry'); | |
| function codetot_maintenance_remove_tracking_parameters_globally() | |
| { | |
| $current_url = wp_parse_url(home_url($_SERVER['REQUEST_URI'])); | |
| $exclude_parameters = [ | |
| 'fbclid', | |
| 'gclid', | |
| 'srsltid', | |
| ]; | |
| if (isset($current_url['query'])) { | |
| $query_params = wp_parse_args($current_url['query']); | |
| $modified = false; | |
| $new_query_params = array(); | |
| foreach ($query_params as $key => $value) { | |
| if (! in_array($key, $exclude_parameters)) { | |
| $new_query_params[$key] = $value; | |
| } else { | |
| $modified = true; | |
| } | |
| } | |
| if ($modified) { | |
| $new_url = $current_url['scheme'] . '://' . $current_url['host'] . $current_url['path']; | |
| if (! empty($new_query_params)) { | |
| $new_url = add_query_arg($new_query_params, $new_url); | |
| } | |
| wp_safe_redirect($new_url, 301); | |
| exit; | |
| } | |
| } | |
| } | |
| add_action('init', 'codetot_maintenance_remove_tracking_parameters_globally'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment