How to Use JSON With AJAX?

I've defined a PHP callback function to handle the communication between server-side and client-side: function json_render_modal_body() { check_ajax_referer( THEME_PREFIX . '-modals', 'nonce' ); $response = array( 'text' => 'no' ); $modal_id = null; if ( isset( $_GET['modal_id'] ) && ! $modal_id = filter_var( $_GET['modal_id'], FILTER_SANITIZE_STRING ) ) { wp_send_json_error( $response ); } if ( ! $form = render_form( $modal_id ) ) { wp_send_json_error( $response ); } $response['text'] = 'yes'; wp_send_json_success( $response ); } I've told WordPress about this function, to …
Category: Web

Error message: Response is not a valid JSON response

Good morning, I need your help! I have my wordpress and woocommerce website hosted on azure, and when trying to edit a widget I get this error 'invalid JSON...' help! I already read all the forums with the different solutions, and nothing works. I already installed the classic editor, save the permalinks, it still doesn't work, it only happens with the widgets, and I only have a problem in azure, because I had it in another hosting and it worked …
Category: Web

Include results with tags relevant to the search keyword with JSON rest API v2

Wordpress doesn't natively search for tags so I need to install WP Extended Search and add post_tags as one of its parameters so whenever I search for keywords natively, posts with that tag will appear. However, when I tried searching on the wp-json REST API, that post doesn't appear. Here's an example with a post that is tagged with "technology". When I search "technology " using this json rest api url, it returns an empty array: example.com/posts?search=technology But if I …
Category: Web

Display full WordPress post under search form using AJAX

Hi I am currently creating a site that shows interactions between different pharmaceutical drugs. I originally started with Live search on JSON Objects Data Using jQuery (originally based on this article: https://www.js-tutorials.com/jquery-tutorials/live-search-json-objects-data-using-jquery/ but modified slightly below) however the client must be able to add constant content and a JSON file was too difficult for them to edit. I am now creating a page for each drug as a WordPress post (and perhaps use a tag to distinguish it for search) …
Category: Web

Display Post Featured Image along with Categories via WP Rest API

I have a custom function that pulls in (3) posts from another Wordpress blog, and it's pulling them from specific categories I specify via a shortcode. Now, with that said, I cannot get it to function and display the actual image, as I can only get it to show the featured Image's (media attachment's) ID itself. Here is my functional code, which works except the image is not displayed, instead it's the images ID (numerical value) function feedpuller_func($atts) { $url …
Category: Web

Why is json_decode failing?

I'm trying to determine if this is related to my having the latest version of PHP on my server while using the latest version of Wordpress. Or if I'm just doing it wrong: Here's my function that is correctly returning values (I can see them when I do an echo or a var dump): function my_Get_CURL (){ $url = 'http://someotherserver/event/94303?radius=30'; // Initiate curl $ch = curl_init(); // Disable SSL verification curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Will return the response, if false …
Category: Web

Why does wp_remote_post returns an empty body response on certain endpoints?

Im gonna make this brief; Im using wp_remote_post to do a POST request to an external API. My local development environment looks something like "http://website.test". As of now I am requesting to a QA version of this API so the protocol of the endpoints is HTTP. The problem is that the $response object always returns an empty body. If I copy the exact request JSON that im sending over, and the exact HTTP URL of the API, and use it …
Category: Web

get all products of one category

i use this function to get all products of one category in ajax request function get_products() { $cat_id = $_POST['category']; $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'posts_per_page' => '12', 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', //This is optional, as it defaults to 'term_id' 'terms' => $cat_id, 'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'. ), array( 'taxonomy' => 'product_visibility', 'field' => 'slug', 'terms' => 'exclude-from-catalog', // Possibly …
Category: Web

Accessing Flask WS APIs over intranet -

I have 2 scripts - A.py and B.py, and both are Flask apps. A.py renders a web page and acts as my UI taking inputs from user. B.py is hold the main logic and has a web service API being called by A.py. Both run as flask app services in localhost from different ports - 5001 and 5002, respectively. My doubts - 1) Every time I make any changes to my B.py (or A.py), do i need to stop and …
Category: Data Science

Working with a json feed & trying to figure out how best to import

Good morning all, I need to be able to import a json product feed on a daily basis to update our products and stock. I am starting to write a plugin to make this work, but have a couple of questions if somebody could please advise? (I'm relatively new to Wordpress) Here is a sample of the json I need to import: { "Type": "Air Pistol", "Mechanism": "CO2", "Calibre": "4.5mm BB", "Make": "ASG", "Model": "SCHOFIELD", "Variant": "GREY", "Origin": null, "Orientation": …
Category: Web

AJAX in plugin wp_send_json() sending html

I wrote a plugin that calls a PHP function from JQuery and the php sends a json response back to the Jquery using wp_send_json(). The functions are all called successfully, but the json request sends a lot of html to the jquery function every time. How do I make the json sent by the php function only a specific message? JQUERY: jQuery( document ).ready( function() { jQuery( 'body' ).on( 'click', '.wpm_mail_link', function( e ) { var varData = 'name:foobar&[email protected]'; jQuery.ajax({ …
Category: Web

how to get json file in wordpress template

I am trying to get data from JSON file in Wordpress template using file_get_contents But can not get the file $data = file_get_contents("../assets/data2.json"); $arr = json_decode($data, true); Facing this error while var_dump $data object(WP_Error)#825 (2) { ["errors"]=> array(1) { ["http_request_failed"]=> array(1) { [0]=> string(29) "A valid URL was not provided." } } ["error_data"]=> array(0) { } } My data2.json file is in the assets folder and trying from templates folder in theme folder in wordpress.
Category: Web

Retrieving JSON data in ajax request from media uploader

I'm having some problems getting my JSON data that's being passed via an ajax request. I'm trying to implement the media uploader into a plugins settings page. I've got it functioning properly using this method as far as the media uploader functionality is concerned. I'd like to be able to retrieve the 'attachment' data in PHP, decode it and then process it once the media uploader is closed and the images have been selected. The ajax request is working because …
Category: Web

Import JSON feed into Wordpress Post (wp_insert_post) every 24 hours

I am importing a JSON feed using a php file I am then looping through the JSON data and creating/updating a post using the wp_insert_post function. I want to query the JSON feed every 24 hours to sync any new information to Wordpress. I thought having something like zapier hit the page every 24 hours to run the php could work but it seemed there must be a better way to do that in wordpress/php?
Category: Web

How to Get Featured Image from REST API?

I am trying to get the featured image of a post using the WordPress REST API. I am adding the extention of ?embed to the end if the REST API url and I am seeing the featured image data under ['wp:featuredmedia'], but I can't seem to display that data. I am using Vue to display data from the REST API and so what I am doing currently to try and get the featured image source is: post._embedded['wp:featuredmedia'][0].source_url But by doing …
Category: Web

Disable and Hide WP-JSON

Without the use of plugin how is possible to deactivate wp-json and redirect the URL to a 404 page. Whenever someone calls example.com/wp-json it redirects it to 404 page instead of loading? I tried to do it via htaccess but it's not working any possible solution please?
Category: Web

WordPress REST API not working on localhost

I have a WordPress site running WordPress 5.9.3 that is using a React-based theme, which uses the REST API (v1), to retrieve content and menus from the standard WordPress CMS back-end. However, when trying to install it locally, I can't get the API to work and retrieve the needed data. I'm using a Docker environment based off this repo: https://github.com/kassambara/wordpress-docker-compose Since the theme uses React and the REST API to get the site content, trying to view the site gets …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.