{"id":61121,"date":"2021-04-07T11:21:01","date_gmt":"2021-04-07T11:21:01","guid":{"rendered":"https:\/\/www.vwthemes.com\/?p=61121"},"modified":"2021-04-15T04:29:55","modified_gmt":"2021-04-15T04:29:55","slug":"ajax-with-php","status":"publish","type":"post","link":"https:\/\/preview.vwthemesdemo.com\/old_website\/ajax-with-php\/","title":{"rendered":"That\u2019s How You Use AJAX With PHP In Your WordPress Website!"},"content":{"rendered":"\n<p><strong>W<\/strong>hen managing WordPress website, you have to know about different languages that help it function. Even if you don\u2019t have to know about coding for creating website; you must know the languages. The one that helps these languages in creation is AJAX! Before we go to use AJAX with PHP, let\u2019s know more about AJAX first. It is one of the technics of web developers that helps to develop interactive applications. With these applications your website could work smoother and faster. This will allow users to update content in website without reloading it. For working with AJAX you need to know other languages as well.<\/p>\n\n\n\n<p>AJAX basically stands for \u2018Asynchronous JavaScript and XML\u2019. This is used to create interactive and entertaining applications. The most popular example of this is Google Maps. Along with it the autocomplete feature in Google search, comments and likes on social media posts etc.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"367\" height=\"247\" src=\"https:\/\/www.vwthemes.com\/wp-content\/uploads\/2021\/04\/AJAX-with-PHP.png\" alt=\"AJAX with PHP\" class=\"wp-image-61122\" srcset=\"https:\/\/preview.vwthemesdemo.com\/old_website\/wp-content\/uploads\/2021\/04\/AJAX-with-PHP.png 367w, https:\/\/preview.vwthemesdemo.com\/old_website\/wp-content\/uploads\/2021\/04\/AJAX-with-PHP-64x43.png 64w, https:\/\/preview.vwthemesdemo.com\/old_website\/wp-content\/uploads\/2021\/04\/AJAX-with-PHP-300x202.png 300w, https:\/\/preview.vwthemesdemo.com\/old_website\/wp-content\/uploads\/2021\/04\/AJAX-with-PHP-89x60.png 89w, https:\/\/preview.vwthemesdemo.com\/old_website\/wp-content\/uploads\/2021\/04\/AJAX-with-PHP-134x90.png 134w\" sizes=\"auto, (max-width: 367px) 100vw, 367px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-some-things-you-should-know-about-ajax\"><strong>Some Things You Should Know About AJAX<\/strong><\/h2>\n\n\n\n<p>AJAX works with all the web languages. Putting together with JavaScript, HTML, CSS and XML it can create faster web experience. There\u2019s another pair that works with web development along with these languages. AJAX with PHP is the other server side language that works with web development.<\/p>\n\n\n\n<p>It uses JavaScript for the content and CSS for presentation and document object model. It also uses HTML for further process. Usually web applications or web pages exchange information with the server synchronously. Whereas in the web application created using AJAX, when you click a button or fill a form the JavaScript create XMLHTTP request. It send this request to the server as well in XML format.<\/p>\n\n\n\n<p>The server creates a server-side response and sends it to browser. Then JavaScript processes this response. The content on the screen gets updated. This doesn\u2019t involve reloading or refreshing of the website. This way the users won\u2019t be aware of sending information to server.<\/p>\n\n\n\n<p>To work with AJAX you need to have certain skills. As mentioned earlier, to work with AJAX you have to know about languages like JavaScript, HTML and CSS. You need to be proficient in the server-side language like PHP and others. And lastly, you need to know some basics about XML and JSON.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-ajax-in-wordpress\"><strong>AJAX in WordPress<\/strong><\/h2>\n\n\n\n<p>In WordPress AJAX is used at a backend. Due to this when you make any changes into posts, moderate comments or make changes into categories the update will be instantly made. The AJAX is mostly used with JQuery in WordPress. WordPress uses AJAX in definite process!<\/p>\n\n\n\n<p>After making the request it goes through \u2018admin-ajax.php\u2019 file in wp-admin folder. The requests would need a piece of data which is called \u2018action\u2019. It can be processed by using \u2018get\u2019 or \u2018post\u2019. This will prompt the code in \u2018admin-ajax.php\u2019 file to create hooks. The hooks are \u2018wp_ajax_my_action\u2019 and \u2018wp_ajax_nopriv_my_action\u2019. The phrase \u2018my_action\u2019 shows the value of variable \u2018action\u2019 of \u2018get\u2019 and \u2018post\u2019 method.<\/p>\n\n\n\n<p>The first hook is for the actions taken by logged in users. Whereas the second hook is for the logged out users. These hooked actions are for graceful degradation. It will ensure that if the JavaScript is disabled in the browsers the code will still work.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-a-wordpress-ajax-plugin\"><strong>A WordPress AJAX Plugin<\/strong><\/h2>\n\n\n\n<p>You can create a WordPress AJAX plugin by following these steps. If done carefully, it wouldn\u2019t take more than few minutes. To start the process first you need to take an empty WordPress plugin created and activated in your website.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-step-1\"><strong>Step 1:<\/strong> <\/h3>\n\n\n\n<p>Then get a good name for this plugin. You can check if the plugin name is already taken by checking the plugin repositories.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-step-2\"><strong>Step 2:<\/strong> <\/h3>\n\n\n\n<p>Now you need to create a PHP file using this plugin name. The plugin should not share the PHP file. Your plugin will require users to put the PHP file in WordPress plugin directory. That\u2019s why the plugin shouldn\u2019t have common name or PHP file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-step-3\"><strong>Step 3:<\/strong> <\/h3>\n\n\n\n<p>The plugin must have at least one PHP file along with JavaScript, CSS languages and an image file. If you find multiple files then put a unique name for directory and for main PHP file. Put all the plugin files in directory. Request the users to upload whole directory in \u2018wp-content\/plugin\/\u2019 directory.<\/p>\n\n\n\n<p>The WordPress can change the standard plugin directory. That\u2019s why you should use \u2018plugin_dir_path()\u2019 and \u2018plugin_url()dor\u2019 path and URLs in PHP code.<\/p>\n\n\n\n<p>We have successfully created WordPress AJAX plugin with PHP. Now we need to find the \u2018single_php\u2019 post template in the theme name. For this you need to go to the root directory of current website. This file should be open for edit.<\/p>\n\n\n\n<p>For users to like the post there has to be a link. If you have enabled JavaScript then you can use JavaScript files. If not then you can use the link directly. For this you need to put codes in the \u2018single_php\u2019 file. You can add the code into any of the template\u2019s parts.<\/p>\n\n\n\n<p>Now we have created the required link successfully. This link will forward you to the \u2018admin_ajax.php\u2019 script. They can\u2019t run any action in it yet. For creating the function in plugin file and to add it in hooks created on WordPress you need to insert a code.<\/p>\n\n\n\n<p>By putting this code, when the user will click on \u2018like this post\u2019 the number of likes will be updated.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-adding-javascript-support\"><strong>Adding Javascript Support<\/strong><\/h2>\n\n\n\n<p>If you have a support from JavaScript, the whole thing would be a lot easier. To use AJAX with PHP you need to enqueue the jQuery library. You need to do it along with custom JavaScript file of the plugin. For this you need to write a certain code.<\/p>\n\n\n\n<p>After this you need to create the JavaScript file \u2018liker_scripts.js\u2019. This file is supposed to be uploaded in the plugin folder of root directory of your website. To create this file you will have to write a code as well.<\/p>\n\n\n\n<p>These are some ways to use AJAX with PHP in WordPress!<\/p>\n\n\n<h2 class=\"test-bundle-banner\"><\/h2>\n\n\n<p>Our <a href=\"https:\/\/www.vwthemes.com\/themes\/wp-theme-bundle\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>best WordPress theme bundle<\/strong><\/a> is packed with all <a href=\"https:\/\/www.vwthemes.com\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>professional WordPress themes<\/strong><\/a>. All the themes have advanced features for you to create the best website for business, blog or services. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>When managing WordPress website, you have to know about different languages that help it function. Even if you don\u2019t have to know about coding for creating website; you must know the languages. The one that helps these languages in creation is AJAX! Before we go to use AJAX with PHP, let\u2019s know more about AJAX [&hellip;]<\/p>\n","protected":false},"author":9250,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-61121","post","type-post","status-publish","format-standard","hentry","category-about-wp-themes"],"_links":{"self":[{"href":"https:\/\/preview.vwthemesdemo.com\/old_website\/wp-json\/wp\/v2\/posts\/61121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/preview.vwthemesdemo.com\/old_website\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/preview.vwthemesdemo.com\/old_website\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/preview.vwthemesdemo.com\/old_website\/wp-json\/wp\/v2\/users\/9250"}],"replies":[{"embeddable":true,"href":"https:\/\/preview.vwthemesdemo.com\/old_website\/wp-json\/wp\/v2\/comments?post=61121"}],"version-history":[{"count":3,"href":"https:\/\/preview.vwthemesdemo.com\/old_website\/wp-json\/wp\/v2\/posts\/61121\/revisions"}],"predecessor-version":[{"id":61487,"href":"https:\/\/preview.vwthemesdemo.com\/old_website\/wp-json\/wp\/v2\/posts\/61121\/revisions\/61487"}],"wp:attachment":[{"href":"https:\/\/preview.vwthemesdemo.com\/old_website\/wp-json\/wp\/v2\/media?parent=61121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/preview.vwthemesdemo.com\/old_website\/wp-json\/wp\/v2\/categories?post=61121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/preview.vwthemesdemo.com\/old_website\/wp-json\/wp\/v2\/tags?post=61121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}