• Resolved generosus

    (@generosus)


    Good Day,

    Unfortunately, your plugin does not work with shortcodes (not ours, at least). We read all support topics in this forum related to shortcodes and applied all of your recommended solutions. None of them work.

    This particular support topic describes exactly what we’re running into.

    So, per your topic request, we would like to see if you can make your plugin work for the exercise given below.

    Our environment: PHP 8.3.8, jQuery 3.7.1, Avada 7.11.9, WP 6.5.5

    Our caching mechanisms: Speed Optimizer (plugin) and Cloudflare.

    Fix appreciated. Your plugin has a lot of potential.

    Thank you!

    —————-

    Exercise:

    1. Add the code snippet provided below to your functions.php file (we’re using the plugin Code Snippets for this purpose). The code snippet generates the shortcode [business_hours]. Note: We used two versions of the code snippet provided below — one with and the other without the conditional if (is_page ('contact'))
    2. Add the shortcode [business_hours] to your “Edit non-cachable content” post (details).
    3. Using the shortcode generated by your plugin [content_no_cache id=”nnnnnn”], add it to your site content (details).
    4. Results: https://prnt.sc/Ep71Azj7RE1g

    ———————-

    Code Snippet for Shortcode [business_hours]:

    function business_hours_shortcode($atts) {
    if (is_page ('contact')) {
    // Set timezone - change this to match your own.
    date_default_timezone_set('America/Chicago');

    // An array of your opening hours.
    $opening_hours = array(
    'Monday' => array('07:30', '17:30'),
    'Tuesday' => array('07:30', '17:30'),
    'Wednesday' => array('07:30', '17:30'),
    'Thursday' => array('07:30', '17:30'),
    'Friday' => array('07:30', '17:30'),
    'Saturday' => array('Closed'),
    'Sunday' => array('Closed'),
    );

    // An array of your holidays (format: month/day - add zero before single-digit month or day)
    $holidays = array('01/01', '05/26', '07/04', '07/05', '09/02', '11/11', '11/28', '11/29', '12/24', '12/25', '12/31');

    // Get current date, day, and time.
    $current_date = date('m/d');
    $current_day = date('l');
    $current_time = date('H:i');

    // Get today's opening hours.
    $today_hours = $opening_hours[$current_day];

    // Check if today is a holiday.
    if (in_array($current_date,$holidays)) {
    return '<span style="font-weight:600;color:#ffffff;background-color:#e14d43;padding:6px 12px;border-radius:3px;">CLOSED FOR THE HOLIDAY</span>';
    }

    // Check if we have opening hours for today.
    if (!isset($today_hours) || count($today_hours) < 2) {
    return '<span style="font-weight:600;color:#ffffff;background-color:#e14d43;padding:6px 12px;border-radius:3px;">CLOSED TODAY</span>';
    }

    // Check if current time is between opening hours.
    if ($current_time >= $today_hours[0] && $current_time <= $today_hours[1]) {
    return '<span style="font-weight:600;color:#ffffff;background-color:#00c853;padding:6px 12px;border-radius:3px;">CURRENTLY OPEN</span>';
    } else {
    return '<span style="font-weight:600;color:#ffffff;background-color:#e14d43;padding:6px 12px;border-radius:3px;">CURRENTLY CLOSED</span>';
    }
    }
    }
    add_shortcode('business_hours', 'business_hours_shortcode');
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Jose

    (@giuse)

    Hi @generosus

    thank you for providing the code, this is a big help.

    It will never work until you have this condition in the function of your shortcode:

    if (is_page ('contact')) {
    //your shortcode code here...
    }

    during the Ajax request is_page( ‘contact’ ) will return false because during the Ajax request you have no contact page.

    Replace that condition with the following one:

    if ( is_page ('contact') || ( isset( $_POST['request'] ) && 'content' === sanitize_text_field( $_POST['request'] ) ) ) {

    //your shortcode code here...

    }

    I reproduced exactly your steps, and it works for me with the new condition.

    In your case you have also another issue. Looking at your screenshot I can say It will not work also after replacing the condition.

    I suspect you added the shortcode [business_hours] in the Visual field instead of the Text field.
    I suggest you edit again the Content No Cache element, and then click on Text.
    Be sure you have nothing around between “[” and business_hours, and nothing between business_hours and “]”.
    After clicking on Text you need to see a clean [business_hours] without any markup.

    In the next version I will remove the Visual editor.

    Please, let me know if you still have issues.

    Have a great day!
    Jose

    Thread Starter generosus

    (@generosus)

    Hi Jose,

    You’re a genius. That fixed it (results).

    After thorough testing, we learned the following (recommendations for your plugin):

    1. Your plugin works even if we insert our shortcode in the visual editor. So, no need to remove the visual editor from your plugin. Leave it in, it’s helpful.
    2. Our code snippet was originally set to run only on the front end. With your adjustments, we had to change our code snippet setting to run everywhere (details).
    3. The front end position of your shortcode (i.e., no cache content) can be easily adjusted using CSS as noted here.

    Other Recommendations:

    1. Update your knowledge base (website info) to include all of the valuable information contained in this forum.
    2. Add to your list: Plugin is compatible with Speed Optimizer and Cloudflare.
    3. Incorporate the recommendations noted here. (much better)

    We will definitely promote your plugin (free and pro versions). Upgrading soon.

    Muchas gracias, Jose!

    Plugin Author Jose

    (@giuse)

    Hi @generosus

    thank you very much for your precious advice! This is very helpful.

    Have a great day!

    Jose

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Does Not Work With Shortcodes’ is closed to new replies.