-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathText-Replacement.php
More file actions
63 lines (56 loc) · 2.23 KB
/
Text-Replacement.php
File metadata and controls
63 lines (56 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/*
Plugin Name: Text Replacement Plugin
Description: Replaces old strings with new strings throughout the website.
Version: 1.1
Author: Mayank Kumar
*/
// Create a menu item in the WordPress dashboard
function text_replacement_plugin_menu() {
add_options_page(
'Text Replacement Plugin',
'Text Replacement',
'manage_options',
'text-replacement-plugin',
'text_replacement_plugin_options_page'
);
}
add_action('admin_menu', 'text_replacement_plugin_menu');
// Display the options page in the WordPress dashboard
function text_replacement_plugin_options_page() {
if (!current_user_can('manage_options')) {
wp_die('You do not have sufficient permissions to access this page.');
}
// Save the new string when the form is submitted
if (isset($_POST['submit'])) {
$old_string = sanitize_text_field($_POST['old_string']);
$new_string = sanitize_text_field($_POST['new_string']);
update_option('text_replacement_old_string', $old_string);
update_option('text_replacement_new_string', $new_string);
echo '<div class="notice notice-success"><p>Strings saved successfully!</p></div>';
}
// Retrieve the saved strings
$old_string = get_option('text_replacement_old_string');
$new_string = get_option('text_replacement_new_string');
// Display the options form
?>
<div class="wrap">
<h1>Text Replacement Plugin</h1>
<form method="post" action="">
<label for="old_string">Old String:</label>
<input type="text" id="old_string" name="old_string" value="<?php echo esc_attr($old_string); ?>" required><br><br>
<label for="new_string">New String:</label>
<input type="text" id="new_string" name="new_string" value="<?php echo esc_attr($new_string); ?>" required><br><br>
<input type="submit" name="submit" value="Save">
</form>
</div>
<?php
}
// Perform text replacement
function replace_text($text) {
$old_string = get_option('text_replacement_old_string');
$new_string = get_option('text_replacement_new_string');
$text = str_replace($old_string, $new_string, $text);
return $text;
}
add_filter('the_content', 'replace_text');