【问题标题】:Can't figure out this T_Variable error无法弄清楚这个 T_Variable 错误
【发布时间】:2011-07-17 22:00:05
【问题描述】:

我正在尝试激活自制的 wordpress 插件,但在下面的这一行中出现 T_Variable 错误。

type: "POST", url: "<?php$pluginDirectory = dirname(plugin_basename(__FILE__));?>/shoutbox.php", data: "action=insert&nick=" + nick + "&message=" + message,
                complete: function(data){

我无法弄清楚,因为我在前几行的另一个 ajax 调用中使用了完全相同的 php,但它不会触发插件激活错误。此代码不会触发错误。有人可以帮忙吗?

$.ajax({
            type: "POST", url: "<?php$pluginDirectory = dirname(plugin_basename(__FILE__));?>/shoutbox.php", data: "action=update",
            complete: function(data){
                loading.fadeOut();
                messageList.html(data.responseText);
                messageList.fadeIn(2000);
            }
        });
    }

触发错误的代码

/*
Plugin Name: Shoutbox plugin
Plugin URI: http://www.blahblha.com/aboutmyplugin
Description: Shoutbox plugin
Author: Me!
Version: 1.0
Author URI: http://www.blahblah.com
*/


function my_function {  ?>

$(document).ready(function(){
    //global vars
    var inputUser = $("#nick");
    var inputMessage = $("#message");
    var loading = $("#loading");
    var messageList = $(".content > ul");

//functions
function updateShoutbox(){
    //just for the fade effect
    messageList.hide();
    loading.fadeIn();
    //send the post to shoutbox.php
    $.ajax({
        type: "POST", url: "<?php echo $pluginDirectory = dirname(plugin_basename(__FILE__));?>/shoutbox.php", data: "action=update",
        complete: function(data){
            loading.fadeOut();
            messageList.html(data.responseText);
            messageList.fadeIn(2000);
        }
    });
}
//check if all fields are filled
function checkForm(){
    if(inputUser.attr("value") && inputMessage.attr("value"))
        return true;
    else
        return false;
}

//Load for the first time the shoutbox data
updateShoutbox();

//on submit event
$("#form").submit(function(){
    if(checkForm()){
        var nick = inputUser.attr("value");
        var message = inputMessage.attr("value");
        //we deactivate submit button while sending
        $("#send").attr({ disabled:true, value:"Sending..." });
        $("#send").blur();
        //send the post to shoutbox.php
        $.ajax({
            type: "POST", url: "<?php echo $pluginDirectory = dirname(plugin_basename(__FILE__));?>/shoutbox.php", data: "action=insert&nick=" + nick + "&message=" + message,
            complete: function(data){
                messageList.html(data.responseText);
                updateShoutbox();
                //reactivate the send button
                $("#send").attr({ disabled:false, value:"Shout it!" });
            }
         });
    }
    else alert("Please fill all fields!");
    //we prevent the refresh of the page after submitting the form
    return false;
});
});

<?php

}   //this bracket is creating the same problem....

add_action('wp_head', 'my_function');

【问题讨论】:

  • @OP: /offtopic: 你希望type: "POST", url: "&lt;?php$pluginDirectory = dirname(plugin_basename(__FILE__));?&gt; 做什么,因为它没有回应任何东西。

标签: php javascript wordpress


【解决方案1】:

&lt;?php 之后和$pluginDirectory 之前需要一个空格

【讨论】:

  • 不幸的是,这并不能解决问题。
  • 它修复了错误,但仍然没有输出任何内容。你可能只是想把&lt;?php echo dirname(plugin_basename(__FILE__)); ?&gt;
  • 好吧,对不起,你是对的。谢谢。但是,函数的右括号(就在我 add_action 之前)正在触发相同类型的错误。我用相关代码更新了 OP。如果你有想法,请告诉我。再次感谢您的帮助。
  • 在您向我们展示的第一行之前,您是否有一个开头的 &lt;?php 标签 (function my_function {)。您还应该将该行更改为function my_function(){
  • 好的,我添加了开始的 php 标签。 T_Errors 消失了。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多