• Resolved boddhid

    (@boddhid)


    Hi all,
    I wrote my first shortcode based on https://pagely.com/blog/creating-custom-shortcodes/
    So I added

    
    function members_index($params = array()) {
    	return include(get_theme_root() . '/includes/index.php');
    }
    add_shortcode('members-index', 'members_index');
    

    to functions.jsp and the php-script to /themes/includes.
    It all works fine, see
    http://website.eftcd.de/test/?lst_id=2
    The same script, but then standalone can be found here:
    http://website.eftcd.de/wp-content/themes/includes

    The only issue I run into is that the digit “1” is added at the bottom, and I can not figure out why.

    I checked for three things:
    (1) The script is not the problem
    I added a test shortcode for an empty php-script, it produces nothing (as should) except the “1” (as shouldn’t). See: http://website.eftcd.de/test2/
    (2) This particular 5.3.1 WordPress installation is not the problem. I also added the test shortcode to an empty php-script to another 5.2.5 WordPress installation. The same results, a “1” were there shouldn’t be one. See: https://www.holdmetight.eu/test/
    (3) I couldn’t find anything on the issue in Google.

    What am I doing wrong? Any help is appreciated.

    Best,
    Hendrik

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Your shortcode handler is returning a boolean, which is then output as the shortcode result of 1. As a side effect of the include statement, your code is being executed and outputting directly, which shortcode handlers should never do. Shortcode handlers should always return a string, and no output should occur.

    Moderator bcworkz

    (@bcworkz)

    Here’s a tip if you need to include within a shortcode handler a file which echoes out content: Use the PHP output buffer to capture output which can then be collected and returned by the handler function.

    Thread Starter boddhid

    (@boddhid)

    Thanks for pointing out! Of course “1” is equivalent with “true” in PHP – how could I forget.

    I changed the shortcode handler (see below) and now it works as a charm.

    
    function members_index($params = array()) {
    	ob_start();
    	include(get_theme_root() . '/includes/index.php');
    	$ob_str=ob_get_contents();
    	ob_end_clean();
    	return $ob_str;
    }
    
    // register shortcode
    add_shortcode('members-index', 'members_index');
    
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Shortcode add 1 to page’ is closed to new replies.