I'm trying to use the following the code
$response = wp_remote_post( 'https://domain.com/forms/3/input', $args );
if( ! is_wp_error( $response ) ) {
$result = json_decode( $response['body'] );
update_field( 'dk_id', $result->result->id, $post->ID );
}
Somehow the meta is not saved to the database. $result->result->id is an integer when using gettype().
This code is working for me just fine though:
$response = wp_remote_post( 'https://domain.com/forms/3/input', $args );
if( ! is_wp_error( $response ) ) {
$result = json_decode( $response['body'] );
update_field( 'dk_id', 'TEST', $post->ID );
}
It looks like the function won't take my variable. Thanks a bunch for helping out!
Related
I know there are a few items out on the internet over how to achieve this, but I can't get it to work...
Can anyone please point me in the right direction?
I currently have two functions. I want to use the variable ($new_data) from function1 inside function2.
Function 1:
function manipulate_form_submission( $record, $ajax_handler ) {
$form_name = $record->get_form_settings( 'form_name' );
$form_data = $record->get( 'fields' );
//change the names of fields before we send them somewhere
$new_data = array(
'email' => isset( $form_data['email']['value'] ) ? $form_data['email']['value'] : '',
'url' => isset( $form_data['url']['value'] ) ? $form_data['url']['value'] : ''
)
}
Function 2:
add_action( 'woocommerce_payment_complete', 'so_payment_complete' );
function so_payment_complete( $order_id ){
$order = wc_get_order( $order_id );
$response = wp_remote_post( 'https://mywebsite.com/insert_values.php', array( 'body' => $new_data) );
}
Thanks in advance!
Just initialize the variable outside the two functions like this:
<?php
$new_data = false;
function manipulate_form_submission( $record, $ajax_handler ) {
global $new_data;
$new_data = "New value";
}
function so_payment_complete( $order_id ){
// The variable is accessible here
global $new_data;
echo $new_data;
}
?>
I have a function which help me to make a redirect of a product if that product doesn't exist anymore in my affiliate xml.
What I want now is to put the product on draft but to redirect his link to a "This products doesnt exist anymore page" - for seo purposes
I didn't try something because I dont know what.
I saw something here and I think my answear is here but i don't know how to apply it:
Change product status if prices are updated in Woocommerce 3
function my_is_post_to_delete( $is_post_to_delete, $post_id, $import ) {
$redirect_url = "https://stackoverflow.com";
if ( $import->id == 72 ) {
$redirects = get_option( '301_redirects', array() );
$redirects = maybe_unserialize( $redirects );
$url_for_post = get_the_permalink( $post_id );
$url = parse_url( $url_for_post );
if ( $url ) {
if ( ! array_key_exists( $url['path'], $redirects ) ) {
$redirects[ $url['path'] ] = $redirect_url;
update_option( '301_redirects', $redirects );
}
}
return false;
}
}
add_filter( 'wp_all_import_is_post_to_delete', 'my_is_post_to_delete', 10, 3 );
I expect that my code to remove the product from my website feed and redirect his link to a page of my website.
For seo purposes I cannot delete the product for all I just need to hide it from my shop but keep the link for google (also the pictures).
You can use WordPress function to update the status of the product where your logic of product doesn't exist anymore in my affiliate xml. exists.
function my_is_post_to_delete( $is_post_to_delete, $post_id, $import ) {
$redirect_url = "https://stackoverflow.com";
if ( $import->id == 72 ) {
/* Start Here*/
$my_product = array(
'ID' => $import->id,
'post_status' => 'draft',
);
wp_update_post( $my_product );
/* End Here*/
$redirects = get_option( '301_redirects', array() );
$redirects = maybe_unserialize( $redirects );
$url_for_post = get_the_permalink( $post_id );
$url = parse_url( $url_for_post );
if ( $url ) {
if ( ! array_key_exists( $url['path'], $redirects ) ) {
$redirects[ $url['path'] ] = $redirect_url;
update_option( '301_redirects', $redirects );
}
}
return false;
}
}
add_filter( 'wp_all_import_is_post_to_delete', 'my_is_post_to_delete', 10, 3 );
function add_price_calculate_tour() {
global $post;
$product = wc_get_product( $post->ID );
if( $product->is_type( 'tour_booking' ) ):
$priceAdult = (float) get_post_meta( $product->get_id(), '_regular_price', true );
$priceChild = (float) get_post_meta( $product->get_id(), 't_children_price', true );
$tour = json_encode(["priceAdult" => $priceAdult,"priceChild" => $priceChild]);
wp_add_inline_script( 'twentyseventeen-global', 'var tour = '.$tour.'', 'before' );
endif;
}
I would like to simplify or improve this code, what it does basically is to create an array with a value, and then create a json and print it in the footer.
Any suggestions?
So I am working with the coinmarketcap api and try to combinate it with WordPress.
In WordPress I am using the following php code:
function api() {
$url = 'https://api.coinmarketcap.com/v1/ticker/?start=0&limit=250';
$response = wp_remote_get( esc_url_raw( $url ) );
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
$name = $api_response[1]["name"];
$usd = $api_response[1]["price_usd"];
echo $name . "<br />";
echo $usd;
}
add_shortcode( 'api_short', 'api' );
So this code is working and I get my results on the WordPress page by using the shortcode api_short.
My problem now is that I want to use the shortcode on the following way:
[api short name ="20"]
This way I can switch from data easily by only using the shortcode instead of changing the code the whole time.
The 'name' variable in this case is the name of the cryptocurrency as can be seen here: https://api.coinmarketcap.com/v1/ticker/?start=0&limit=250 . 0 = bitcoin , 1 = ethereum.
I hope someone knows a way to get this working, I tried somethings but so far without a result.
The WordPress shortcode documentation can be found here: https://codex.wordpress.org/Shortcode_API
A few things:
First:
Prefix your functions - especially if you're using a name that's got a global scope or appeal.
api() should really be something along the lines of solaiman_api() to prevent any conflicts.
Second:
You should be using the WP Transients API to cache results so you don't rate limit the CMC API
Third:
For the actual answer to your question, you just need to parameterize the input value in the URL.
function solaiman_api( $atts ) {
extract( shortcode_atts( array(
'placeholder' => ''
), $atts ) );
$coin = $atts['coin'] ? $atts['coin'] : 0; // Default to 0 for BTC
$limit = $atts['limit'] ? $atts['limit'] : 250; // Default to 250
$url = 'https://api.coinmarketcap.com/v1/ticker/?start='. $coin .'&limit='. $limit;
$response = wp_remote_get( esc_url_raw( $url ) );
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
foreach( $api_response as $c ){
echo $c['name'].': $'.$c['price_usd'].'<br />';
}
}
add_shortcode( 'cmc_api', 'solaiman_api' );
Note I changed your function name and the shortcode, since api_short isn't terribly descriptive.
This shortcode would now look like this:
[cmc_api coin="1" limit="1"]
Which should give you just the price of Ethereum.
Use This Code Instead:
Here's a better one that caches the results for 10 minutes according to CMC's terms of service:
function solaiman_api( $atts ) {
extract( shortcode_atts( array(
'placeholder' => ''
), $atts ) );
$coin = $atts['coin'] ? $atts['coin'] : 0; // Default to 0 for BTC
$limit = $atts['limit'] ? $atts['limit'] : 250; // Default to 250
$url = 'https://api.coinmarketcap.com/v1/ticker/?start='. $coin .'&limit='. $limit;
$transient_name = 'cmc_api_'.$coin.'_'.$limit; // One transient per coin per limit
if( false === ( $response = get_transient( $transient_name ) ) ){
$response = wp_remote_get( esc_url_raw( $url ) );
set_transient( $transient_name, $response, 600 ); // Cache for 10 minutes
}
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
foreach( $api_response as $c ){
echo $c['name'].': $'.$c['price_usd'].'<br />';
}
}
add_shortcode( 'cmc_api', 'solaiman_api' );
You can see an example of [cmc_api coin="1" limit="1"] here: http://xhynk.com/headless/2018/02/28/cmc-test/
I'm trying to show SMS balance using an API in a PHP page.
What am I missing to simplify this? (The API is working fine directly):
public function show_sms_credits() {
if ( false === ( $my_query = get_transient( 'available_sms_credits' ) ) ) {
$api_key = get_option( 'wc_api_key', true );
$api_token = get_option( 'wc_api_token', true );
if( empty ( $api_key ) || empty( $api_token ) ){
return;
}
$url = 'http://mywebsite.com/api/';
$response = wp_remote_get("{$this->url}GetBalance?User={$this->api_key}&Password={$this->api_token}");
if( ! is_wp_error( $response ) && 200 == wp_remote_retrieve_response_code( $response ) ) {
$body = json_decode( wp_remote_retrieve_body( $response ) );
set_transient( 'available_sms_credits', $body->sms_credits, 12 * HOUR_IN_SECONDS );
}
}
$sms_credits = get_transient( 'available_sms_credits' );
}
}
<p> SMS Balance: <?php echo 'available_sms_credits': ?> </p>
Because you use this syntax
$url = 'http://mywebsite.com/api/';
You should simply use
$response = wp_remote_get("{$url}GetBalance?User={$api_key}&Password={$api_token}");
Because you've only assigned local variables, not assignments to the current object itself. If you needed to add the URL as a property against your object you'd have use $this like...
$this->url = 'http://mywebsite.com/api/';
Also your're not doing anything with $sms_credits