Use ajax from function.php

As Milo already said in comment, jQuery is in noConflict mode in WordPress. This way other JS libraries can also use $ character… One way to solve it is using jQuery everywhere in your JS files. Another is to create block and define $ variable in it: jQuery(function ($) { $.ajax(…); // here you can … Read more

Enqueuing javascript files

Example for themes I would recommend using wp_enqueue_script with get_theme_file_uri this allows child themes to overwrite this file. get_theme_file_uri() function Searches in the stylesheet directory before the template directory so themes which inherit from a parent theme can just override one file. wp_enqueue_script( ‘example-script’, get_theme_file_uri( ‘/js/example-script.js’ ), array(‘jquery’), null, true ); Example for plugins wp_enqueue_script( … Read more

jQuery Ajax passing empty parameters to my function?

The values are not passed as parameters, but passed in the $_POST array. You need to add nonce for security using check_ajax_nonce https://developer.wordpress.org/reference/functions/check_ajax_referer/ You need to sanitize values submitted before inserting into database Here’s how this should be done: add_action( ‘wp_ajax_send_projectmessage’, ‘send_projectmessage’ ); function send_projectmessage() { global $wpdb; check_ajax_referer( ‘send_projectmessage’, ‘send_projectmessage_nonce’ ); $projectid = sanitize_text_field( … Read more

How to add WP API and JS featured image attachment

I manage to solved it by adding this code (at the bottom) to the YOUR BLOG THEMES my path : wp-content/themes/twentysixteen/function.php: function ws_register_images_field() { register_rest_field( ‘post’, ‘images’, array( ‘get_callback’ => ‘ws_get_images_urls’, ‘update_callback’ => null, ‘schema’ => null, ) ); } add_action( ‘rest_api_init’, ‘ws_register_images_field’ ); function ws_get_images_urls( $object, $field_name, $request ) { $medium = wp_get_attachment_image_src( get_post_thumbnail_id( … Read more

Sending jsPDF documents to the server

Keep in mind not everyone is running JavaScript, so you really need a server-side form processor like PHP rather than relying solely on jQuery. Best thing to do on a WordPress site is create a plugin that processes your form. You can include helper libraries like FPDF to not only generate the PDFs but that … Read more

Asking popup for delete post in WordPress [closed]

Yes, it’s possible. You have to create a JavaScript File and enqueue this file for admin use. This hook is to add files for the backend: add_action( ‘admin_enqueue_scripts’, ‘your_function_name’ ); This hook is to add files for the frontend: add_action( ‘wp_enqueue_scripts’, ‘your_function_name’ ); So we need to use the admin_enqueue_scripts hook and create our function: … Read more

Stop theme’s jQuery function from running

We need to know how the script is being enqueued, but if it’s done right, you will be able to remove that .js file using wp_dequeue_script. How to correctly enqueue scripts function my_register_script() //Registers a script to be enqueued later wp_register_script(‘script-handle’, plugins_url(‘script.js’, __FILE__)); } add_action( ‘wp_enqueue_scripts’, ‘my_register_script’, 10); function my_enqueue_script(){ //Enqueue the script wp_enqueue_script(‘script-handle’); } … Read more

Create URL with parameter for JQuery toggle status

You need to process 2 scenarios: When page is loaded with #espanol hash When button is clicked //check for #espanol hash and toggle on page load spanishByHash(); //process toggle on click event $(‘.js-spanish-toggle’).on(‘click’, function() { hashToggle(); spanishToggle(); }); function spanishToggle() { $(‘.js-spanish-toggle’) .find(‘button’) .toggleClass(‘active’); $(‘.js-spanish-toggle’) .parents(‘.spanish-container’) .toggleClass(‘is-spanish’); } function hashToggle(){ if($(location).prop(‘hash’) === ‘#espanol’){ $(location).prop(‘hash’,”); }else{ … Read more

combining js scripts from a js newb [closed]

The jQuery(document).ready event is called when the document DOM is ready to be manipulated. In earlier days we attached all our handlers to document.load, but this waits until all images are loaded too, and most of the time one doesn’t need that. There are three equivalent ways of hooking into this event, and a fourth … Read more