我正在尝试获取当前用户 ID 并将其作为变量传递给 javascript 函数(请参阅“USER_ID_GOES_HERE”)。这样做的最佳做法是什么?
这是我的功能:
function hide_loading() {
Wild.onChartsReady(function() {
var series = new Wild.Series("Viewed Post", {
analysisType: "count",
timeframe: "this_week",
interval: "daily",
groupBy: "title",
filters: [{"property_name":"author","operator":"eq","property_value":'USER_ID_GOES_HERE'}]
});
series.draw(document.getElementById("mine"), { lineWidth: 2 });
});
}
最佳答案
把它放在一个 PHP 文件中,比如你的主题的 functions.php
.我更喜欢把它放在一个自定义插件中。
function headcheese() { ?>
<script type=”text/JavaScript”>
var current_user_id = '<?php echo get_current_user_id(); ?>';
</script>
<?php
}
add_action('wp_head', 'headcheese');
这会将当前用户 ID 放入 JavaScript 全局变量中。所以你可以在你的
filters
中使用它大批。
关于javascript - 如何在wordpress的Javascript函数中传递用户ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23399661/