wp-content » themes » functions.php » Display CryptoCurrency Prices on a WordPress Website without any plugins

Display CryptoCurrency Prices on a WordPress Website without any plugins

Here is How to display current CryptoCurrency prices on a WordPress website without using any plugins.

Add these snippets to the functions.php file of your active theme and then add the shortcode where you want to display the content.


Bitcoin Price

This snippet uses the CoinDesk API to retrieve the current Bitcoin price:

function bitcoin_price_shortcode() {
    $response = wp_remote_get( 'https://api.coindesk.com/v1/bpi/currentprice.json' );
    $data = json_decode( wp_remote_retrieve_body( $response ), true );
    $price = $data['bpi']['USD']['rate'];

    return $price;
}
add_shortcode( 'bitcoin', 'bitcoin_price_shortcode' );

Then just add the [bitcoin] shortcode anywhere you want to display the price, for example:

The current Bitcoin price is $[bitcoin]
image 17 - Display CryptoCurrency Prices on a WordPress Website without any plugins

You can also change the currency in the code above from USD to EUR.


Etherium Price

This snippet uses the CryptoCompare API to retrieve the current Ethereum price:

function eth_price_shortcode() {
    $response = wp_remote_get( 'https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD' );
    $data = json_decode( wp_remote_retrieve_body( $response ), true );
    $price = $data['USD'];

    return  $price;
}
add_shortcode( 'ethereum', 'eth_price_shortcode' );

Add the [ethereum] shortcode anywhere you want to display the price, for example:

The current Ethereum price is $[ethereum]
image 16 - Display CryptoCurrency Prices on a WordPress Website without any plugins

Top 5 List

Use the shortcode [top5] to display a list of the top 5 cryptocurrencies along with their current price in USD:

image 15 - Display CryptoCurrency Prices on a WordPress Website without any plugins
function top_cryptos_shortcode() {
    $response = wp_remote_get( 'https://min-api.cryptocompare.com/data/top/totalvolfull?limit=5&tsym=USD' );
    $data = json_decode( wp_remote_retrieve_body( $response ), true );

    $output = '<ul>';
    foreach ( $data['Data'] as $crypto ) {
        $name = $crypto['CoinInfo']['FullName'];
        $price = $crypto['RAW']['USD']['PRICE'];
        $output .= '<li>' . $name . ': $' . $price . '</li>';
    }
    $output .= '</ul>';

    return $output;
}
add_shortcode( 'top5', 'top_cryptos_shortcode' );

Top10 List

To display a list of the top10 cryptocurrencies with their prices use the following snippet and [top10] shortcode:

function top10_cryptos_shortcode() {
    $response = wp_remote_get( 'https://min-api.cryptocompare.com/data/top/totalvolfull?limit=10&tsym=USD' );
    $data = json_decode( wp_remote_retrieve_body( $response ), true );

    $output = '<ul>';
    foreach ( $data['Data'] as $crypto ) {
        $name = $crypto['CoinInfo']['FullName'];
        $price = $crypto['RAW']['USD']['PRICE'];
        $output .= '<li>' . $name . ': $' . $price . '</li>';
    }
    $output .= '</ul>';

    return $output;
}
add_shortcode( 'top10', 'top10_cryptos_shortcode' );
image 18 - Display CryptoCurrency Prices on a WordPress Website without any plugins

Top 10 Simple Table

This snippet uses CryptoCompare API to get prices of the top 10 currencies and displays a nice table with 24h price changes in red or green color.

function table_cryptos_shortcode() {
    $response = wp_remote_get( 'https://min-api.cryptocompare.com/data/top/totalvolfull?limit=10&tsym=USD' );
    $data = json_decode( wp_remote_retrieve_body( $response ), true );

    $output = '<table>';
    $output .= '<tr><th>Name</th><th>Price</th><th>24h Change</th></tr>';
    foreach ( $data['Data'] as $crypto ) {
        $name = $crypto['CoinInfo']['FullName'];
        $price = $crypto['RAW']['USD']['PRICE'];
        $change = round( $crypto['RAW']['USD']['CHANGE24HOUR'], 2 );
        $color = ( $change >= 0 ) ? 'green' : 'red';
        $output .= '<tr><td>' . $name . '</td><td>$' . $price . '</td><td style="color:' . $color . '">' . $change . '</td></tr>';
    }
    $output .= '</table>';

    return $output;
}
add_shortcode( 'cryptotable', 'table_cryptos_shortcode' );

Use the [cryptotable] shortcode anywhere you want to display the table.

chrome cwCho4RKbO - Display CryptoCurrency Prices on a WordPress Website without any plugins
Top10 Simple Table

Top10 Advanced Table

An advanced table of the top 10 Cryptocurrencies that also shows their image, price, 24h change, supply, market cap, and trading volume.

Different background colors for the 24hr change and rounding up numbers.

Use the [cryptotablepro] shortcode anywhere you want to display the table.

chrome rNlKyXO7L0 - Display CryptoCurrency Prices on a WordPress Website without any plugins
Top10 Advanced Table
function top_tablepro_shortcode() {
    $response = wp_remote_get( 'https://min-api.cryptocompare.com/data/top/totalvolfull?limit=10&tsym=USD' );
    $data = json_decode( wp_remote_retrieve_body( $response ), true );

    $output = '<table>';
    $output .= '<tr><th>Name</th><th>Price</th><th>24h Change</th><th>Market Cap</th><th>Volume</th><th>Supply</th></tr>';
    foreach ( $data['Data'] as $crypto ) {
        $name = $crypto['CoinInfo']['FullName'];
        $price = $crypto['RAW']['USD']['PRICE'];
        $change = round( $crypto['RAW']['USD']['CHANGE24HOUR'], 2 );
        $market_cap = $crypto['RAW']['USD']['MKTCAP'];
        $volume = $crypto['RAW']['USD']['VOLUME24HOUR'];
        $supply = $crypto['RAW']['USD']['SUPPLY'];
        $color = ( $change >= 0 ) ? 'lightgreen' : 'red';

        $market_cap_string = ( $market_cap >= 1000000000 ) ? round( $market_cap / 1000000000, 2 ) . 'B' : ( ( $market_cap >= 1000000 ) ? round( $market_cap / 1000000, 2 ) . 'M' : ( ( $market_cap >= 1000 ) ? round( $market_cap / 1000, 2 ) . 'K' : $market_cap ) );
        $volume_string = ( $volume >= 1000000000 ) ? round( $volume / 1000000000, 2 ) . 'B' : ( ( $volume >= 1000000 ) ? round( $volume / 1000000, 2 ) . 'M' : ( ( $volume >= 1000 ) ? round( $volume / 1000, 2 ) . 'K' : $volume ) );
        $supply_string = ( $supply >= 1000000000 ) ? round( $supply / 1000000000, 2 ) . 'B' : ( ( $supply >= 1000000 ) ? round( $supply / 1000000, 2 ) . 'M' : ( ( $supply >= 1000 ) ? round( $supply / 1000, 2 ) . 'K' : $supply ) );

        $output .= '<tr><td><img src="https://www.cryptocompare.com' . $crypto['CoinInfo']['ImageUrl'] . '" alt="' . $name . ' logo" style="width:25px;height:25px;"> ' . $name . '</td><td>$' . $price . '</td><td style="background-color:' . $color . '">' . $change . '</td><td>$' . $market_cap_string . '</td><td>$' . $volume_string . '</td><td>' . $supply_string . '</td></tr>';
    }
    $output .= '</table>';

    return $output;
}
add_shortcode( 'cryptotablepro', 'top_tablepro_shortcode' );

Table of Top10 Exchanges

This snippet uses the CoinGecko API to get a list of exchanges sorted by “trust score” and displays exchanges along with their logo and link to the website + a 24hrs trading volume in BTC.

image 19 - Display CryptoCurrency Prices on a WordPress Website without any plugins
Table of Top10 Crypto Exchanges
function crypto_exchanges_shortcode_function() {
    $output = '<table>';
    $output .= '<tr>';
    $output .= '<th>Exchange</th>';
    $output .= '<th>Trading Volume (24h)</th>';
    $output .= '</tr>';

    $response = wp_remote_get( 'https://api.coingecko.com/api/v3/exchanges' );
    $data = json_decode( wp_remote_retrieve_body( $response ), true );

    usort( $data, function( $b, $a ) {
        return $a['trust_score'] <=> $b['trust_score'];
    } );

    $i = 0;
    foreach ( $data as $exchange ) {
        if ( $i >= 10 ) {
            break;
        }

        $volume = ceil( $exchange['trade_volume_24h_btc'] );

        $output .= '<tr>';
        $output .= '<td>' . $exchange['name'] . '</td>';
        $output .= '<td>' . $volume . ' BTC</td>';
        $output .= '</tr>';

        $i++;
    }

    $output .= '</table>';

    return $output;
}
add_shortcode( 'crypto_exchanges', 'crypto_exchanges_shortcode_function' );

Use the [crypto_exchanges] shortcode anywhere you want to display the table.


Table of NFT projects

Display a table of the NFT’s from CoinGecko API (beta):

image 20 1024x838 - Display CryptoCurrency Prices on a WordPress Website without any plugins
Table of Top NFTs from CoinGecko

Use the [nft_table] shortcode anywhere you want to display the table of NFTs.

function nft_table_shortcode_function() {
    $output = '<table>';
    $output .= '<tr>';
    $output .= '<th>NFT</th>';
    $output .= '<th>Symbol</th>';
    $output .= '<th>Contract Address</th>';
    $output .= '</tr>';

    $response = wp_remote_get( 'https://api.coingecko.com/api/v3/nfts/list' );
    $data = json_decode( wp_remote_retrieve_body( $response ), true );

    foreach ( $data as $collection ) {
        $output .= '<tr>';
        $output .= '<td>' . $collection['name'] . '</td>';
        $output .= '<td>' . $collection['symbol'] . '</td>';
        $output .= '<td>' . $collection['contract_address'] . '</td>';
        $output .= '</tr>';
    }

    $output .= '</table>';

    return $output;
}
add_shortcode( 'nft_table', 'nft_table_shortcode_function' );

Was this post helpful?

Leave a Comment

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.

Recommended