Title: Conditional Hyperlinks
Last modified: October 1, 2022

---

# Conditional Hyperlinks

 *  Resolved [sujanm](https://wordpress.org/support/users/sujanm/)
 * (@sujanm)
 * [3 years, 8 months ago](https://wordpress.org/support/topic/conditional-hyperlinks/)
 * Hi Peter,
 * I’m trying to conditionally display a hyperlink. Something like what you had 
   suggested here:
 * [https://wordpress.org/support/topic/can-a-hyperlink-column-not-display-a-link-until-it-gets-one-assigned/](https://wordpress.org/support/topic/can-a-hyperlink-column-not-display-a-link-until-it-gets-one-assigned/)
 * the dynamic hyperlink is set as follows
 *     ```
       <script>
         if (myCondition) {
            document.write("");
         }
       </script>
       ```
   
 * However the script tag doesn’t get interpreted as html. Element in the browser
   is
 *     ```
       <td class=" wpda_hyperlink_2 wpda_format_string">&lt;script&gt;  if (myCondition) {  document.write("");  } &lt;/script&gt;</td>
       ```
   
 * What am I doing wrong?

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Plugin Author [Passionate Programmer Peter](https://wordpress.org/support/users/peterschulznl/)
 * (@peterschulznl)
 * [3 years, 8 months ago](https://wordpress.org/support/topic/conditional-hyperlinks/#post-16062166)
 * Hi [@sujanm](https://wordpress.org/support/users/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](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](https://wordpress.org/support/users/sujanm/)
 * (@sujanm)
 * [3 years, 8 months ago](https://wordpress.org/support/topic/conditional-hyperlinks/#post-16062326)
 * 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!
 *  Plugin Author [Passionate Programmer Peter](https://wordpress.org/support/users/peterschulznl/)
 * (@peterschulznl)
 * [3 years, 8 months ago](https://wordpress.org/support/topic/conditional-hyperlinks/#post-16065184)
 * Hi [@sujanm](https://wordpress.org/support/users/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](https://wordpress.org/support/users/sujanm/)
 * (@sujanm)
 * [3 years, 8 months ago](https://wordpress.org/support/topic/conditional-hyperlinks/#post-16065805)
 * 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?
 *  Plugin Author [Passionate Programmer Peter](https://wordpress.org/support/users/peterschulznl/)
 * (@peterschulznl)
 * [3 years, 8 months ago](https://wordpress.org/support/topic/conditional-hyperlinks/#post-16068083)
 * Hi [@sujanm](https://wordpress.org/support/users/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](https://wordpress.org/support/users/sujanm/)
 * (@sujanm)
 * [3 years, 8 months ago](https://wordpress.org/support/topic/conditional-hyperlinks/#post-16068467)
 * 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?
 *  Plugin Author [Passionate Programmer Peter](https://wordpress.org/support/users/peterschulznl/)
 * (@peterschulznl)
 * [3 years, 8 months ago](https://wordpress.org/support/topic/conditional-hyperlinks/#post-16068723)
 * Hi [@sujanm](https://wordpress.org/support/users/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](https://wordpress.org/support/users/sujanm/)
 * (@sujanm)
 * [3 years, 8 months ago](https://wordpress.org/support/topic/conditional-hyperlinks/#post-16068870)
 * 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](https://wordpress.org/support/users/sujanm/).
 *  Plugin Author [Passionate Programmer Peter](https://wordpress.org/support/users/peterschulznl/)
 * (@peterschulznl)
 * [3 years, 8 months ago](https://wordpress.org/support/topic/conditional-hyperlinks/#post-16068895)
 * My pleasure! 😊

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Conditional Hyperlinks’ is closed to new replies.

 * ![](https://ps.w.org/wp-data-access/assets/icon-256x256.png?rev=3299063)
 * [WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards](https://wordpress.org/plugins/wp-data-access/)
 * [Support Threads](https://wordpress.org/support/plugin/wp-data-access/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-data-access/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-data-access/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-data-access/reviews/)

 * 9 replies
 * 2 participants
 * Last reply from: [Passionate Programmer Peter](https://wordpress.org/support/users/peterschulznl/)
 * Last activity: [3 years, 8 months ago](https://wordpress.org/support/topic/conditional-hyperlinks/#post-16068895)
 * Status: resolved