尝试在一个页面上使用两次快捷代码时的自定义插件问题

时间:2019-12-17 作者:Grant Olson

感谢您提供的一切帮助。这几天来,我一直在努力解决这个问题,但我发现自己已经不知所措了。。。任何帮助都将不胜感激——这是我第一次尝试创建插件,我对PHP非常陌生。

我有一个自定义插件,是由Joshua David Nelson的Weather in Wordpress with DarkSky 第条和this video series 来自CodeTime。它工作得非常好,除非我尝试在一个页面上使用两次短代码(它嵌入在菜单中,并且尝试在页面上再次使用短代码会导致错误)。我试图在主页上以不同的方式显示天气信息,我想我可以使用相同的功能/快捷码。

以下是插件内容,减去坐标和API键:

function weather_station(){

    $coordinates = \'XXXX\'; //coordinates are here
    $api_key = \'XXXX\'; //api key is here

    $api_url = \'https://api.darksky.net/forecast/\'.$api_key.\'/\'.$coordinates;

    $cache_key = md5( \'remote_request|\' . $api_url );
    $forecast_request = get_transient( $cache_key );

    if ( false === $forecast_request ) {
        $forecast_request = wp_remote_get( $api_url );

        if ( is_wp_error( $forecast_request ) ) {
            // Cache failures for a short time, will speed up page rendering in the event of remote failure.
            set_transient( $cache_key, $forecast_request, 60 );
            return false;
        }
        // Success, cache for a longer time.
        set_transient( $cache_key, $forecast_request, 300 );
    }

    if ( is_wp_error( $forecast_request ) ) {
        return false;
    }

    $body = wp_remote_retrieve_body( $forecast_request );

    $forecast = json_decode( $body );
    //$forecast = json_decode(file_get_contents($api_url));


    $icon_currently = $forecast->currently->icon;
    $temperature_currently = round( $forecast->currently->temperature );
    $summary_hourly = $forecast->hourly->summary;

    // Set the default timezone
    date_default_timezone_set($forecast->timezone);

    // Get the appropriate icon
    function get_icon($icon) {
        if($icon===\'clear-day\') {
            $the_icon = \'<i class="fas fa-sun"></i>\';
            return $the_icon;
        }
        elseif($icon===\'clear-night\') {
            $the_icon = \'<i class="fas fa-moon-stars"></i>\';
            return $the_icon;
        }
        elseif($icon===\'rain\') {
            $the_icon = \'<i class="fas fa-cloud-showers-heavy"></i>\';
            return $the_icon;
        }
        elseif($icon===\'snow\') {
            $the_icon = \'<i class="fas fa-cloud-snow"></i>\';
            return $the_icon;
        }
        elseif($icon===\'sleet\') {
            $the_icon = \'<i class="fas fa-cloud-sleet"></i>\';
            return $the_icon;
        }
        elseif($icon===\'wind\') {
            $the_icon = \'<i class="fas fa-wind"></i>\';
            return $the_icon;
        }
        elseif($icon===\'fog\') {
            $the_icon = \'<i class="fas fa-fog"></i>\';
            return $the_icon;
        }
        elseif($icon===\'cloudy\') {
            $the_icon = \'<i class="fas fa-clouds"></i>\';
            return $the_icon;
        }
        elseif($icon===\'partly-cloudy-day\') {
            $the_icon = \'<i class="fas fa-clouds-sun"></i>\';
            return $the_icon;
        }
        elseif($icon===\'partly-cloudy-night\') {
            $the_icon = \'<i class="fas fa-clouds-moon"></i>\';
            return $the_icon;
        }
        elseif($icon===\'hail\') {
            $the_icon = \'<i class="fas fa-cloud-hail"></i>\';
            return $the_icon;
        }
        elseif($icon===\'thunderstorm\') {
            $the_icon = \'<i class="fas fa-thunderstorm"></i>\';
            return $the_icon;
        }
        elseif($icon===\'tornado\') {
            $the_icon = \'<i class="fas fa-tornado"></i>\';
            return $the_icon;
        }
        else {
            $the_icon = \'<i class="fas fa-thermometer-half"></i>\';
            return $the_icon;
        }
    }

?>

<div class="weather-station">
    <div class="weather-station-button">
        <span class="weather-station-icon"><?php echo get_icon($forecast->currently->icon) ?></span>
        <span class="weather-station-temperature-currently"><?php echo $temperature_currently ?>&deg;</span>
    </div>
    <div class="weather-station-details">
        <p class="weather-station-details-title">Forecast</p>
        <?php

            // Start the foreach loop to get hourly forecast
            foreach($forecast->daily->data as $day):

        ?>

        <p class="weather-station-details-temperature-range"><?php echo round($day->temperatureLow).\'&deg;/\'.round($day->temperatureHigh).\'&deg;\'; ?></p>

        <?php
            // Break because we got today\'s low/high
            break;

            // End the foreach loop
            endforeach;

        ?>

        <p class="weather-station-details-summary"><?php echo $summary_hourly ?></p>

        <?php if (!empty($forecast->alerts)) { ?>

            <ul class="weather-station-details-alerts">

                <?php

                    // Start the foreach loop to get hourly forecast
                    foreach($forecast->alerts as $alert):

                ?>

                <li><a href="<?php echo $alert->uri ?>"><?php echo $alert->title ?></a><br><span>expires <?php echo date("g:i a", $alert->expires) ?></span></li>

                <?php

                    // End the foreach loop
                    endforeach;

                ?>

            </ul>

        <?php } ?>

        <p class="weather-station-details-darksky"><a href="https://darksky.net/poweredby/" target="_blank">Powered by Dark Sky</a></p>
    </div>


</div>

<?php }

add_shortcode(\'go_weather_station\', \'weather_station\');
我打开了wp\\u debug,它向我显示了问题所在--我只是不确定修复它的最佳方法。错误消息为:

致命错误:无法在/www/wp-content/plugins/go-weather/go-weather.php:52中重新声明get\\u icon()。php在线52

后来我读到,我不应该在函数中声明函数,这是我在第52行所做的,但我尝试将其移动到没有任何效果(插件似乎根本不起作用)。如果有任何提示,我将不胜感激;我自己也会努力解决这个问题,但如果有人愿意给我提振的话,那就太棒了。非常感谢。

2 个回复
最合适的回答,由SO网友:Chetan Vaghela 整理而成

正如@TomJNowell所说,您绝不能嵌套这样的函数。因此,首先,你必须将这些函数移出短代码函数,并分别粘贴到插件的文件或活动主题函数中。php文件。其次,遵循法典所说的Shortcodes, 如果短代码生成大量HTMLob_start 可用于捕获输出并将其转换为字符串。我在页面中使用了两个短代码进行了测试,效果很好https://prnt.sc/qceew0

function weather_station(){
    ob_start();
    $coordinates = \'XXXX\'; //coordinates are here
    $api_key = \'XXXXXX\'; //api key is here
    $api_url = \'https://api.darksky.net/forecast/\'.$api_key.\'/\'.$coordinates;
    $cache_key = md5( \'remote_request|\' . $api_url );
    $forecast_request = get_transient( $cache_key );

    if ( false === $forecast_request ) {
        $forecast_request = wp_remote_get( $api_url );
        if ( is_wp_error( $forecast_request ) ) {
            // Cache failures for a short time, will speed up page rendering in the event of remote failure.
            set_transient( $cache_key, $forecast_request, 60 );
            return false;
        }
        // Success, cache for a longer time.
        set_transient( $cache_key, $forecast_request, 300 );
    }

    if ( is_wp_error( $forecast_request ) ) {
        return false;
    }
    $body = wp_remote_retrieve_body( $forecast_request );
    $forecast = json_decode( $body );
    //$forecast = json_decode(file_get_contents($api_url));

    $icon_currently = $forecast->currently->icon;
    $temperature_currently = round( $forecast->currently->temperature );
    $summary_hourly = $forecast->hourly->summary;

    // Set the default timezone
    date_default_timezone_set($forecast->timezone);

    ?>
    <div class="weather-station">
        <div class="weather-station-button">
            <span class="weather-station-icon"><?php echo get_icon($forecast->currently->icon) ?></span>
            <span class="weather-station-temperature-currently"><?php echo $temperature_currently ?>&deg;</span>
        </div>
        <div class="weather-station-details">
            <p class="weather-station-details-title">Forecast</p>
            <?php
                // Start the foreach loop to get hourly forecast
                foreach($forecast->daily->data as $day):
            ?>

            <p class="weather-station-details-temperature-range"><?php echo round($day->temperatureLow).\'&deg;/\'.round($day->temperatureHigh).\'&deg;\'; ?></p>
            <?php
                // Break because we got today\'s low/high
                break;
                // End the foreach loop
                endforeach;
            ?>
            <p class="weather-station-details-summary"><?php echo $summary_hourly ?></p>
            <?php if (!empty($forecast->alerts)) { ?>
                <ul class="weather-station-details-alerts">
                    <?php
                        // Start the foreach loop to get hourly forecast
                        foreach($forecast->alerts as $alert):
                    ?>
                    <li><a href="<?php echo $alert->uri ?>"><?php echo $alert->title ?></a><br><span>expires <?php echo date("g:i a", $alert->expires) ?></span></li>
                    <?php
                        // End the foreach loop
                        endforeach;
                    ?>
                </ul>
            <?php } ?>
            <p class="weather-station-details-darksky"><a href="https://darksky.net/poweredby/" target="_blank">Powered by Dark Sky</a></p>
        </div>
    </div>
    <?php 
   return ob_get_clean();
}
add_shortcode(\'go_weather_station\', \'weather_station\');

// Get the appropriate icon
function get_icon($icon) {
    if($icon===\'clear-day\') {
        $the_icon = \'<i class="fas fa-sun"></i>\';
        return $the_icon;
    }
    elseif($icon===\'clear-night\') {
        $the_icon = \'<i class="fas fa-moon-stars"></i>\';
        return $the_icon;
    }
    elseif($icon===\'rain\') {
        $the_icon = \'<i class="fas fa-cloud-showers-heavy"></i>\';
        return $the_icon;
    }
    elseif($icon===\'snow\') {
        $the_icon = \'<i class="fas fa-cloud-snow"></i>\';
        return $the_icon;
    }
    elseif($icon===\'sleet\') {
        $the_icon = \'<i class="fas fa-cloud-sleet"></i>\';
        return $the_icon;
    }
    elseif($icon===\'wind\') {
        $the_icon = \'<i class="fas fa-wind"></i>\';
        return $the_icon;
    }
    elseif($icon===\'fog\') {
        $the_icon = \'<i class="fas fa-fog"></i>\';
        return $the_icon;
    }
    elseif($icon===\'cloudy\') {
        $the_icon = \'<i class="fas fa-clouds"></i>\';
        return $the_icon;
    }
    elseif($icon===\'partly-cloudy-day\') {
        $the_icon = \'<i class="fas fa-clouds-sun"></i>\';
        return $the_icon;
    }
    elseif($icon===\'partly-cloudy-night\') {
        $the_icon = \'<i class="fas fa-clouds-moon"></i>\';
        return $the_icon;
    }
    elseif($icon===\'hail\') {
        $the_icon = \'<i class="fas fa-cloud-hail"></i>\';
        return $the_icon;
    }
    elseif($icon===\'thunderstorm\') {
        $the_icon = \'<i class="fas fa-thunderstorm"></i>\';
        return $the_icon;
    }
    elseif($icon===\'tornado\') {
        $the_icon = \'<i class="fas fa-tornado"></i>\';
        return $the_icon;
    }
    else {
        $the_icon = \'<i class="fas fa-thermometer-half"></i>\';
        return $the_icon;
    }
}

SO网友:Kaperto

您可以像这样分解“get\\u icon”函数

function get_icon($icon) {

    $names = [
        "clear-day" => "sun",
        "clear-night" => "moon-stars",
        "rain" => "cloud-showers-heavy",
        "snow" => "cloud-snow",
        "sleet" => "cloud-sleet",
        "wind" => "wind",
        "fog" => "fog",
        "cloudy" => "clouds",
        "partly-cloudy-day" => "clouds-sun",
        "partly-cloudy-night" => "clouds-moon",
        "hail" => "cloud-hail",
        "thunderstorm" => "thunderstorm",
        "tornado" => "tornado",
    ];

    $value = $names[$icon] ?? "thermometer-half";


    return "<i class=\\"fas fa-{$value}\\"></i>";

}

相关推荐

批量操作在单击时重定向到“options.php”页面(WP_LIST_TABLE)

我对WP\\U List\\U表格有问题。我正在学习一个关于互联网的教程(https://wpengineer.com/2426/wp_list_table-a-step-by-step-guide/) 因为我需要在插件页面上显示一个表。问题是批量操作不起作用,我已经尝试使用我刚才提到的链接提供的原始代码,并且我也尝试遵循另一个教程。但是,当我按下批量操作按钮时,wordpress会将我重定向到wp admin/options。php页面,无论我按哪个动作块。我不知道为什么会这样。