• Resolved derekbeck

    (@derekbeck)


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

Viewing 15 replies - 1 through 15 (of 15 total)
  • 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…

    Thread Starter derekbeck

    (@derekbeck)

    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 : https://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!

    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.

    Thread Starter derekbeck

    (@derekbeck)

    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 https://www.derekbeck.com/1775/test/ if you want to look at it.

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

    Thread Starter derekbeck

    (@derekbeck)

    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.

    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?

    Thread Starter derekbeck

    (@derekbeck)

    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.

    Thread Starter derekbeck

    (@derekbeck)

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

    add_shortcode( 'ContactForm', 'ContactInsert' );
    
    function ContactInsert() {
           $file = include(ABSPATH . "../contact.php");
    	return $file;
    }

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

    Thread Starter derekbeck

    (@derekbeck)

    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.

    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.

    Thread Starter derekbeck

    (@derekbeck)

    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…

    Thread Starter derekbeck

    (@derekbeck)

    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

    https://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 ( https://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

    Thread Starter derekbeck

    (@derekbeck)

    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: https://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();
        }
Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Including a PHP file via a function that is part of a plugin?’ is closed to new replies.