How to get data from array object in WordPress plugin api - wordpress

http://localhost/wordpress/give-api/forms/?key=15443f18029e6f5d3b65d04e1640ffbe&token=c3de770a410282359413c74a588c5c74
The above link is a plugin api link. Above link won't work to your browser.
when I set the above link in the browser , it returns array object like http://postimg.org/image/6ozmjy0e7/ .
My question is , how can I set this url in a variable in wordpress and how can I get the data from that array object. I just want to get the data from that array object. If any other process is available, then please suggest me. Thanks...

In functions.php:
function displayApiUrl() {
global $apiUrl; // you probably don't actually need to set it global as it is a function
$apiUrl = 'http://localhost/wordpress/give-api/forms/?key=15443f18029e6f5d3b65d04e1640ffbe&token=c3de770a410282359413c74a588c5c74';
return $apiUrl;
}
In your theme you can now use:
<?php $api = displayApiUrl(); ?>
With that you can process your array in a foreach loop:
<?php
$json_url = file_get_contents($api);
$json_data = json_decode($json_url, true);
foreach ($json_data['forms'] as $form) {
$form_id = $form['info']['id'];
echo $form_id;
}
?>

The new "standard" for WordPress rest apis is Json Rest API, which will be partially integrated into WordPress core in the next release.
You can get it here https://wordpress.org/plugins/json-rest-api/ and documentation at http://wp-api.org/
In terms of the question how to put array information into the URL, the format is
http://www.example.com/wp-json/endpoint?array_1[key1]=Pensacola&array_1[key2]=Florida
The URL of course changes, and the wp-json/endpoint is replaced with whatever the final endpoint is for which ever rest api you choose to use.

Related

Wordpress - Build one Page to use URL to pull data from REST API

I'd appreciate any advice, resources or assistance with this issue.
I want to be able to have part of my Wordpress site where I can parse the URL and then use those values to populate the page with content from another API.
For example:
server.zz/weather/Sydney%20Australia
server.zz/weather/Houston%20Texas
Where I could write a Plugin which would intercept these requests, be able to extract the end of the URLs, and then call another API to get the data to then merge into a Template to be presented to the visitor.
I know that there are Custom Post Types, but I wasn't sure if they were the best solution for this usage case.
As I said, any advice or suggestions would be appreciated.
I found the solution to this problem by using add_rewrite_rule(), add_rewrite_endpoint(), and flush_rewrite_rule().
For the example I provided earlier, I created the following code in a Plugin.
// Define the URL Rewrite Rules
function crw_rewrite_urls(){
add_rewrite_rule(
'^weather/(.+)$' ,
'index.php?weather_location=$matches[1]' ,
'top'
);
add_rewrite_endpoint('weather_location', EP_ROOT);
flush_rewrite_rules();
}
add_action('init', 'crw_rewrite_urls');
// Initialise the Query Variable
function crw_query_var( $vars ) {
$vars[] = 'weather_location';
return $vars;
}
// Check for the Variable and Display Content as needed
function crw_handler() {
global $wp_query;
if ( isset( $wp_query->query_vars['weather_location'] ) ) {
// Call the API, fill the Template here
}
return;
}
add_action('template_redirect', 'crw_handler');

How to use filters in GuzzleHttp in Laravel?

I am currently using WordPress with Laravel. I write posts in WordPress and display the posts in my Laravel application using WordPress REST API and GuzzleHttp.
Is there any way I can use different filters like where(), sortBy() etc. for filtering the data received from WordPress REST API using GuzzleHttp?
Yes you can serialize the json response to a Laravel Collection and use functions like that from there.
Retrieve the response body, which we assume contains json.
$response = $response->getBody();
Turn the json into an array. Note the seconds parameter is set to true to always output an array.
$responseArray - json_decode($response, true);
Turn the array into a Laravel collection in order to use its cool functions.
$collection = collect($responseArray);
So in short:
$collection = collect( json_decode( $response->getBody(), true ) );
$sortedByPrice = $collection->sortBy('price');

Alter the Main WP_Comment_Query (comments at the bottom of posts)?

My plugin retrieves a few comments at the beginning of the post through my own WP_Comment_Query. I save these IDs so I can then alter the WP_Comment_Query request and not fetch these IDs.
When I use the pre_get_comments hook to hide these already-fetched IDs, they are also hidden from my first query at the beginning of each post. It defies the point.
$this->loader->add_action( 'pre_get_comments', $plugin_public, 'hide_the_comments' );
public function hide_the_comments( $comment_query ) {
$comment_query->query_vars['comment__not_in'] = $the_ids_to_hide;
}
How can we target the bottom request only, just like there is is_main_query() for the post loop?
Create a private variable, eg private $count = 0;
Increment it each time your function is run
Don't hide the comments if it's the first time you're running it :)
If you need to target the "main" WP_Query_Comments() within the comments_template() core function, then the comments_template_query_args filter is available since WordPress 4.5:
$comment_args = apply_filters( 'comments_template_query_args', $comment_args );
$comment_query = new WP_Comment_Query( $comment_args );
See ticket #34442 for more info and a simple example here.

WP Shortcodes not being added

I was creating a wordpress plugin where the user enters in some information, and is able to generate shortcodes. I was not quite sure where the shortcodes are supposed to go - my current setup is class-based, and I want to be able to create a shortcode when an AJAX request is being made, and is successful. The following two methods are in the same file in the class.
This method gets called via the admin-ajax.php file:
public static function processAjax()
{
global $wpdb;
$event_data = $_POST['obj'];
$event_title = $event_data[0];
$event_subdomain = $event_data[1];
$result_events = $wpdb->get_results("SELECT * FROM wp_shortcode_plugin WHERE subdomain = '{$event_subdomain}'", OBJECT);
if (sizeof($result_events)>0) {
echo "duplicate";
} else {
add_shortcode($event_subdomain, 'getEmbed');
$results = $wpdb->insert('wp_shortcode_plugin', array("event_name"=>$event_title, "subdomain"=>$event_subdomain));
echo json_encode($_POST['obj']);
}
die();
}
And here is my getEmbed() method that I would like to call.
public static function getEmbed()
{
return 'test';
}
It seems that the shortcodes are not being created, however. Am I missing something here? Also, is it possible to pass a value to the getEmbed function from the add_shortcode() method?
Instead of adding shortcode directly from AJAX, you should use update_option to store in the information for the shortcode to be loaded. If option doesn't exist, it will be created.
Than you will simple use wp_init hook to load up all of the shortcodes you need to load in the function.php file for the theme or plugin php file.
You should use get_option within the wp_init hook and check the values in there. You will need to have function(s) associated with the shortcodes, which can be autogenerated in php using create_function or you can route them through 1 function (defined in your php file) that will have the $atts and $content parameters defined and do whatever depending on the value of your get_option that you send to that function.
add_shortcode function should be defined within the wp_init hook, after checking the value of the get_option function. You will need to give your option a name and add to it via the ajax function. the option will most likely want to be an array, that wordpress will automatically serialize. Than you use that array returned from get_option to loop through the array of shortcodes, and call add_shortcode as many times as you need there. This requires setting up your option array so that it has a shortcode tag defined in each index of the array. I would, personally, make the shortcode tag the key of the array and all attributes of the shortcode, imo, would than be an array of that array.
Hope this helps you to get started on this.

Module field with feeds, module generating data

I have an issue with triming a field before it is saved. I wanted to use substr(), or regex() with preg_match(). I have built a Drupal 7 module, but it can't work at all. I have tried using the trim plugin in feeds tamper module, but it doesn't seem to work. The data I am using is from a feed from Google Alerts. I have posted this issue here.
This is what I have done so far, and I know my regular expression is wrong; I was trying to get it do anything, just to see if I could get it to work, but I am pretty lost on how to add this type of function to a Drupal module.
function sub_node_save() {
$url = $node->field_web_screenhot['und'][0]['url'];
$url = preg_match('~^(http|ftp)(s)?\:\/\/((([a-z0-9\-]*)(\.))+[a-z0-9]*)($|/.*$)~i',$url );
$node->field_web_screenhot['und'][0]['url'] =$url;
return ;
}
I used the Devel module to get the field.
If there's an easy way to use substr(), I would consider that or something else.
Basically, I just want to take the Google redirect off the URL, so it is just the basic URL to the web site.
Depending on your question and later comments, I'd suggesting using node_presave hook (http://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_presave/7) for this.
It's called before both insert (new) and update ops so you will need extra validations to prevent it from executing on node updates if you want.
<?php
function MYMODULE_node_presave($node) {
// check if nodetype is "mytype"
if ($node->type == 'mytype'){
// PHP's parse_url to get params set to an array.
$parts = parse_url($node->field_web_screenhot['und'][0]['url']);
// Now we explode the params by "&" to get the URL.
$queryParts = explode('&', $parts['query']);
$params = array();
foreach ($queryParts as $param) {
$item = explode('=', $param);
$params[$item[0]] = $item[1];
}
//valid_url validates the URL (duh!), urldecode() makes the URL an actual one with fixing "//" in http, q is from the URL you provided.
if (valid_url(urldecode($parms['q']))){
$node->field_web_screenhot['und'][0]['url'] = urldecode($parms['q']);
}
}
}

Resources