1

I'm trying to run a javascript/jquery code inside a PHP function in wordpress. Actually I found this code at this link: http://jsfiddle.net/fXrv2/

As you can see, it works perfectly. But it seems that something is missing in my final code bellow, because the text-field isn't able to auto complete the number format.

Any idea why is it not working?

    <?php

    function teste() {
        ?>

        <script type="text/javascript">
            $('input.number').keyup(function(event) {

                // skip for arrow keys
                if(event.which >= 37 && event.which <= 40){
                    event.preventDefault();
                }

                $(this).val(function(index, value) {
                    return value
                        .replace(/\D/g, "")
                        .replace(/([0-9])([0-9]{2})$/, '$1.$2')
                        .replace(/\B(?=(\d{3})+(?!\d)\.?)/g, ",")
                        ;
                });
            });
        </script>

        <input type="text" class="number">

        <?php
    }
3
  • 3
    seems that something is missing Questions should have a clear problem statement, or no one will know what your issue is. Commented May 24, 2018 at 1:47
  • 1
    PHP is server-side. Javascipt (in this situation) is client side. what are you trying to achieve both sides? Commented May 24, 2018 at 1:54
  • I create a PHP function with a shortcode. And after that I add this shortcode on my website pages. Is it not correct? I'm new on wordpress, sorry! @whitelettersinblankpapers Commented May 24, 2018 at 2:02

1 Answer 1

2

Since that JS is all client-side you're going to have more consistent results including that script completely separate from your PHP. If your theme already has a JavaScript file you can add it to that's great, or you can add it to a separate .js file and enqueue that file using wp_enqueue_scripts().

Whichever way you choose you'll want to invoke it within document.ready() or on window load (see the jQuery docs) to make sure that the input element you're trying to select is available for the JS to find. In the JS fiddle you linked to it's set to use the JS onLoad and that likely makes all the difference over your current inline script.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the answer! I understand your point. But is it right create a webpage on Wordpress with a PHP function and shortcode?
Shortcodes are a good solution whenever you need to make some piece of code available to an end-user, who can then select where some element/code should be place on the site. If that's the case for this feature I'd recommend placing all code within a plugin, and the shortcode should handle only the generation of the input element with the appropriate class name. In your plugin you could then have your JS selecting that class name. It's an appropriate solution if your end user will know where they want that input element to be but not what the appropriate html would be.
Thank you so much for this help! Now I can see it. But when you say "I'd recommend placing all code within a plugin" what kind of plugin are you talking about? I'm creating a website with 2 parts: Presentation pages and members account pages. The presentation webpage is been built only with HTML but on the members account pages I'm using this PHP function with shortcodes. This PHP function has database access and HTML tags. Is there some risks in that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.