Shortcode to include PHP file, pass various parameters to include?

Thanks for your comments and guidance. I implemented solutions which didn’t reinvent the wheel.

The complication was finding a way to remove the [insert_php][/insert_php] wrapped code from pages and posts and still preserve the content and user experience.

Custom Template Pages

Pages and Posts which were almost entirely PHP were made into custom template files. These were placed into my child theme folder. Old posts and pages were updated to the custom templates.

For large websites, I suggest writing a command line script to bulk update post templates. Or, use a good plugin.

Created Short Codes for Content Snippets

All instances of PHP includes, tables and DB calls within posts and pages were rewritten as short codes. These codes were added to my child theme’s functions.php file.

This preserved essential tables, ads and images. Also, I removed text from within the PHP wrapper and placed it back into the WP content. This makes for much easier content management and integration with helpful plugins like Yoast SEO.

Easier to update content

I’ve found periodically re-coding my content functions improves efficiency and maintenance. It’s similar to editing and rewriting written prose.

Making these changes was lengthy. However it will be much easier to update content snippets in the future!

Faster Website

Removing the [insert_php][/insert_php] content and plugin improved server performance and page speed. This is critical for SEO. Google Page Speed Insights noted a slight but noticeable improvement in server response time, roughly 0.30 ~ 0.23 for most pages.

Previously the server had to process all WP PHP, then run my own PHP wrapped inside each post. This was redundant and wasted time.

More Secure

Running a PHP wrapper inside WP content creates a substantial security vulnerability. Remember, these wrappers allow any PHP code to be executed, including MySQL, Exec, etc. commands. This is dangerous even if you have secured your WP site and database.

Now my sites are much safer. As an aside, I suggest using 2 Factor authentication for WP login. If someone gets in and runs PHP code (or installs a PHP wrapper plugin and subsequently runs code) you could be in big trouble!

2 Factor login adds an extra layer of security even if you password gets compromised. I recommend a plugin with Google Authenticator.

Do PHP Plugins Have Any Valid Purpose?

At this point I’m not sure. Given immense security risks, I wouldn’t recommend them for large, high traffic websites.

I definitely would not recommend them if you’re including DB logins, passwords and SQL queries to non-WP DBs in your code. If you must, make sure your DB user is limited to one database and “SELECT” queries. Do NOT give such users global privileges.

They might be helpful for testing. It is much easier for newbies to use these plugins while experimenting with content and design ideas than custom-coding a template. But I would highly recommend doing so after testing is done.

Finally, if it is possible to limit PHP commands and output, these plugins might be less risky.