I have done a lot of Googling , but have not been able to figure this out yet. I have used a function in a plugin I am creating that swaps out the file url of any image inserted and replaces it with a placeholder “%HOME%” or “%SITE”
Here is the code for that function below:
function awel4wd_editor_links($html) {
$homeURL = get_home_url();
$siteURL = get_site_URL();
$newContent = str_replace($siteURL, "%SITE%", $html);
$newContent = str_replace($siteURL, "%HOME%", $newContent);
$html = $newContent;
return $html;
}
This does what I want except as you can imagine, images in visual mode, no longer work and display as a broken image link.
So my goal is this: I would like to have some sort of filter that detects when visual mode is selected, grabs the content in the editor that is being used to display in visual mode and will run it through a function that will replace %SITE% and %HOME% with the full URLs so images will show up in the visual editor etc.
Then, when it switches back to text mode, it needs to do the reverse and put the %HOME% and %SITE% placeholders back in. I want the placeholdrs to be what is given to the live site though.
I have tried creating functions to do this to “the_content_editor” and “richedit_pre” but these hooks output before the content loads in general so they dont update for the visual editor and change the links completely for html and visual. I would be open to suggestions on how to do this with shortcodes (essentially by running the shortcode in the visual editor ) but so far I haven’t been able to find the right hooks or actions to accomplish this.
I have also looked into creating a tiny MCE plugin which makes sense, but Im not super strong with js, and most of the examples I have seen are these monsters that do more than I need and it is difficult for me to sort through. I JUST need to determin if in visual mode, find and replace and then do the reverse. At least thats what I think.
Help is appreciated, specific code that is simple is double appreciated. Thanks!