I need to get only title of all Pages of my wordpress site with WP Handbook API. I use this function in php to get all pages, but I need only the title. How do I do to manipolate the result data?
function getHomePage($link){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link . '/wp-json/wp/v2/pages');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$pagine = curl_exec($ch);
curl_close($ch);
echo $pagine;
}
In reality I need to get the content of only homepage. How do I do that?
Sorry for my bad english, but I'm Italian.
Related
I created a post-type called "material", with taxonomies of type tags called "ano". I'm trying to filter through the wordpress rest api a specific value for that "ano (years)", but it doesn't work, it returns all posts, including those that don't have the specific tag.
<?php
$url = 'https://cat.site.com/wp-json/wp/v2/material?filter[taxonomy]=ano&filter[term]=1994';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
var_dump($response);
?>
I found the solution, I used the tag ID:
https://cat.site.com/wp-json/wp/v2/material?ano=28
I need help with my code. What it's suppose to do is a user enter the full URL from twitter into a text field ('https://twitter.com/openbayou/status/1487977976033685506') and when the post is saved, it breaks down the url by using explode and then gets data from a tweet via Twitter API v2.
My code:
$tweet_url_raw = get_field('twitter_url');
$parts = explode('/', $tweet_url_raw);
$url = 'https://api.twitter.com/2/tweets/' . $parts[5] . '?expansions=author_id&tweet.fields=created_at&user.fields=username,verified';
$authorization = 'Authorization: Bearer ' . get_field_object('twitter_bearer_token', 'option')['value'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
$tweet_data = json_encode($result);
$tweet_data2 = json_decode($tweet_data);
The code above does get the data:
{"data":{"text":"SEC Super Bowl!!","created_at":"2022-01-31T02:36:09.000Z","author_id":"1471331","id":"1487977976033685506"},"includes":{"users":[{"name":"OpenBayou","verified":false,"id":"1471331","username":"openbayou"}]}}
The problem I'm having is when I'm trying to get individual data from the output. I'm trying to update a text field called tweet with the text from text. I've tried to use update_field('tweet', $tweet_data2->data, $post_id); and it was blank. When I use update_field('tweet2', $tweet_data2["data"], $post_id); all it saves is {
Any idea what I'm doing wrong?
You are almost there.
Since you omitted the associative parameter for json_decode, it defaults to false so you get an object, not an array. You then need to reference it as such:
$tweet_data2 = json_decode($tweet_data);
echo 'Tweet text: ' . $tweet_data2->data->text;
If you prefer to work with arrays, simply pass true to json_decode:
$tweet_data2 = json_decode($tweet_data, true);
echo 'Tweet text: ' . $tweet_data2['data']['text'];
More info on json_decode can be found at the PHP Manual site:
https://www.php.net/manual/en/function.json-decode.php
I want to call instagram api in callback function but when i try to do it an error occured. "Headers alreay sent". I have registered my application on instagram developer. This is very basic thing but i'm new with api.
function insta_plugin_menu(){
add_menu_page( 'insta', 'Insta', 'manage_options', 'insta_slug', 'insta_callback_function','dashicons-admin-users',3 );
}
add_action('admin_menu','insta_plugin_menu');
function insta_callback_function(){
wp_redirect("https://api.instagram.com/oauth/authorize/?client_id=af450e026f99441abf0d4dab66daf84c&redirect_uri=http://wsiserver.website/shahbaz/wordpress/wp-admin/admin.php?page=insta_slug&response_type=code");
}
If you want to call instagram API then wp_redirect() is probably not the best way to do. That function is used to redirect not for calling any api.
You are getting the error may be because of various reasons.
You have to use wp_redirect() before get_header() Then it will not show header error.
you're sending data to the browser before calling wp_redirect()
But ; You should try
function insta_plugin_menu(){
add_menu_page( 'insta', 'Insta', 'manage_options', 'insta_slug', 'insta_callback_function','dashicons-admin-users',3 );
}
add_action('admin_menu','insta_plugin_menu');
function insta_callback_function(){
$url="https://api.instagram.com/oauth/authorize/?client_id=af450e026f99441abf0d4dab66daf84c&redirect_uri=http://wsiserver.website/shahbaz/wordpress/wp-admin/admin.php?page=insta_slug&response_type=code";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
curl_error($ch);
curl_close($ch);
$result = json_decode($result);
}
I am running wordpress on localhost, I can successfully save files into upload directory of wordpress using the following code, but wordpress does not show them in its media library, although it shows just those that I upload using its upload feature.
require("/Applications/MAMP/htdocs/wordpress/wp-load.php");
$Addr = "http://www.xxx.com" . $Addr;
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, $Addr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
ini_set( 'display_errors', TRUE );
error_reporting( E_ALL );
$upload_dir = wp_upload_dir();
$location= $upload_dir['basedir']. "/" . "a.jpg";
file_put_contents($location, $output);
echo 'Photo is uploaded';
When I go to following address I can see the photos there.
http://localhost:8888/wordpress/wp-content/uploads/
I even copied a file manually but it does not show that one as well but when I upload the same file using wordpress itself the file will be shown.
Seems a permission problem, try set 644 with chmod:
file_put_contents($location, $output);
chmod($location, 0644); // <--- new code
I've a problem with wordpress xml-rpc api. My code gets some data from an xml and posts to a blog. Page title posted well, there is no problem on blog but custom fields are broken.
Code file, xml, blog settings and database tables they are all utf-8 encoded.
function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$thumbnail,$cfields,$category,$keywords='',$encoding='UTF-8') {
$title = html_entity_decode(htmlentities($title,ENT_NOQUOTES,$encoding));
$body = html_entity_decode(htmlentities($body,ENT_NOQUOTES,$encoding));
$keywords = html_entity_decode(htmlentities($keywords,ENT_NOQUOTES,$encoding));
array_walk($cfields,arr_encoding); // this function does the same thing with above
$content = array(
'title'=>$title,
'description'=>$body,
'mt_allow_comments'=>0, // 1 to allow comments
'mt_allow_pings'=>0, // 1 to allow trackbacks
'post_type'=>'post',
'mt_keywords'=>$keywords,
'categories'=>array($category),
'custom_fields' => $cfields
);
$params = array(0,$username,$password,$content,true);
$request = xmlrpc_encode_request('metaWeblog.newPost',$params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_URL, $rpcurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_ENCODING, "UTF-8" );
$results = curl_exec($ch);
if(curl_errno($ch))
echo '<hr>curl error:'.curl_error($ch)."<hr>";
curl_close($ch);
return $results;}
and this is arr encoding function:
function arr_encoding($cfields){
if(is_array($cfields))
array_walk($cfields, 'arr_encoding');
else if(is_string($cfields))
$cfields = html_entity_decode(htmlentities($cfields,ENT_NOQUOTES,"UTF-8"));}
Do you have any idea?
OK, here it is:
Don't use
xmlrpc_encode_request('blogger.newPost',$params);
and use:
xmlrpc_encode_request('blogger.newPost',$params,
array('encoding'=>'UTF-8','escaping'=>'markup'));