I am using WordPress 4.9.7 and I am trying to develop a plugin that downloads an image from a website and saves it in the media library and sets it as featured image on a post.

Find below my full plugin code for saving the image:


Plugin Name: Custom Importer Coin Data
Author: Test

add_action('admin_menu', 'button_menu');

function button_menu()
    add_menu_page('Import Coin Page', 'Import Coins from CMC/WTM', 'manage_options', 'coinmarketcapimporter-slug', 'importCoinMarketCap_admin_page');


function importCoinMarketCap_admin_page()

    if (!current_user_can('manage_options')) {
        wp_die(__('You do not have sufficient privilege to access this page.'));

    echo '<div class="wrap">';
    echo '<h2>Import Data</h2>';

    // Check whether the button has been pressed AND also check the nonce
    if (isset($_POST['importCoinMarketCap']) && check_admin_referer('importCoinMarketCap_clicked')) {
        // the button has been pressed AND we've passed the security check

    echo '<form action="options-general.php?page=coinmarketcapimporter-slug" method="post">';

    echo '<input type="hidden" value="true" name="importCoinMarketCap" />';
    submit_button('Import CoinMarketCap/WhatToMine Coins');
    echo '</form>';

    echo '</div>';


function importAction()
    $COIN_MARKET_CAP_URL = 'https://s2.coinmarketcap.com/generated/search/quick_search.json';
    $WHAT_TO_MINE_URL = 'https://whattomine.com/calculators.json';

    echo '<div id="message" class="updated fade"><p>' . 'The "Call Function" button was clicked.' . '</p></div>';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $COIN_MARKET_CAP_URL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    $output = str_replace("'", "", $output);
    $outputdecoded = json_decode($output, true);

    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, $WHAT_TO_MINE_URL);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    $outputWTM = curl_exec($c);
    $outputWTM = str_replace("'", "", $outputWTM);
    $outputdecodedWTM = json_decode($outputWTM, true);

    echo '<b>Start import</b>';
    echo '<ul>';
    foreach ($outputdecodedWTM['coins'] as $key => $wtm) {
        $algo = $wtm['algorithm'];

        foreach ($outputdecoded as $coin) {
            if($wtm['tag'] == $coin['symbol']){

                // Check if the post already exists
                if (!post_exists($coin['name'])) {
                    // Get CMC ID
                    $cmcid = $coin["id"];
                    // Get public ID
                    $pubid = $coin["slug"];
                    // Using the CMC ID build the link for logo
                    $file_url = "https://s2.coinmarketcap.com/static/img/coins/128x128/" . $cmcid . ".png";

                    // insert the post
                    echo ('<li>' . $pubid . '</li>');

                    $postId = wp_insert_post(array(
                        'post_type' => 'coin',
                        'post_status' => 'publish',
                        'post_title' => sanitize_textarea_field($coin['name']),
                        'post_name' => sanitize_textarea_field($coin['slug']),
                        'meta_input' => array(
                            'symbol' => sanitize_textarea_field($wtm['tag']),
                            'coin_algorithm' => sanitize_textarea_field($algo),

                    // get image
                    $uploaddir  = wp_upload_dir();
                    $filename = $uploaddir['path'] . '/' . $coin["slug"]  . '.png';

                    $contents = file_get_contents($file_url);
                    $savefile = fopen($filename, 'w');
                    fwrite($savefile, $contents);

                    // add image to media library
                    $wp_filetype = wp_check_filetype(basename($filename), null);

                    $attachment = array(
                        'post_mime_type' => $wp_filetype['type'],
                        'post_title' => $filename,
                        'post_content' => '',
                        'post_status' => 'inherit'

                    $thumbnail_id = wp_insert_attachment($attachment, $filename);
                    set_post_thumbnail($postId, $thumbnail_id);
    echo '</ul>';
    echo '<b>End import</b>';

The download and upload to the image path works perfectly on my dev server, however, when I upload the plugin to the production server, I cannot load the image correctly.

On the production server, the image cannot be found as the file path seems to be wrong:

enter image description here

Am I saving the image wrongly or with the wrong file path?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.