Hi @sujanm,
You are correct, the script tag is disabled in data tables for security reasons. Sorry! I totally forgot.
But you can achieve this with a macro. Macro’s were not available when the post was written you are referring to. They are rather new and added to improve security.
Macro’s are explained here:
https://wpdataaccess.com/docs/data-explorer/dynamic-hyperlinks/#macro-if-then-else
It is basically the same you are doing with javascript, just more secure. Please let me know if this helps.
Best regards,
Peter
Thread Starter
sujanm
(@sujanm)
Hi Peter,
Thanks, yes I’m aware of Macros and I use them to compare the state of a column value to determine if a hyperlink should be displayed.
Will macros be able to handle it based upon the value of a javascript variable on the page? That is essentially what I would need. I’ve read the link on Macros and it doesn’t give information on how the conditions of the macro are parsed.
Thanks again for the help!
Hi @sujanm,
Sorry, I’m not sure if I understand your question correctly… Macros are executed on the server when the page is rendered. They have no effect in the browser and you cannot embed them in javascript.
Does this answer your question? Or can you explain what you want to achieve?
Thanks,
Peter
Thread Starter
sujanm
(@sujanm)
Hi Peter,
Sorry for not being clearer. What I’m trying to achieve is to render or not render a dynamic hyperlink based upon some kind of global state (eg. only display a link if the user is logged on). I could do that in the example above if “myCondition” is a boolean javascript variable as I can set its value through PHP. If I was simply using PHP to render a page I can conditionally render an element on the page based upon a test of a PHP variable or condition. I’m not seeing how I can do that with macros.
Eg. Can I do something like this
#macro if is_user_logged_in()
or
#macro if $myPHPVariable
or alternatively
#macro if myJavascriptVariable
Is that clearer?
Hi @sujanm,
I should have asked! π
WordPress adds the logged-in class to the html body when the user is logged in. This allows you to easily test in javascript:
if (jQuery("body").hasClass("logged-in")) {
// This code is only executed when the user is logged in...
}
Does this help?
Thanks,
Peter
Thread Starter
sujanm
(@sujanm)
Thanks Peter, but I’m missing something. How would the javascript above be run if the script tags have been disabled in the dynamic hyperlink?
You have suggested using Macros, but can you post what the macro would look like if it included the above code when rendering the dynamic hyperlink?
Hi @sujanm,
The macro cannot test if the user is logged in. Sorry! It does not process php code. It is just a simple macro that substitues column values under specific circumstances.
Here is a possible solution (there are probably more)…
Just create your hyperlink and hide it by setting the style to display:none. Add a class with a specific name. Then execute a javascript function after page load that deletes the hyperlinks when the user is not logged in and displays them when the user is logged in.
Makes sure to check if the user is logged in on the url of the hyperlink. Just in case someone follows the link withi=out being logged in.
Does this make sense?
Best regards,
Peter
Thread Starter
sujanm
(@sujanm)
Hi Peter, yes that makes perfect sense. I will likely do something like you suggested. Thanks again for the help.
-
This reply was modified 3 years, 8 months ago by
sujanm.