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.
function build_total_search_method( $atts ) {

    $shorcode_php_function = include( dirname(__FILE__) . "/includes/total_search.php" );

    return $shorcode_php_function;

}
add_shortcode( 'total_search', 'build_total_search_method' );

I've created a shortcode that returns an included PHP file instead of a string ... however, after the returned statement, there is a 1 printed.

Two questions:

1) What is the `1` all about and how do I remove it?

2) Am I doing this properly?
share|improve this question
1  
What is in /includes/total_search.php? –  Chip Bennett Jan 22 '13 at 1:16
    
Pure HTML, that's it. –  dcolumbus Jan 22 '13 at 17:52

3 Answers 3

up vote 1 down vote accepted

You are returning the result of calling include. You want to grab the output of the included file and return than instead. Use output buffering to do that.

function build_total_search_method( $atts ) {

    ob_start();
    include( dirname(__FILE__) . "/includes/total_search.php" );
    $shorcode_php_function = ob_get_clean();

    return $shorcode_php_function;

}
add_shortcode( 'total_search', 'build_total_search_method' );
share|improve this answer
    
Are there any performance hits with this? –  dcolumbus Jan 22 '13 at 17:54
    
If the included file has no PHP statements, then there is a slight performance benefit to using Dan's solution. If it does have PHP statements, this is how you need to do it. –  webaware Jan 22 '13 at 22:17

you're getting the 1 from your include, meaning your include succeeded. if you want to assign the value to a variable, your included file can only return data. example, in total_search.php:

<?php
$some_var = 'Some content';
return $some_var;
share|improve this answer

include() (and the usually-safer include_once()) return whether they succeeded or not. You're seeing a 1 because that is the successful return value. Incidentally, it returns FALSE, not 0 on failure (no idea why).

To do what you are trying to achieve, @webaware's solution will work, but a less round-about (and more efficient solution for what it looks like you are trying to do would be to use file_get_contents() like so:

function build_total_search_method( $atts ) {
    return file_get_contents( dirname(__FILE__) . "/includes/total_search.php" );
}
add_shortcode( 'total_search', 'build_total_search_method' );

Note that I also stripped out the extra variable since it was never used and I think it becomes less readable with that extra line.

share|improve this answer
    
include doesn't always return 1 on success. –  Milo Jan 22 '13 at 2:10
    
Technically, you're correct, but that is the default behavior. "include returns FALSE on failure and raises a warning. Successful includes, unless overridden by the included file, return 1." (php.net/manual/en/function.include.php) –  Dan Jan 22 '13 at 2:16
1  
Dan's solution is correct if the contents of the included .php don't include any PHP statements that need to be executed (and he's right, it's faster). My solution is needed if there are PHP statements, e.g. template tags. –  webaware Jan 22 '13 at 2:51
1  
@Dan Is include_once appropriate here - the shortcode could appear twice? –  Stephen Harris Jan 22 '13 at 11:24
1  
@StephenHarris, agreed. Given that the poster seemed very new to PHP, I wanted to include that bit in there for future reference, but for this particular instance it would probably not be appropriate. –  Dan Jan 22 '13 at 13:41

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.