Including a PHP file via a function that is part of a plugin?
-
I struggled with this all day, and now am here…
I have a contact form generated by including a php. Presently, I have this code in my theme’s index.php to include it:
if ( is_page('contact') ) { include("../contact.php"); }
which renders and functions as expected: https://www.derekbeck.com/1775/contact/
However, editing this into the theme is problematic because of theme upgrades (I have to hard code it back in each time) and now, especially, because I’m using a mobile theme for mobile users, who will not see my contact form when they use the mobile theme. Bottom line: I need to put this into a plugin.
I know the mechanics of making a plugin, that’s not my question.
My question is: I cannot seem to figure out a way to properly include the above via a function in my plugin file.
For instance, I’ve tried this:
add_shortcode( 'ContactForm', 'ContactInsert' ); function ContactInsert() { $file = include(ABSPATH . "../contact.php"); return $file; }
And I place the shortcode [ContactForm] into the body of my test page, but it renders strange results:
https://www.derekbeck.com/1775/test/
(ignore the “1” and “2” which I added for testing)If I make it simply
function ContactInsert() { ob_start(); include(ABSPATH . "../contact.php"); return ob_get_clean(); }
I get no results on the page, and only have of the theme displays.
What am I doing wrong?
Thanks!
(PS: If I were to simply paste all of that included contact.php into my plugin page, it might work, but it is a very long file, and I have other reasons to keep it separate.)
- The topic ‘Including a PHP file via a function that is part of a plugin?’ is closed to new replies.