首页 问答 正文

如何在WordPress中添加货币转换功能

注册会员 / 悠然自学 / 2023-06-11/ 浏览 68 次

可以通过在WordPress中使用插件或代码来添加货币转换功能。

  1. 使用插件:

可以使用插件来添加货币转换功能,最常用的插件是WP Currency Converter。安装并激活该插件后,可以通过以下步骤启用货币转换功能:

步骤1:启用插件后,转到该插件的设置页面(左侧边栏-> Settings-> WP Currency Converter)。

步骤2:选择默认货币和目标货币并保存更改。

步骤3:在文章或页面中插入转换表单。可以使用特定的短代码([currency_converter])或添加可视化编辑器中提供的小部件。

  1. 使用代码:

另一种添加货币转换功能的方法是使用代码。可以使用以下代码将货币转换器添加到 WordPress 在文章页面或其他页面。

<?php
function currency_converter_shortcode() {
    //计数器变量
    $counter = 0;
    //将所有货币代码保存在数组中
    $currencies = array('USD' => 'US Dollar', 'EUR' => 'Euro', 'JPY' => 'Japanese Yen', 'GBP' => 'British Pound Sterling', 'AUD' => 'Australian Dollar', 'CAD' => 'Canadian Dollar', 'CHF' => 'Swiss Franc', 'CNY' => 'Chinese Yuan Renminbi', 'HKD' => 'Hong Kong Dollar', 'INR' => 'Indian Rupee', 'NZD' => 'New Zealand Dollar', 'SEK' => 'Swedish Krona', 'SGD' => 'Singapore Dollar', 'TRY' => 'Turkish Lira', 'ZAR' => 'South African Rand');
  //输出表单
    ob_start();
    ?>
    <form id="currency-converter-form" class="currency-converter" action="<?php echo esc_url( $_SERVER['REQUEST_URI'] ); ?>" method="post">
        <?php wp_nonce_field( 'currency_converter_form_submit', 'currency_converter_form_nonce' ); ?>
        <input type="number" name="currency-converter-amount" placeholder="Amount..." />
        <select name="currency-converter-from" id="currency-converter-from">
            <?php
            //循环输出所有货币
            foreach ( $currencies as $code => $name ) :
                ?>
                <option value="<?php echo esc_attr( $code ); ?>"><?php echo esc_html( $name ); ?> (<?php echo esc_html( $code ); ?>)</option>
            <?php
            endforeach;
            ?>
        </select>
        <select name="currency-converter-to" id="currency-converter-to">
            <?php
            //循环输出所有货币
            foreach ( $currencies as $code => $name ) :
                ?>
                <option value="<?php echo esc_attr( $code ); ?>"><?php echo esc_html( $name ); ?> (<?php echo esc_html( $code ); ?>)</option>
            <?php
            endforeach;
            ?>
        </select>
        <button type="submit" name="currency-converter-submit" id="currency-converter-submit">Convert</button>
    </form>
    <?php
    //输出表单信息
    if( isset( $_POST['currency-converter-submit'] ) && wp_verify_nonce( $_POST['currency_converter_form_nonce'], 'currency_converter_form_submit' ) ) {
        $_amount = isset( $_POST['currency-converter-amount'] ) ? floatval( $_POST['currency-converter-amount'] ) : 1;
        $_from = isset( $_POST['currency-converter-from'] ) ? $_POST['currency-converter-from'] : 'USD';
        $_to = isset( $_POST['currency-converter-to'] ) ? $_POST['currency-converter-to'] : 'USD';

        $url = "https://api.exchangeratesapi.io/latest?symbols={$_to}&base={$_from}";
        $response = wp_remote_get( $url );

        if( is_array( $response ) ) {
            $_rates = json_decode( $response['body'], true );
            if( isset( $_rates['rates'] ) ) {
                $_rate = $_rates['rates'][ $_to ];
                $_result = round( $_amount * $_rate, 2 );
                echo '<div class="currency-converter-result"><p>' . $_amount . ' ' . $_from . ' = ' . $_result . ' ' . $_to . '</p></div>';
            }
        }
    }
    // return shortcode output
    return ob_get_clean();
}
add_shortcode( 'currency_converter', 'currency_converter_shortcode' ); 

要在 WordPress 页面或文章中显示货币转换表单,只需要添加以下短代码:

[currency_converter]

大家谈论
    我的见解