Static Block Shortcodes

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

Before starting, here is an overview of the Static Block Plugin and the shortcodes it provides:

Plugin Icon
Static Block

"Static Block is a robust WordPress plugin that enables you to create and manage static content blocks with ease. Ideal for adding repetitive text or images across your website."

★★★★☆ (5) Active Installs: 2000+ Tested with: 5.7.10 PHP Version: false
Included Shortcodes:
  • [static_block_content]
  • [static_block_thumbnail]

Static Block [static_block_content] Shortcode

The ‘static_block_content’ shortcode is used to display specific content based on the current date and time. It fetches data from post meta fields, calculates start and end times, and checks if the current time falls within the specified range. If the current time is within the range, the shortcode displays the content of the specified post. This is useful for displaying time-sensitive content.

Shortcode: [static_block_content]

Parameters

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

  • id – The unique identifier for a specific post
  • tr_start_date – The start date of the event
  • tr_start_date_hour – The hour the event starts
  • tr_start_date_minute – The minute the event starts
  • tr_end_date – The end date of the event
  • tr_end_date_hour – The hour the event ends
  • tr_end_date_minute – The minute the event ends

Examples and Usage

Basic example – Displaying static block content by referencing ID

[static_block_content id=1 /]

This shortcode will fetch and display the static block content associated with the ID provided. In this case, the static block content with ID ‘1’ will be displayed.

Advanced examples

Displaying static block content by referencing ID and adjusting start and end dates/times

[static_block_content id=1 tr_start_date="2022-01-01" tr_start_date_hour="09" tr_start_date_minute="00" tr_end_date="2022-12-31" tr_end_date_hour="17" tr_end_date_minute="00" /]

In this advanced example, we are not only referencing the ID of the static block content, but we are also defining the start and end dates and times for the displayed content. The ‘tr_start_date’ and ‘tr_end_date’ parameters define the start and end dates, respectively, while the ‘tr_start_date_hour’, ‘tr_start_date_minute’, ‘tr_end_date_hour’, and ‘tr_end_date_minute’ parameters define the start and end times, respectively. In this case, the static block content with ID ‘1’ will be displayed from 9:00 AM on January 1, 2022, until 5:00 PM on December 31, 2022.

PHP Function Code

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

Shortcode line:

add_shortcode('static_block_content','static_block_content');

Shortcode PHP function:

function static_block_content($atts, $content=NULL){
    $atts = shortcode_atts( array(
        'id' => ''
    ), $atts, 'static_block_content' );
    $id = $atts['id'];
    $startDate = get_post_meta( $id, 'tr_start_date', true );
    $startHour = get_post_meta( $id, 'tr_start_date_hour', true );
    $startMinute = get_post_meta( $id, 'tr_start_date_minute', true );
    $startTime = "";

    if($startDate){
        $startTime = date('Y-m-d H:i:s', strtotime($startDate." ".$startHour.":".$startMinute.":00"));
    }


    $endDate = get_post_meta( $id, 'tr_end_date', true );
    $endHour = get_post_meta( $id, 'tr_end_date_hour', true );
    $endMinute = get_post_meta( $id, 'tr_end_date_minute', true );

    $endTime = "";

    if($endDate){
        $endTime = date('Y-m-d H:i:s', strtotime($endDate." ".$endHour.":".$endMinute.":00"));
    }

    $currentTime = date('Y-m-d H:i:s');
    if(!$startTime || ($startTime && $startTime < $currentTime)){
        if(!$endTime || ($endTime && $endTime > $currentTime)){    
            $post = get_post($id);
            ob_start();
            echo apply_filters( 'the_content', $post->post_content );
            $output = ob_get_contents();
            ob_end_clean();
        }
    }
    return $output;
}

Code file location:

static-block/static-block/static-block.php

Static Block [static_block_thumbnail] Shortcode

The ‘static_block_thumbnail’ shortcode is designed to display the full-sized thumbnail of a specific post. The shortcode takes the ‘id’ attribute to identify the post. It retrieves the start and end times of the post if set. If the current time falls within this range, the post’s thumbnail is displayed.

Shortcode: [static_block_thumbnail]

Parameters

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

  • id – The unique ID of the specific post or page.

Examples and Usage

Basic example – A simple usage of the shortcode to display the thumbnail of a static block by referencing the ID.

[static_block_thumbnail id=1 /]

Advanced examples

Using the shortcode to display the thumbnail of a static block by referencing the ID, along with a start and end time. The thumbnail will be displayed if the current time falls within the start and end time.

[static_block_thumbnail id=1 tr_start_date="2022-01-01" tr_start_date_hour="00" tr_start_date_minute="00" tr_end_date="2022-12-31" tr_end_date_hour="23" tr_end_date_minute="59" /]

Please note that the date format should be ‘YYYY-MM-DD’ and the time format should be in 24 hours format. The ‘tr_start_date_hour’ and ‘tr_start_date_minute’ parameters specify the start time while the ‘tr_end_date_hour’ and ‘tr_end_date_minute’ parameters specify the end time.

PHP Function Code

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

Shortcode line:

add_shortcode('static_block_thumbnail','static_block_thumbnail');

Shortcode PHP function:

function static_block_thumbnail($atts, $content=NULL){
    $atts = shortcode_atts( array(
        'id' => ''
    ), $atts, 'static_block_thumbnail' );
    $id = $atts['id'];
    $startDate = get_post_meta( $id, 'tr_start_date', true );
    $startHour = get_post_meta( $id, 'tr_start_date_hour', true );
    $startMinute = get_post_meta( $id, 'tr_start_date_minute', true );
    $startTime = "";

    if($startDate){
        $startTime = date('Y-m-d H:i:s', strtotime($startDate." ".$startHour.":".$startMinute.":00"));
    }


    $endDate = get_post_meta( $id, 'tr_end_date', true );
    $endHour = get_post_meta( $id, 'tr_end_date_hour', true );
    $endMinute = get_post_meta( $id, 'tr_end_date_minute', true );

    $endTime = "";

    if($endDate){
        $endTime = date('Y-m-d H:i:s', strtotime($endDate." ".$endHour.":".$endMinute.":00"));
    }

    $currentTime = date('Y-m-d H:i:s');
    $currentTime = date('Y-m-d H:i:s');
    if(!$startTime || ($startTime && $startTime < $currentTime)){
        if(!$endTime || ($endTime && $endTime > $currentTime)){
            ob_start();
            echo get_the_post_thumbnail( $id, 'full' );
            $output = ob_get_contents();
            ob_end_clean();
        }
    }
    return $output;
}

Code file location:

static-block/static-block/static-block.php

Conclusion

Now that you’ve learned how to embed the Static Block Plugin shortcodes, 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 *