I'm trying to get the current users ID and pass it as a variable into a javascript function (see "USER_ID_GOES_HERE"). Whats the best practice for doing so?
here is my function:
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 });
});
}
Put this in a PHP file, such as your theme's functions.php
. I prefer to put it in a custom plugin.
function headcheese() { ?>
<script type=”text/JavaScript”>
var current_user_id = '<?php echo get_current_user_id(); ?>';
</script>
<?php
}
add_action('wp_head', 'headcheese');
This puts the current user ID in a JavaScript global variable. So you can then use it in your filters
array.
Change this line:
filters: [{"property_name":"author","operator":"eq","property_value":'USER_ID_GOES_HERE'}]
to
filters: [{"property_name":"author","operator":"eq","property_value":<?=get_current_user_id() ?>}]
Note: that this isn't secure for some applications, double check the user id server side.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With