WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Including a PHP file via a function that is part of a plugin? (16 posts)

  1. derekbeck
    Member
    Posted 1 year ago #

    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.)

  2. s_ha_dum
    Member
    Posted 1 year ago #

    What does your contact.php look like? I am betting that it drops in and out of PHP instead of creating a proper string to return. Every time you drop out of PHP you are printing to the browser. I am pretty sure that is why your form is printing out in the head of the page. Just a guess...

  3. derekbeck
    Member
    Posted 1 year ago #

    Thanks for the reply. It is a fairly complicated contact.php, which technically just generates the form, and the calls on another php to execute the form. I am using a slightly modified form of the contact form offered at Free PHP Formmail Generator : http://phpfmg.sourceforge.net. I'm reluctant to modify it further of course, because it is fairly complex and I didn't write the code, so I'd have to study it carefully were I to modify it. If by "drops in and out of of PHP" you mean stuff like:

    <li class='field_block' id='phpfmg_captcha_div'>
    	<div class='col_label'><label class='form_field'>Security Code:</label> <label class='form_required' >*</label> </div><div class='col_field'>
    	<?php phpfmg_show_captcha(); ?>
    	</div>
    </li>

    Then yes, it does that quite a bit.

    Thanks for the reply!

  4. s_ha_dum
    Member
    Posted 1 year ago #

    Yes. That is what I mean. Everything not inside PHP tags-- <?php and ?> is going to print out immediately. That isn't going to work in the context. I am a little puzzled why your ob_start() idea doesn't work.

  5. derekbeck
    Member
    Posted 1 year ago #

    Yeah, I'm puzzled about that too. It worked for me for other features I put in. I just re-implemented that version of the code for the page http://www.derekbeck.com/1775/test/ if you want to look at it.

  6. toocoolone
    Member
    Posted 1 year ago #

    Have you tried dropping the '../' when declaring an absolute path...that seems unnecessary and may be why the file isn't being found?

    function ContactInsert() {
           ob_start();
           include(ABSPATH . "/contact.php");
           return ob_get_clean();
    }
  7. derekbeck
    Member
    Posted 1 year ago #

    the contact.php is in a directory above, for various reasons. But the code IS finding the contact.php, because, if I say, put merely contact.php, or even ../../../contact.php , it will render on the wordpress page something like "Error on line 32 of inserts.php, file contact.php not found." (Insert.php being my plugin.)

    So the ../ is definitely not the issue, and in fact is working right. It is just not rendering. There some code error or limitation here that I don't understand.

  8. s_ha_dum
    Member
    Posted 1 year ago #

    it will render on the wordpress page something like "Error on line 32 of inserts.php,

    That is important. What is on line 32 of that file?

  9. derekbeck
    Member
    Posted 1 year ago #

    Line 32 is simply the

    include(ABSPATH . "../contact.php");

    It calls an error if I don't have the ../ because contact.php is not at the same folder level as the plugin I've written. If I do indeed put the proper path, with the ../ , there is no error reported, it just doesn't render properly.

    I am sure the issue is not a matter of the path address, it is something else.

  10. derekbeck
    Member
    Posted 1 year ago #

    PS: I've returned the test page to this code construct:

    add_shortcode( 'ContactForm', 'ContactInsert' );
    
    function ContactInsert() {
           $file = include(ABSPATH . "../contact.php");
    	return $file;
    }
  11. s_ha_dum
    Member
    Posted 1 year ago #

    include isn't going to capture your file as a string, even if you use file =. This is basically where you started. You should get the same results without the return $file;.

  12. derekbeck
    Member
    Posted 1 year ago #

    What I can't figure out is why it appears on the test page with the above code, yet does not appear where my shortcode is calling it.

  13. s_ha_dum
    Member
    Posted 1 year ago #

    I already explained that. Your code, unless you have altered it, is jumping in and out of PHP. Everything not inside PHP tags will print immediately. You need to construct and return a string.

  14. derekbeck
    Member
    Posted 1 year ago #

    Ok, I will try rebuilding the code.

    I must admit I don't understand "why" the code works but places it at the top, but must be rewritten to buffer in the right place. But I will do as you suggest and report back.

    I hope I won't find trouble because contact.php calls upon a master email.php as well...

  15. derekbeck
    Member
    Posted 1 year ago #

    Ok, I've tried all of this, but still no luck.

    Finally, I went the next step and put all of the code into the plugin itself, instead of calling contact.php. Now, these (3) functions that were in contact.php still calls another library.php but that's a core file for my various sites. It seems to work better, and appears to work at first glance, but doesn't. For various reasons, I put this new attempt at

    http://test.derekbeck.com/1775/test/

    (my testing site, in part because this new attempt interferes with my working contact form on my life site)

    Notice it looks nice? But, try sending a message, and no matter how good you are at deciphering the recaptcha, you'll get an error that you got the recaptcha wrong. Now I haven't changed any of that code, and it works fine in my live contact form ( http://www.derekbeck.com/1775/contact/ ), so something is being affected by plugin-izing this. Oddly enough, though you get an error message on the recaptcha, if you got it right, it does send through.

    This is obviously become a complex question for the forum.

    One thing that would make life easier:

    Is there no way, beside ob_start(), to simply include a file via a plugin and get it to be, maybe no in the content, but at the start or end of the content? I'm convinced ob_start is the cause of the trouble, and yet if I don't use it, I cannot manage to get the contact form placed in the right position.

    For instance, I am using shortcode to call the form. But is there some way to just include the file ahead of the content based on the page ID? Basically, via a plugin, is there a way for me to mimic what I was doing before: hard-coding include('../contact.php'); into my index.php so that it appears after the content.

    That is, is there a way for me to mimic this, which I was doing by hard-coding before into my theme:

    if ( is_page('Contact') ) {
    				include("../contact.php");
    			}

    Thanks, derek

  16. derekbeck
    Member
    Posted 1 year ago #

    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

    Thanks for everyone's help though!

    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();
        }

Topic Closed

This topic has been closed to new replies.

About this Topic