$post_data = file_get_contents("php://input");
$id = (int) filter_var( $post_data->id, FILTER_SANITIZE_NUMBER_INT );
I don't know whats wrong. In var_dump($post_data) the $post_data is always string(0).
Related
I have a WordPress hook: add_action( 'publish_post', 'post_published_notification', 10, 2 );
When publishing I am unable to get the featured image. I have tried many different methods to no avail. After the initial publishing of an article I get '0' for the thumbnail id but after editing and clicking update I get all the correct values.
Hook looks like this:
function post_published_notification( $post_id, $post ) {
$post_status = get_post_status( $post_id ); //always returns "publish"
$title = $post->post_title; //works correctly always
$permalink = get_permalink( $post_id ); //works correctly always
$excerpt = get_the_excerpt($post); //works correctly always
//All three variables below are empty when publishing a post but assign correctly when updating an existing post
$image = get_the_post_thumbnail_url( $post_id, 'medium' );
$image2 = get_the_post_thumbnail_url($post_id, 'article-thumbnail-image');
$image3 = get_post_meta( get_post_meta( $post_id, "_thumbnail_id", true ), "_wp_attached_file", true );
$thumb_id = get_post_thumbnail_id( $post_id ); //returns 0 on publish; Correct Id on update
}
Try using the "wp_after_insert_post" hook.
add_action( 'wp_after_insert_post', 'after_insert_post', 10, 4 );
function after_insert_post( $post_id, $post, $update, $post_before ) {
if ( 'publish' !== $post->post_status||( $post_before && 'publish' === $post_before->post_status )||wp_is_post_revision( $post_id )) {
return;
}
$image_url = get_the_post_thumbnail_url($post_id);
}
I'm making an API call using the wp_remote_get method to fetch data from an external source (not a WP site). The API call is working and i'm getting back the response i want which is just a number. Now i want to store this number (the response) as a meta_value in wp_postmeta with the following meta_key (donation_amount) and a specific post_id. The database table already exists. What i have so far is the API Call and the response. I've tried wp_update_post but maybe i am doing something wrong or maybe is not the right approach. Any help would be appreciated.
$url = 'https://developer.mozilla.org/en-US/docs/Web/API/URL_API'; // Not the real url
$response = wp_remote_get( $url );
if( is_wp_error( $response ) ) {
return false;
} else {
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
}
echo $api_response ['theNumber'];
Use WP update_post_meta function. check the below code.
$url = 'https://developer.mozilla.org/en-US/docs/Web/API/URL_API'; // Not the real url
$response = wp_remote_get( $url );
if( is_wp_error( $response ) ) {
return false;
} else {
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
}
$donation_amount = $api_response['theNumber'];
replace $post_id with your post id.
update_post_meta( $post_id, 'donation_amount', $donation_amount );
I would like to generate a link or may be a simple product link with some args to change the status of the product. So when I give that link to anyone he opens the link and then the status of the product change from private to publish and remain publish all the time until I change back from the admin side.
I have tried using this code.
add_action( 'woocommerce_before_single_product', 'cs_change_product_status', 10 );
function cs_change_product_status(){
global $product;
$status = $product->get_status();
if($status == 'private'){
$terms = get_the_terms ( $product->get_id(), 'product_cat' );
$cat_id = 0;
foreach ( $terms as $term ) {
$cat_id = $term->id;
break;
}
if($cat_id === 75){
wp_update_post( array( 'ID' => $product->get_id(), 'post_status' => 'publish' ) );
}
}
}
This is not working on the private products. I think wordpress not loading any woocommerce hook when the product is private and showing 404 error page.
Im extremely new to WordPress and WooCommerce code. I've been provided with some working code that resides in the plugins/woocommerce/templates/archive-product.php
The function is pretty simple, it simply fetches for an array of data from a remote site and makes use of the JSON returned to find the matching SKUs and inject them as items in the product list.
Works quite nicely, however, as I'm new to Woo & WP, I'm hoping someone might be able to show me how I can transform this code into the proper way of it being defined as a plugin?
I'm hoping its just a case of wrapping some additional code around the function, but I'm unsure as to where to start
any tips greatly appreciated
if ( wc_get_loop_prop( 'total' ) ) {
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$cat = explode('/', $actual_link);
if ($cat[4] == 'my-product-list') {
global $current_user;
get_currentuserinfo();
$data = array( 'email' => $current_user->user_email);
$response = wp_remote_post( 'https://www.shop.com/remote-data/', array( 'data' => $data ) );
$curl = 'https://www.shop.com/remote-data/';
$response = wp_remote_get( $curl );
$rows = wp_remote_retrieve_body( $response ) ;
$decode = json_decode(stripslashes($rows), true);
global $wpdb;
$product_id = array();
$i = 0;
foreach ($decode as $single_data) {
foreach ($single_data['items'] as $data) {
$result = $wpdb->get_results ( "SELECT post_id FROM wp_postmeta WHERE meta_key = '_sku' AND meta_value = '".$data."'" );
$product_id[$i] = $result[0]->post_id;
$i++;
}
}
$product_id = array_filter(array_unique($product_id));
$args = array(
'post_type' => 'product',
'post__in' => $product_id
);
//The Query
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) {
$the_query->the_post();
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
You could try to hook it into woocommerce_before_main_content
Like so:
function fetch_shop_data() {
// your code
}
add_action('woocommerce_before_main_content', 'fetch_shop_data');
Some of my wordpress posts have a custom field called a QR shortcode. I want to be able to redirect any URL request when someone goes to domain.com/QRCODEVALUE, and have them directed to the post with the corresponding QR code. Is there any way to do this? I don't even know where to start!
I haven't tested this, but the logic should help you get somewhere. Grab the URL request path, remove the trailing slash if it exists, and query posts where the custom field contains QRCODEVALUE:
<?php
function my_redirect() {
$request = parse_url($_SERVER['REQUEST_URI']);
$path = $request["path"];
$path = explode('/', $path);
$length = count($path);
$result = $path[$length-1];
$id;
$query = new WP_Query( array('meta_key' => 'QRCODEVALUE', 'meta_value' => $result) );
if($query->have_posts()): while($query->have_posts()): $query->the_post();
$id = $post->ID;
endwhile; endif;
wp_redirect( get_permalink( $id ) );
exit;
}
add_action('template_redirect', 'my_redirect');
Reference: Get Everything After Domain Name into a string
Is domain.com/QRCODEVALUE a page? If so, you could do something like this:
function my_redirect() {
if ( is_page( 'QRCODEVALUE' ) ) {
// Get the ID of the post with the corresponding QR code here
// Save the post ID to $post_id
wp_redirect( get_permalink( $post_id ) );
exit;
}
}
add_action( 'template_redirect', 'my_redirect' );