• Resolved kevinselectos

    (@kevinselectos)


    Hi,

    I try to get the default url (https://my-site.com/wp-upload/…) in the function.php file to send it to a json Rest route.

    I get the url of the image I want trough ACF (get_field()).

    I tried to clean the url with preg_replace('/https[:\/\]*ml[a-z1-9]*.i.optimole.com\/.*http/',"http",get_field("shop-color-logo",$id)) but i still got the full optimole url in my generated JSON.

    I also tried to use preg_match('/.+(https[:\/\/]*my-site.+)/',get_field("shop-color-logo",$id),$logoColor) but $logoColor return an empty array.

    How can I get and return the default image URL ?

    Thank you.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Vytis

    (@bvytis)

    Hi @kevinselectos,

    I think your code is correct from what I see and it should be working if
    get_field("shop-color-logo",$id) returns the value from added image URL, however, I’m not sure where you use this value later.

    Inside the shop-color-logo ACF you probably have original image URL added [not the Optimole one] so you don’t even need to replace it here as Optimole changes the URL in the front-end only when page loads so maybe that’s your issue – it’s being replaced AFTER you execute the function.

    I recommend you execute it later if possible after Optimole replaces the URLs on the page load.

    Thread Starter kevinselectos

    (@kevinselectos)

    Hi @bvytis,

    Here is the full code:

    add_action('rest_api_init', function () {
        register_rest_route( 'westland/v1', 'shops',array(
                      'methods'  => 'GET',
                      'callback' => 'get_all_shops'
            ));
      });
    
      function get_all_shops(){
        $output = array();
    
        $shops = new WP_Query(array(
            'post_type' => array('commerces','restaurants'),
            'posts_per_page' => -1,
            'order' => 'DESC',
        ));
    
        if (empty($shops->posts)) {
            return new WP_Error( 'empty_category', 'There are no posts to display', array('status' => 404) );
        }
    
        foreach($shops->posts as $shop){
            $id = $shop->ID;
            $logoColor = "";
            $logoWhite = "";
            preg_match('/.+(https[:\/\/]*westland.+)/',get_field("shop-logo-couleur",$id),$logoColor);
            preg_match('/.+(https:\/\/westland.+)/',get_field("shop-logo-blanc",$id),$logoWhite);
            array_push($output,array(
                'basic'=>array(
                    'id'          => $id,
                    'name'        => get_field("shop_name",$id),
                    'description' => get_field("shop_description",$id),
                    'logoColor'   => $logoColor[1],
                    'logoWhite'   => $logoWhite[1],
                    'category'    => get_the_terms($id,'shops_cats')[0]->name,
                    'type'        => $shop->post_type,
                    'pageURL'     => get_permalink($id),
                    'giftCard'    => get_field("gift_card_participation",$id),
                ),
            ));
        }
    
        $response = new WP_REST_Response($output);
        $response->set_status(200);
    
        return $response;
    }

    As you can see, the goal is to create a REST endpoint to display store data in JSON format.

    But when I’m calling this endpoint, I can see that the value of logoColor is null for every shop.

    I just tried to log the value of the get_field() function with error_log(), and it correctly returns the default URL. So I assume the modification is done when the endpoint is called. How can I prevent optimole from changing the URL of this endpoint ?

    Thread Starter kevinselectos

    (@kevinselectos)

    Hi @bvytis ,

    Any news ?

    Plugin Support Vytis

    (@bvytis)

    Hi @kevinselectos,

    After checking with our team I think I was also overthinking this use case and as your goal is not to have the optimized image in this response the easiest way would be to add the URL of the response to the exclusion list and you will have unoptimized image URLs there. Try adding it like this: https://vertis.d.pr/lbvfiQ and it will not replace the URLs on that response.

    I hope it helps!

    Thread Starter kevinselectos

    (@kevinselectos)

    Hi @bvytis,

    My bad, I didn’t see the URL exclusion setting. Thanks for your help, everything is working fine now.

    Have a good day !

    Plugin Support Vytis

    (@bvytis)

    Perfect! Thanks for confirming it 😎

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Get default url in backend’ is closed to new replies.