Last active
September 24, 2018 22:35
-
-
Save jesselau76/95d59fcfcdc1165857cbdcc0e1210cbb to your computer and use it in GitHub Desktop.
Find And Replace Text Mode, a Wordpress Plugin. Add find and replace function in WordPress editor text mode.
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 | |
| /** | |
| * Plugin Name: Find and Replace Text Mode | |
| * Plugin URI: http://jesselau.com | |
| * Version: 1.0 | |
| * Author: jesse Lau | |
| * Author URI: https://jesselau.com | |
| * Description: Add find and replace function in WordPress editor text mode. | |
| * License: GPL2 | |
| */ | |
| class jesse_find_replace_button { | |
| /** | |
| * Constructor. Called when the plugin is initialised. | |
| */ | |
| function __construct() { | |
| if ( is_admin() ) { | |
| add_action( 'admin_print_footer_scripts', array( &$this, 'admin_footer_scripts' ) ); | |
| } | |
| } | |
| /** | |
| * Adds the Find and Replace button to the Quicktags (Text) toolbar of the content editor | |
| */ | |
| function admin_footer_scripts() { | |
| // Check the Quicktags script is in use | |
| if ( ! wp_script_is( 'quicktags' ) ) { | |
| return; | |
| } | |
| ?> | |
| <script type="text/javascript"> | |
| QTags.addButton( 'jesse_find_replace_button', 'Find & Replace', insert_button ); | |
| function insert_button() { | |
| //WP Editor always use "content" as ID | |
| // | |
| var defaultid = "content"; | |
| var x = document.getElementById(defaultid); | |
| if ((x==null)||(x.type!=="textarea")){ | |
| alert("You TEXT AREA ID is not default value. Please read help file."); | |
| return; | |
| } | |
| var text = document.getElementById(defaultid).value; | |
| var search = prompt('Find What:'); | |
| if( !search) | |
| return; | |
| var pattern = new RegExp(search,"gi"); | |
| var findCount = (text.match(pattern) || []).length; | |
| if(findCount == 0){ | |
| alert("Nothing found!"); | |
| return; | |
| } | |
| var replace = prompt('Replace With:'); | |
| if( !replace) | |
| replace=""; | |
| if(confirm("find '"+search+"', "+findCount + " matches found, replace with '"+replace+"' now?")){ | |
| document.getElementById(defaultid).value= text.replace(pattern, replace); | |
| } | |
| } | |
| </script> | |
| <?php | |
| } | |
| } | |
| $jesse_find_replace_button = new jesse_find_replace_button; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment