【问题标题】:Wordpress - Assign the value return by do_shorcode() to a variableWordpress - 将 do_shorcode() 返回的值分配给变量
【发布时间】:2017-06-06 06:41:02
【问题描述】:

我正在使用 IP2Location Tags 插件通过 IP 地址获取用户的位置。我想将 do_shorcode() 返回的值分配给 PHP 变量。我试过这段代码:

<?php
$region = do_shortcode('{ip:regionName}');     
echo ord($region);
?>

并使用ord() 打印ASCII 值。 echo ord($region); 返回 ASCII 值 123(即 { )。在得到$region 中每个字符的ASCII 值后,我知道$region 包含值{ip:regionName},但是在使用echo $region; 时会打印Maharashtra

所以我的问题是如何将Maharashtra 作为$region; 中的值,这将有助于我比较字符串。

【问题讨论】:

    标签: php wordpress plugins


    【解决方案1】:

    由于这个插件使用过滤器替换关键字,do_shortcode 什么都不做。我很惊讶您会通过回显 $region 来获得马哈拉施特拉邦,但如果您在 the_content 中回显它,则可能会应用过滤器。

    要获取位置,您只需从插件中调用类并调用必要的函数即可。

    <?php
        $ip2l = new IP2LocationTags();
        $ip2l_location = $ip2l->get_location($_SERVER['REMOTE_ADDR']);
        $region = $ip2l_location['regionName'];
    ?>
    

    $region 将是 regionName。

    $ip2l_location 将包含整个位置值,包括 countryCode、countryName 等,它们存储在关联数组中。

    【讨论】:

    • 非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多