1
votes

This is my function:

            function include_product_table( $atts ) {
            $a = shortcode_atts( array(
            'tablename' => ''
            ), $atts );
            $tablefile = '/home/panacol1/data/wp-content/themes/Divi-child/producttables/pt-'.$a[ 'tablename' ].'.php';

            return include $tablefile;

            }
            add_shortcode('producttable', 'include_product_table');

My post contains text content with some NextGen images and then ends with:

'[producttable tablename="bonding-plastics-and-dissimilar-substrates"]'

Which calls a file that contains the HTML for a table. But the table displays ahead of the content. It is also displayed above the title on the posts edit page in the Wordpress Dashboard.

If I remove 'include' from the line ' return include $tablefile;' to make it 'return $tablefile;' the path to the file displays at the bottom of the content as I want the table to.'

Any help is appreciated.

3

3 Answers

1
votes

You can do this a couple ways. I don't know what your tablename.php file looks like, but I'll assume it's basically flat HTML.

function include_product_table( $atts ) {
    $a = shortcode_atts( array(
      'tablename' => ''
    ), $atts );
    $tablefile = ABSPATH . '/wp-content/themes/Divi-child/producttables/pt-'.$a[ 'tablename' ].'.php'; // filepath relative to WP install path.
    if (!is_file($tablefile)) { 
          // Bail out if the file doesn't exist
          return false;
    }

    ob_start();
    include $tablefile;
    $table_data = ob_get_contents();
    ob_end_clean();

    return $table_data;
}
add_shortcode('producttable', 'include_product_table');

A PHP output buffer will capture any HTML the tablename.php wants to display and put it into $table_data when you call ob_get_contents();.

Running ob_end_clean() is just cleaning up after yourself.

0
votes

if you draw table in php file I suggest you put table information in array then:

  function include_product_table( $atts ) {
        $a = shortcode_atts( array(
        'tablename' => ''
        ), $atts );
        $tablefile = '/home/panacol1/data/wp-content/themes/Divi-child/producttables/pt-'.$a[ 'tablename' ].'.php';

        require_once($tablefile);

        table_information = function_in_your_php_file_return_table_information();

        return table_information;

        }
        add_shortcode('producttable', 'include_product_table');

you can draw it in this function then return it or return information then draw it.

0
votes

you should not return , include function in php. for example your table file (pa-1.php) :

<?php
function p_1(){
return "Hi";
}
?>

and your shortcode :

function include_product_table( $atts ) {
            $a = shortcode_atts( array(
            'tablename' => ''
            ), $atts );
            $tablefile = get_template_directory().'/Divi-child/producttables/pt-'.$a[ 'tablename' ].'.php';

 include $tablefile;
$function_name = "p_".$a[ 'tablename' ];
return $function_name();

            }
            add_shortcode('producttable', 'include_product_table');