Support » Fixing WordPress » Form included via shortcode showing stray "1" below submit button

  • ResolvedModerator Kathryn

    (@zoonini)


    I’ve been trying to use shortcodes on a site in order to pull in a form on only certain pages.

    Here is what I added to my functions.php:

    add_shortcode('inquiry','inquiry_function');
    function inquiry_function() {
       $inquiry = include('contact-form-sidebar.php');
       return $inquiry;
    }

    I can then add forms to certain pages by using this shortcode:

    [inquiry]

    It works great except for one thing. For some reason, a stray number 1 is getting added just below the form. I cannot for the life of me figure out why. It is just below the form’s submit button – here’s a screenshot: http://www.twitpic.com/5ze4xv

    If I include the file directly via the template file by using

    include ('contact-form-sidebar.php');

    …the form appears correctly, without the stray “1” – so it doesn’t appear to be an issue with the form code itself.

    Any ideas where the stray “1” is coming from? Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Kathryn

    (@zoonini)

    I think I’ve found a solution. I’ve modified the shortcode function as follows to adjust the way the included file is being called in:

    add_shortcode('inquiry','inquiry_function');
    function inquiry_function() {
    	ob_start();
    	include('contact-form-sidebar.php');
    	$inquiry = ob_get_clean();
    	return $inquiry;
    }

    I’ll admit that I’m not sure why this took care of the stray 1 but apparently it’s a more desirable way of including a file within a function, according to my Googling. (If anyone knows why this worked I’d love to know!)

    Hi Zoonini!

    PHP’s include statement was successful and the file you are including most likely has no return value. When it’s output is stored in a variable, the value will be “1”. Please see php doc example 130 for a better explanation of this.

    So, when you look at this code:

    $inquiry = include('contact-form-sidebar.php');

    Two things are happening:

    1. A file is being included which I assume contains html.
    2. The result of the include state,ent is stored in $inquiry.

    When you return the value of $inquiry, WordPress will “inject” it into the post_content in place of the shortcode. Would be interesting to see what happens using this function with content both before and after the shortcode.

    Hope this makes sense 🙂

    Moderator Kathryn

    (@zoonini)

    Wow, that actually does make sense – thanks so much for explaining it, Michael! The mystery of the stray 1 is solved.

    Would be interesting to see what happens using this function with content both before and after the shortcode.

    I did have content displaying both before and after the shortcode in the sidebar – being called in via the sidebar template file – and it actually came out fine. Should it not have?

    No problem!

    I did have content displaying both before and after the shortcode in the sidebar – being called in via the sidebar template file – and it actually came out fine. Should it not have?

    Honestly, I have no idea 🙂 I know that printing would not. Never tried it with an included file though. Take for instance the following scenario:

    functions.php

    add_shortcode('inquiry','inquiry_function');
    function inquiry_function() {
       print '<p>PRINT</p>';
       return '<p>THIS IS THE RETURN VALUE</p>';
    }

    Post Content:

    Up Top.
    
    [inquiry]
    
    Down Below.

    Whe you view the post, the following is generated:

    PRINT
    Up Top.
    THIS IS THE RETURN VALUE
    Down Below.

    Notice how print is at the top of the content. I thought that the same might happen with includes too.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Form included via shortcode showing stray "1" below submit button’ is closed to new replies.