The whole plugin that saves some text to the database and replace provided values in the content
<?php
/*
Plugin Name: Change content
Description: Plugin that changes provided words for something different
Version 1.0
Author: Matineo
Author URI: https://www.pikademia.pl/how-to-add-custom-add_action-hook-to-the-custom-admin-settings-page/
*/
if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
add_action('admin_menu', 'ourMenu');
add_filter('the_content', 'filterLogic');
function filterLogic($content) {
$badWords = explode(',', get_option('myPluginDBopt'));
$badWordsTrimmed = array_map('trim', $badWords);
return str_ireplace($badWordsTrimmed, esc_html(get_option('myPluginDBoptReplace', '****')), $content);
}
function ourMenu() {
$mainPageHook = add_menu_page(
'Change text plugin',
'Change text',
'manage_options',
'myPluginSlug',
'myPluginCallBackFunc',
'dashicons-format-quote',
100);
add_action("load-{$mainPageHook}", 'myPluginAssets');
}
function myPluginAssets() {
wp_enqueue_style('myPluginCss', plugin_dir_url(__FILE__) . 'mypluginstyle.css');
}
function handleForm() {
if (wp_verify_nonce($_POST['ourNonce'], 'saveMyText') AND current_user_can('manage_options')) {
update_option('myPluginDBopt', sanitize_text_field($_POST['myplugin1']));
update_option('myPluginDBoptReplace', sanitize_text_field($_POST['replaceText']));?>
<div class="updated">
<p>Your filtered words were saved.</p>
</div>
<?php } else { ?>
<div class="error">
<p>Sorry, you do not have permission to perform that action.</p>
</div>
<?php }
}
function myPluginCallBackFunc() { ?>
<div class="wrap">
<div class="mainDiv">
<h1>Change text plugin</h1>
<?php if ($_POST['justsubmitted'] == "true") {handleForm();} ?>
<form method="POST">
<input type="hidden" name="justsubmitted" value="true">
<?php wp_nonce_field('saveMyText', 'ourNonce') ?>
<label for="myplugin1"><p>Enter words, separated by coma</p></label>
<div class="textArea">
<textarea name="myplugin1" id="myplugin1" placeholder="word1,word2,word3,word4,..."><?php echo esc_textarea(get_option('myPluginDBopt')) ?></textarea>
</div>
<p>Change for this value</p>
<input type="text" name="replaceText">
<input type="submit" name="submit" id="submit" class="button button-primary" value="Save Changes">
</form>
</div>
</div>
<?php }