在functions.php中的wordpress javascript错误

时间:2019-01-30 14:19:09

标签: javascript php wordpress

我正在尝试使用wp发票和自定义预订页面创建结帐页面。 这是我放入functions.php

中的代码
    function paypalpayment() {
    global $wpdb;
    $user = wp_get_current_user();
    $user_ID = $user->ID;
    $shortcode = $wpdb->get_row("SELECT MAX(ap.pending) AS pending, ap.book_datetime, ap.id, ap.hash FROM ea_appointments AS ap "
            ."INNER JOIN ea_users AS us ON ap.id_users_customer = us.id "
            ."WHERE us.wp_id ='".$user_ID."'");
    $html = '';

    if ($shortcode->pending == ''){
        $html .= '<h1>Processing Error: Appointment has been deleted. </h1>';
        $html .= '<p align="center"><a class="fep-button" style="width: 195px; text-align: center;" href="http://lectiotutoring.co.za/EasyBlue" /> Schedule an appointment</a></p>';
    } else {
        $html .= '<h2>Fee Policy</h2>';
        $html .= '<p>You may reschedule an appointment without charge within 24 hours of your appointment.  Cancelation of an appointment within 24 hours can either result in a refund or a credit for your next appointment per your request. You will need to inform Lectio Tutoring on the discussion board about how you would like your cancelation to be handled. If you would like a refund, refunds will be the full amount of the cost of your session minus the PayPal processing fees. There are no refunds for cancelations later than 24 hours in advance. <span class="bigger"><b>If payment is not completed within 10 minutes the appointment will be deleted.</b></span></p>';
        date_default_timezone_set('Africa/Johannesburg');
        $refreshtime = strtotime($shortcode->book_datetime) - strtotime("-10 minutes");

        $html .= '<meta http-equiv="refresh" content="';
        $html .=   $refreshtime;
        $html .=  '">';

        $html .= '<style>
            ul.wpi_checkout_block.wpi_checkout_billing_address {
                display: none;
            } 
            ul.wpi_checkout_block.wpi_checkout_customer_information {
                display: none;
            }   
            ul.wpi_checkout_block.wpi_checkout_billing_information {
                display: none;
            }
            .wpi_checkout_submit_btn.btn.btn-success.wpi_checkout_process_payment.wpi_paypal {
                margin: -1px;
            }
            input {
                margin-top: 10px;
                width: 130px;
            }
            form.wpi_checkout .total_price {
                top: 1px;
            }
            .loader {
              border: 4px solid #f3f3f3;
              border-radius: 50%;
              border-top: 4px solid #3498db;
              width: 30px;
              height: 30px;
              -webkit-animation: spin 2s linear infinite; /* Safari */
              animation: spin 2s linear infinite;
            }

            /* Safari */
            @-webkit-keyframes spin {
              0% { -webkit-transform: rotate(0deg); }
              100% { -webkit-transform: rotate(360deg); }
            }

            @keyframes spin {
              0% { transform: rotate(0deg); }
              100% { transform: rotate(360deg); }
            } 

            div#spinner {
                margin-left: 160px;
                position: absolute;
            }

            input#stepone {
                position: absolute;
                margin-top: -2px;
                padding: 0px;
            }

            .bigger { 
                font-size:125%; 
            }
        </style>';

        $html .= '<input type="button" id="stepone" onclick="processpaypal()" value="Process Payment">';
        $html .= '<div id="spinner" class="loader" style="display:none"></div>';
        $html .= do_shortcode($shortcode->pending);
        $html .= '<input type="button" onclick="deletapt()" value="Delete Apt.">';
        $html .= '<script>
            cancelurl = "http://lectiotutoring.co.za/EasyBlue/index.php/appointments/cancel/' . $shortcode->hash . '";
            function deletapt(){
                window.location = cancelurl;
            }
            jQuery(document).ready(function($) {
                $("input[name=return]").val("http://lectiotutoring.co.za/payment-success/");
                $("input[name=cancel_return]").val(cancelurl);
            });  
            jQuery(document).ready(function($) {
            function processpaypal($){
                $("#spinner").css("display","block");
                $("#stepone").css("display","none");
                setTimeout(
                function() 
                {
                    $(".wpi_checkout_submit_btn").click();
                }, 250);                
            }
            });  

        </script>';

    }   
    return $html;
}
add_shortcode("paypalpay", "paypalpayment");

当我在控制台中查找错误时,它会在Uncaught ReferenceError: processpaypal is not defined区域显示$html .= '<div id="spinner" class="loader" style="display:none"></div>';

我已经将我的jquery转换为与wordpress兼容,但是由于如上所述出现错误,它似乎无法正常工作,这是什么问题?我在此区域jQuery(document).ready(function($) { function processpaypal($){

中将我的js错误转换了吗

1 个答案:

答案 0 :(得分:0)

...我想评论还不够

  1. 使您的processpaypal函数成为全局函数,将其移至文档之外。了解有关范围here
  2. 的更多信息
  3. 要解决wordpress中的$冲突,请执行此操作。

a。

   var $ = jQuery; // make sure to make it global

b。

   //using jQuery instead of $ like so, (like you did it here jQuery(document))
   function processpaypal(){
            jQuery("#spinner").css("display","block");
            jQuery("#stepone").css("display","none");
            setTimeout(
            function() 
            {
                jQuery(".wpi_checkout_submit_btn").click();
            }, 250);                
     }

也请考虑以下建议:

  1. 最好使用单独的css和js文件,而不是内联。
  2. 使用您的html输出创建单独的模板文件,并使用函数中的输出buffering包含它
  3. date_default_timezone_set('Africa/Johannesburg');,您可以将其放在functions.php的开头,此功能将为整个文件设置时区全局。 reference