League Table Shortcode

Below, you’ll find a detailed guide on how to add the League Table Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the League Table Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the League Table Plugin and the shortcodes it provides:

Plugin Icon
League Table

"League Table is a versatile WordPress plugin that allows you to create responsive, sortable, and customizable tables, perfect for sports leagues, statistics, and comparative data."

✩✩✩✩✩ () Active Installs: 2000+ Tested with: 6.2.3 PHP Version: 7.2
Included Shortcodes:
  • []

League Table [null] Shortcode

The League Table Lite shortcode is used to display a league table on a WordPress page or post. The shortcode is: [ltl]. It fetches a specified league table from the database and displays it. The table ID is passed as an attribute to the shortcode. If the table doesn’t exist or the ID is not provided, an error message is displayed. The shortcode also handles table properties like cell properties, column width, and manual sorting. It can also add images or links to cells.

Shortcode: [null]

Parameters

Here is a list of all possible null shortcode parameters and attributes:

  • id – Unique identifier of the league table

Examples and Usage

Basic example – Display a league table by referencing its ID.

[ltl id=1 /]

Advanced examples

Display a league table by referencing its ID. If the table is not found by ID, it will display a message asking to enter the identifier of the table.

[ltl id=2 /]

Display a league table by referencing its ID. If the table is not found by ID or if the same shortcode is used multiple times, it will display a message stating that the same shortcode cannot be used multiple times.

[ltl id=3 /]

PHP Function Code

In case you have difficulties debugging what causing issues with [null] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('ltl', array($this, 'display_league_table'));

Shortcode PHP function:

function display_league_table($atts)
    {

        /*
         * Parse the shortcode only inside the full content of posts and pages if the "Limit Shortcode Parsing" option
         * is enabled. Do not parse the shortcode inside feeds.
         */
        if (!is_feed() and ((is_single() or is_page()) or intval(get_option($this->shared->get('slug') . '_limit_shortcode_parsing'), 10) == 0)) {

            //get the table id
            if(isset($atts['id'])){
                $table_id = intval($atts['id'], 10);
            }else{
	            return '<p>' . esc_attr__('Please enter the identifier of the table.', 'league-table-lite') . '</p>';
            }

            //get table object
            global $wpdb;
            $table_name = $wpdb->prefix . $this->shared->get('slug') . "_table";
            $safe_sql = $wpdb->prepare("SELECT * FROM $table_name WHERE id = %d AND temporary = 0", $table_id);
            $table_obj = $wpdb->get_row($safe_sql);

            //terminate if there is no table with the specified id
            if ($table_obj === NULL) {
                return '<p>' . esc_attr__('There is no table associated with this shortcode.', 'league-table-lite') . '</p>';
            }

            //terminate if this table id has already been used
            if ( intval(get_option($this->shared->get('slug') . '_verify_single_shortcode'), 10) === 1 and
                 in_array($table_id, self::$shortcode_id_a, true)) {
                return '<p>' . esc_attr__("You can't use multiple times the same shortcode.", 'league-table-lite') . '</p>';
            }

            //store the shortcode id
            self::$shortcode_id_a[] = $table_id;

            //the tables property saves all the tables included in this post
            $this->tables[] = $table_obj;

            //generate output ------------------------------------------------------------------------------------------

            //get table data
            global $wpdb;
            $table_name = $wpdb->prefix . $this->shared->get('slug') . "_data";
            $safe_sql = $wpdb->prepare("SELECT * FROM $table_name WHERE table_id = %d ORDER BY row_index ASC", $table_id);
            $results = $wpdb->get_results($safe_sql, ARRAY_A);

            if (intval($table_obj->enable_cell_properties, 10) == 1) {

                //get the cell properties of all the cell of the table
                global $wpdb;
                $table_name = $wpdb->prefix . $this->shared->get('slug') . "_cell";
                $safe_sql = $wpdb->prepare("SELECT * FROM $table_name WHERE table_id = %d", $table_id);
                $table_cells_properties = $wpdb->get_results($safe_sql, ARRAY_A);

            }

            //turn on output buffer
            ob_start();

            ?>

            <!-- Generate the "table" HTML element -------------------------------------------------------------------->
            <table id="daextletal-table-<?php echo $table_id; ?>"
                   class="daextletal-table" <?php echo $this->generate_table_data_attributes($table_obj); ?>>

	            <?php

	            //Generate the "colgroup" HTML element -----------------------------------------------------------------
	            if ( intval( $table_obj->column_width, 10 ) === 1 ) {

		            //extract the numeric values in column width value in an array
		            $column_width_value_a = explode( ',', preg_replace( '/\s+/', '', $table_obj->column_width_value ) );

		            //if column width value doesn't include any numeric value do nothing
		            if ( count( $column_width_value_a ) > 0 ) {

			            if ( count( $column_width_value_a ) === 1 ) {

				            //if column width value is a single value apply the value to all the columns
				            $number_of_columns = $this->shared->get_number_of_columns( $table_id, true );
				            echo '<colgroup>';
				            for ( $i = 0; $i < $number_of_columns; $i ++ ) {
					            echo '<col style="width: ' . intval( $column_width_value_a[0], 10 ) . 'px;">';
				            }
				            echo '</colgroup>';

			            } else {

				            //if column width value are multiple values apply the various values to the columns
				            echo '<colgroup>';
				            foreach ( $column_width_value_a as $key => $column_width ) {
					            echo '<col style="width: ' . intval( $column_width, 10 ) . 'px;">';
				            }
				            echo '</colgroup>';

			            }


		            }

	            }

	            ?>

                <!-- Generate the "thead" HTML element ---------------------------------------------------------------->
                <thead>
                <tr>
                    <?php

                    foreach ($results as $key1 => $value) {
                        if ($key1 > 0) {
                            break;
                        }
                        $row_data = json_decode($value['content'], true);

                        foreach ($row_data as $key2 => $cell_data) {

                            $cell_data = esc_html($cell_data);

                            if (intval($table_obj->enable_cell_properties, 10) == 1) {

                                /*
                                 * Search the properties of this cell in the array which include all the cell properties
                                 * of this table
                                 */
                                $cell_properties = false;
                                foreach ($table_cells_properties as $key => $val) {
                                    if ($val['row_index'] == $key1 and $val['column_index'] == $key2) {
                                        $cell_properties = $val;
                                        break;
                                    }
                                }

                            }

                            if (isset($cell_properties) and $cell_properties !== false and intval($table_obj->enable_cell_properties, 10) == 1) {

                                if (intval($table_obj->enable_cell_properties, 10) == 1) {

	                                /*
                                     * Use a link instead of the text if the "link" property is set and the
                                     * "Enable Manual Sorting" option is disabled
                                     */
	                                if (strlen(trim($cell_properties['link'])) > 0 and intval($table_obj->enable_manual_sorting, 10) == 0) {
		                                $cell_data = '<a href="' . esc_url(stripslashes($cell_properties['link'])) . '">' . $cell_data . '</a>';
	                                }

                                    //Add an image to the left of the cell if the "image_left" property is set
                                    if (strlen(trim($cell_properties['image_left'])) > 0) {
                                        $cell_data = '<img class="daextletal-image-left" src="' . esc_url(stripslashes($cell_properties['image_left'])) . '">' . $cell_data;
                                    }

                                    //Add an image to the right of the cell if the "image_right" property is set
                                    if (strlen(trim($cell_properties['image_right'])) > 0) {
                                        $cell_data = '<img class="daextletal-image-right" src="' . esc_url(stripslashes($cell_properties['image_right'])) . '">' . $cell_data;
                                    }

                                }

                            }

	                        echo '<th>' . $cell_data . '</th>';

                        }

                    }

                    ?>
                </tr>
                </thead>

                <!-- Generate the "tbody" HTML element ---------------------------------------------------------------->
                <tbody>

                <?php


                foreach ($results as $key1 => $value) {
                    if ($key1 == 0) {
                        continue;
                    }
                    $row_data = json_decode($value['content'], true);
                    echo '<tr>';
                    foreach ($row_data as $key2 => $cell_data) {

                        $cell_data = esc_html($cell_data);

                        if (intval($table_obj->enable_cell_properties, 10) === 1) {

                            /*
                             * Search the properties of this cell in the array which include all the cell properties
                             * of this table
                             */
                            $cell_properties = false;
                            foreach ($table_cells_properties as $key => $val) {
                                if ($val['row_index'] == $key1 and $val['column_index'] == $key2) {
                                    $cell_properties = $val;
                                    break;
                                }
                            }

                        }

                        if (isset($cell_properties) and $cell_properties !== false and intval($table_obj->enable_cell_properties, 10) == 1) {

                           if (intval($table_obj->enable_cell_properties, 10) == 1) {

	                           //Use a link instead of the text if the "link" property is set
	                           if (strlen(trim($cell_properties['link'])) > 0) {
		                           $cell_data = '<a href="' . esc_url(stripslashes($cell_properties['link'])) . '">' . $cell_data . '</a>';
	                           }

                                //Add an image to the left of the cell if the "image_left" property is set
                                if (strlen(trim($cell_properties['image_left'])) > 0) {
                                    $cell_data = '<img class="daextletal-image-left" src="' . esc_url(stripslashes($cell_properties['image_left'])) . '">' . $cell_data;
                                }

                                //Add an image to the left of the cell if the "image_right" property is set
                                if (strlen(trim($cell_properties['image_right'])) > 0) {
                                    $cell_data = '<img class="daextletal-image-right" src="' . esc_url(stripslashes($cell_properties['image_right'])) . '">' . $cell_data;
                                }

                            }

                        }

	                    echo '<td>' . $cell_data . '</td>';

                    }
                    echo '</tr>';
                }

                ?>


                </tbody>

            </table>

            <?php

            $out = ob_get_clean();

            //If the container is enabled include the table in a container
            if (intval($table_obj->enable_container, 10) == 1) {
                $out = '<div class="daextletal-table-container">' . $out . '</div>';
            }

            return $out;

        }

    }

Code file location:

league-table-lite/league-table-lite/public/class-daextletal-public.php

Conclusion

Now that you’ve learned how to embed the League Table Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *