• I am trying to get a picture from google sheets and show it on WordPress. Currently I can get the contents of the cell and display it on WordPress using shortcode, but it won’t show anything if there is an image in the cell. This is the plugins code that I made. `

    function sheet_value_shortcode($atts) {
        $API = 'AIzaSyCraqgs12gUCy2asiXyvnPOaNd238bZ2k';
        $google_spreadsheet_ID = '1DQ7JxN9P4C241uMxT-aMSjt4pZe0mObWtMUIixinU9';
        $api_key = esc_attr( $API);
    	
    	
        $location = $atts['location'];
    	
    	
    	
        $get_cell = new WP_Http();
        $cell_url = "https://sheets.googleapis.com/v4/spreadsheets/$google_spreadsheet_ID/values/$location?&key=$api_key";	
        $cell_response = $get_cell -> get( $cell_url);
        $json_body = json_decode($cell_response['body'],true);	
        $cell_value = $json_body['values'][0][0];
        return $cell_value;
    	
    }
    add_shortcode('get_sheet_value', 'sheet_value_shortcode');

    If that is impossible, then would it be possible to have the cell contain a link to the image in google drive or somewhere else and have WordPress show it?

    • This topic was modified 2 years, 5 months ago by nathanhkn.
Viewing 3 replies - 1 through 3 (of 3 total)
  • It depends on what Google returns here as a value. Unfortunately I can’t execute your code because I don’t know your table. Have a look with var_dump() exactly what it is for a value. If it is a binary code (base64 encoded if necessary) representing an image, you could import it into wordpress and then display it in some form. Have a look here: https://developer.www.ads-software.com/reference/functions/wp_insert_attachment/

    Thread Starter nathanhkn

    (@nathanhkn)

    Thanks, I think my code gets a json. For the wp_insert_attachment would my first parameter be $cell_value? And I’m guessing wp_insert_attachment can go into any of my functions.

    Yes, you get a JSON which you already convert to an array. What exactly is in it for the cell, I can not tell you as I said because that is dependent on Google and not a task of WordPress. You would have to check that and depending on that then implement the further steps for the import of the image or its output via data.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Import picture from google sheets to WordPress’ is closed to new replies.