Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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: http://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: http://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.)

share|improve this question
    
In trying to figure this out, I put the function back to the one with ob_start() as described above. –  Derek W Beck Aug 30 '12 at 19:33
    
I've returned the Test Page back to the code: add_shortcode( 'ContactForm', 'ContactInsert' ); function ContactInsert() { $file = include(ABSPATH . "../contact.php"); return $file; } –  Derek W Beck Sep 6 '12 at 21:36
add comment

2 Answers

By creating the above short-code you are trying to call/include entire file wherever [ContactForm] is inserted on your page whereas A short-code is supposed to return a value or output some content. Since you don't want a plugin, the best solution I could think of is to create a custom page template. As it's not a part of your default theme structure so even updating the theme won't affect it.

share|improve this answer
    
Can't I do this somehow as a plugin? Otherwise, any tips on making it a template? –  Derek W Beck Aug 30 '12 at 4:06
    
If you are simply looking to create a "contact" Plugin I would rather suggest you to use existing ones like Contact Form 7 (Free) or Gravity forms(Paid). Re-inventing the wheel would be a waste of time. Creating a page template is easy. You can find the instructions on WP Codex codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates –  Puneet Sahalot Sep 2 '12 at 9:01
    
Thanks for the advice, but I'm not re-inventing anything. I've used this form on multiple sites, it already works, and it uses common libraries that I'm using across multiple sites. I just need to include it properly in WP. If I add it to the index.php it works fine. I want it added as a plugin. –  Derek W Beck Sep 3 '12 at 5:37
add comment
up vote 0 down vote accepted

The answer to this was not, probably (not tested) the formatting of the code, but the complicated form in that include. I've worked with the code designer to derive the solution, but it is too complicated for here. If you want to know how the WP plugin of my contact form works, it is now available for all here: http://formmail-maker.com/wordpress-plugin-form-loader.php

Cheers, Derek

PS: In general, I believe this code should work for simple includes via a plugin:

function ContactInsert() {
      ob_start();
      include(ABSPATH . "../contact.php");
      return ob_get_clean();
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.