How to Insert Page with embedded text replace feature
-
Hello,
I am using intensively “Insert Pages” plugin.
I wonder how to replace a specific text within the inserted page in order to change the displayed page content.
A kind of embedded search&replace text feature.
Would you please be kind to suggest a possible solution ?
Regards.
JP
-
Aloha, you can hook into the
insert_pages_wrap_contentfilter and do your string replace there. For example:add_filter( 'insert_pages_wrap_content', function ( $content ) { return str_replace( 'old text', 'new text', $content ); } );The filter is documented here if you need to use any of the other params:
https://github.com/uhm-coe/insert-pages/blob/master/insert-pages.php#L977-L993Thanks Pail,
for your suggested solution.
Any trick how to pass ‘old text’ and ‘new text’ within the [insert] short code ?
Regards.
JPYou could add the search/replace text to the querystring param of the shortcode, and then look for that in your filter hook. For example:
[insert page='your-page' display='content' querystring='search=old&replace=new']Then your hook would look like:
add_filter( 'insert_pages_wrap_content', function ( $content, $inserted_page, $attributes ) { if ( ! empty( $attributes['querystring'] ) ) { parse_str( $attributes['querystring'], $querystring ); if ( isset( $querystring['search'], $querystring['replace'] ) ) { $content = str_replace( $querystring['search'], $querystring['replace'], $content ); } } return $content; }, 10, 3 );Just be aware that you should probably sanitize at least the
replacequerystring value if you don’t trust your Editors, since it could be vulnerable to javascript injection or other nefarious purposes.Excellent Paul,
I will try and let you know.
JP
Hello Paul,
Just to confirm you I did implement your solution and it worked perfectly.
Thanks again for your excellent job and this perfect solution.
May be could you explain the “10, 3” syntax at the end, I could not figue out ?
Regards.
JP
Great!
Regarding the
10, 3at the end: the WordPress functionadd_filter()lets you hook your custom code into specific spots throughout the codebase. There are 4 parameters to that function:
1. the name of the filter hook
2. your function to execute (callback)
3. the priority of your function (in case there are other functions also hooked into the same hook)
4. how many parameters your function in #2 accepts
https://developer.ww.wp.xz.cn/reference/functions/add_filter/#parametersSo the
10is the priority (10 is the default; in our case here it doesn’t really matter whether our functions runs before or after other ones, since we’re just replacing some text. In the past I’ve used the predefined constantPHP_INT_MINto ensure that my code runs before anything else that might be hooked into the same hook, andPHP_INT_MAXto ensure that my code runs after anything else).And finally the
3is number of parameters to our custom function ($content, $inserted_page, $attributes).In the documentation above you may have noticed that those last 2 params default to
10and1respectively, so if your hook only has 1 parameter (or you only need to access the first parameter of the hook), and you don’t care about which order it runs, then you can leave them off and just use those defaults! (That’s what I did in my first code sample above.)Note: newer versions of PHP allow you to pass entire functions as parameters to other functions. So that’s what we’re seeing with the more consise way of just writing the whole function within the filter hook instead of declaring it separately. (If you’re familiar with Javascript, you see this convention more often there.) More traditionally you might have seen it written like so:
function whatever_function_name_you_want( $content, $inserted_page, $attributes ) { ... } add_filter( 'insert_pages_wrap_content', 'whatever_function_name_you_want', 10, 3 );Thanks a lot Ryan, for your detailled descriptive.
The topic ‘How to Insert Page with embedded text replace feature’ is closed to new replies.