• Hy,
    I have added a shortcode to functions.php for inserting javascript only to logged in users. The shortcode is working, only the javascript fails.
    I want to do readonly specific input fields (name=your_name and name=your_email) when a user is logged in.

    I have made this:

    [member]
    <script type="text/javascript">
    $('input[name$="your-name"]').attr("readonly", "readonly");
    $('input[name$="your-email"]').attr("readonly", "readonly");
    </script>
    [/member]

    But it doesnt works :(.
    Where I am wrong? What could be the solution?
    Thanks forward for answers!

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Your script is more than javascript, it is jQuery. The jQuery library is also required. WP uses one, but it is not normally loaded by default on the front end. Thus, even though it’s not quite proper, you could probably load the library through a “src” reference, either from the WP folders or googleapis.com

    If you use the WP version, it runs in noConflict mode so you need to use a noConflict wrapper or explicitly reference jQuery instead of using the $ shortcut.

    The proper way is to use wp_enqueue_script('jquery'); in a callback for the ‘wp_enqueue_scripts’ action. The problem with doing it this way is the library is loaded on every page load unless you can come up with some conditional that determines on which page the library is actually needed.

Viewing 1 replies (of 1 total)
  • The topic ‘Conditional javascript in a post’ is closed to new replies.