Custom plugin not compatible with SG Optimizer

[ad_1]

Hi,

I created a custom plugin with the following code, but the times that I am outputting through PHP shortcode (which calls a Javascript function in a .js file) are not being displayed at the proper place where they are supposed to show up. Instead they are displayed at the end of the page (below the footer). This is happening due to SG Optimizer. I tested by disabling it’s options like “Defer Render-blocking JavaScript”, but with Defer Render-blocking JavaScript enabled, the times doesn’t even show up even at the end of the page.

Could you please help me out on how to make my custom plugin compatible with SG Optimizer with it’s options enabled?

My plugin has the following coed

<?php

/*
Plugin Name:  JKY-Custom-Hooks
Version    :  1.0
Description:  Demonstrating WordPress Hooks (Actions and Filters) with multiple examples.
Author     :  Sam
Author URI :  
License    :  GPLv2 or later
License URI:  
Text Domain:  my-custom-hooks
*/
    
    /**
     * Proper way to enqueue scripts and styles.
     */
    function wpdocs_moment_js_scripts() {
        wp_enqueue_script( 'moment-js', plugins_url('/assets/js/moment-js/moment.min.js', __FILE__ ), array(), '1.0.0', false);
        wp_enqueue_script( 'moment-timezone', plugins_url('/assets/js/moment-js/moment-timezone.min.js', __FILE__ ), array(), '1.0.0', false);
        wp_enqueue_script( 'moment-timezone-data', plugins_url('/assets/js/moment-js/moment-timezone-with-data.min.js', __FILE__ ), array(), '1.0.0', false);
        wp_enqueue_script( 'moment-timezone-tenyears', plugins_url('/assets/js/moment-js/moment-timezone-with-data-10-year-range.min.js', __FILE__ ), array(), '1.0.0', false);
        wp_enqueue_script( 'moment-timezone-nineteenseventy', plugins_url('/assets/js/moment-js/moment-timezone-with-data-1970-2030.min.js', __FILE__ ), array(), '1.0.0', false);
    }    
add_action( 'wp_enqueue_scripts', 'wpdocs_moment_js_scripts' );

add_action( 'wp_enqueue_scripts', 'wp_custom_plugin_js_scripts' );
function wp_custom_plugin_js_scripts() {
    //wp_register_script( 'my-custom-hooks', plugins_url( '/assets/js/custom-date-scripts.js' , __FILE__ ),array('jquery'),1.0);
    wp_enqueue_script('custom-date-scripts', plugins_url( '/assets/js/custom-date-scripts.js' , __FILE__ ));
}
 
function wpb_hook_get_client_timezonetime($attr) {
    $input_arr = shortcode_atts( array(
					'input_time' => '5:00 PM',
                    'input_timezone' => 'CST'
				), $attr);
    //wp_enqueue_script( 'my-custom-hooks' );
    return "<script type="text/javascript">get_client_time( '". $input_arr['input_time'] ."', '". $input_arr['input_timezone'] ."' );</script>";
}
add_shortcode('get_client_timezonetime', 'wpb_hook_get_client_timezonetime');

//=================================================
// Security: Abort if this file is called directly
//=================================================
if ( !defined('ABSPATH') ) { 
    die;
}

?>

My .js file has the following code:


	function get_client_time($input_time, $input_timezone) {
 
            var dt = moment($input_time, ["h:mm A"]).format("HH:mm");               
            var splitTime = dt.split(/:/)
            var full_date = moment(new Date().setHours(splitTime[0], splitTime[1], 0)).tz("America/Chicago", true);
                
		var dateTime = moment(full_date).format("YYYY-MM-DD h:mm:ss a");
             
            var local_dateTime = moment(full_date).tz(moment.tz.guess()).format("YYYY-MM-DD h:mm:ss a");
            var local_time = moment(full_date).tz(moment.tz.guess()).format("h:mm a z");
            console.log("local date time: " + local_dateTime);
                console.log($input_time);
                console.log($input_timezone);
		var result = "<span style="color: #DA4505; font-size: 16px; font-weight: 600; font-style: italic; display: block; text-align: center">" + local_time + "</span>";
                document.write(result);
                //return result;
            }

Please help me in making this work.

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

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer