wp_localize_script with boolean and init
I just do something like this so long : wp_add_inline_script(‘helloworld’,’var site_config =’.json_encode($site_config));
I just do something like this so long : wp_add_inline_script(‘helloworld’,’var site_config =’.json_encode($site_config));
There are two ways you could do that. 1. Using JS only WordPress themes normally use the body_class() function. As a result you’ll see that the <body> tag will have lots of classes. You can then target pages with a specific class to run your code in JavaScript: if( $(‘body.whateverclass’).length || $(‘body.anotherclass’).length ){ // Your … Read more
Comments processing is done in the file: wp-comments-post.php. You can use the hook pre_comment_on_post to validate the values entered in the comment form fields. function custom_validate_comment_url() { if( !empty( $_POST[‘url’] ) && !preg_match( ‘\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]’, $_POST[‘url’] ) // do you url validation here (I am not a regex expert) wp_die( __(‘Error: please enter a valid url … Read more
Each of the files you have mentioned are theme files. When you upgrade WordPress itself, it will not touch any themes (or plugins) you have installed, whether or not you have modified them. If this was a theme you created yourself, then you have no worries at all. However, if this was a theme created … Read more
First of all, you shouldn’t call your own PHP file. You should use admin-ajax endpoint. Documentation On admin side, you could use ajaxurl to get URL for AJAX calls. On front side you have to declare by yourself variable with url. This is written in documentation: Note: Unlike on the admin side, the ajaxurl javascript … Read more
Google Chart API Free service, that generates chart images (hosted and served by Google) from data in URL requests. If you need something very specific and of fixed parameters it is easy to hardcode most of it and get away without any library (and licensing issues) at all. Disadvantages: not suitable for sensitive data (passed … Read more
Use wp_localize_script() to pass any kind of data to your loaded scripts, in this case we need plugins_url(): wp_enqueue_script(‘my-script’, get_stylesheet_directory_uri() . ‘/js/my-script.js’); wp_localize_script(‘my-script’, ‘myScript’, array( ‘pluginsUrl’ => plugins_url(), )); Now you will have access to myScript.pluginsUrl in your script file: var href = myScript.pluginsUrl + ‘/path/to/resource’;
In the new WordPress (WP 5+) Editor – Gutenberg, you can use a “Custom HTML” block to prevent the automatic line breaks and paragraphs: on Visual Editor click + > Formatting > Custom HTML – and put your JavaScript in there on Code Editor enclose your JavaScript with <!– wp:html –> <!– /wp:html –>
This was very frustrating to figure out. I spent hours on this issue and discovered your problem is in this input: <input type=”text” name=”name” id=”name” size=”30″ value=””/> Try changing the input field name to anything but “name”, for example: <input type=”text” name=”user_name” id=”name” size=”30″ value=””/>
Use null as $ver param: wp_deregister_script( ‘jquery’ ); wp_register_script( ‘jquery’, ‘https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js’, false, null); wp_enqueue_script(‘jquery’); Output: <script type=”text/javascript” src=”https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js”></script>