• I have written a shortcode to include the contents of a php file in my posts but no matter where I place the shortcode, it always displays at the top of the post before the title. Here is the code added to my functions.php:

    function inline() {
        return include (TEMPLATEPATH . '/inline.php');;
    }
    add_shortcode('in', 'inline');

    I’ve also tried:

    function inline() {
    	global $content;
    	$output = include ( TEMPLATEPATH . '/inline.php' );
        return $output;
    }
    add_shortcode('in', 'inline');

    It doesn’t seem to matter what the contents of my inline.php file are I’ve tried changing it to a number of things from just text to js but nothing changes. It appears to the the fact that I’m returning the php include function. When I change the return to be ‘hello’ then it displays in the correct place, but when I change it back to the include statement it jumps to the top of the post.

    Most of the people I have seen with this problem have it because they are using “echo” instead of “return” but I’m not. I found a couple of posts by other people but their solutions make no sense to me.

    Like this for example, doesn’t make sense to me. I tried it anyway and it didn’t work.

    Please help!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter 610m

    (@610m)

    I forgot to mention, that when it the shortcode return to the top of the post, it puts the number “1” in the place where the shortcode is supposed to return.

    Thread Starter 610m

    (@610m)

    I’ve also tried this but it didn’t work:

    function inline() {
        $output = include ( TEMPLATEPATH . '/inline.php' );
        return $output;
    }
    add_shortcode('in', 'inline');

    funny, I am working on the exact same issue, created a custom shortcode and it goes to the top of the screen, if you find a solution twitter me @qwik3r

    in theory it should work, I think you have an issue with the include, you can run a test and replace output with a string and see if it shows up.

    Thread Starter 610m

    (@610m)

    I found a solution that works. I don’t know why it works but it does:

    function include_file($atts) {
    	//check the input and override the default filepath NULL
    	//if filepath was specified
    	extract(shortcode_atts(array('filepath' => 'NULL'), $atts));
    	//check if the filepath was specified and if the file exists
    	if ($filepath!='NULL' && file_exists(TEMPLATEPATH.$filepath)){
    	//turno on output buffering to capture script output
    	ob_start();
    	//include the specified file
    	include(TEMPLATEPATH.$filepath);
    	//assign the file output to $content variable and clean buffer
    	$content = ob_get_clean();
    	//return the $content
    	//return is important for the output to appear at the correct position
    	//in the content
    	return $content;
    	}
    }
    //register the Shortcode handler
    add_shortcode('include', 'include_file');

    To call the file:

    Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
    
    [include filepath="/SomDirectory/MyIncludedFile.php"]
    
    Duis libero orci, pretium a, convallis quis, pellentesque a, dolor.

    Found it here: https://www.amberpanther.com/knowledge-base/using-the-wordpress-shortcode-api-to-include-an-external-file-in-the-post-content/

    @qwik3r Thanks for the response. I haven’t had a chance to check that out yet. Hope the solution I found helps you.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Shortcode is displaying in the wrong place’ is closed to new replies.