【问题标题】:Wordpress Shortcode not passing variable from function (Newbie)Wordpress Shortcode 未从函数传递变量(新手)
【发布时间】:2015-02-02 06:59:40
【问题描述】:

这是有问题的代码的sn-p:

function hoursearned_func($user) {
    $key = 'Service Hours Earned';
    if(isset($user[$key])) {
        $result = $user[$key];
    }
return "Service Hours Earned: ".$result." Hours";
}
add_shortcode('hoursearned', 'hoursearned_func');

这个小函数只是简单地返回给定键的数组的值,但是当调用短代码 [hoursearned] 时,它只显示:

Service Hours Earned: Hours

而不是:

Service Hours Earned: 200 Hours

其中 200 是一个字符串,由给定 $user 的 hoursearned_func 函数返回。

由于某种原因,Wordpress 没有将函数返回的变量传递给简码。如何让 Wordpress 显示函数的结果?

如果有帮助,这里是完整的代码...(请注意,所有这些都是作为插件加载的)

//Dump CSV file (SalesForce Report) to php array to be parsed..
add_action('dump_csv_array', 'dump_csv_array', 10);
function dump_csv_array(){
    $data = array();
    $header = NULL;
    if (($file = fopen('http://blah.com/info.csv', 'r')) !==FALSE) { //The link to the CSV is insignificant
        while (($row = fgetcsv($file, 1000, ",")) !==FALSE) {
            if(!$header)
                $header = $row;
            else
                $data[] = array_combine($header, $row);
            }
    fclose($file);
    }
    return $data;
}

$multiarray = (array) dump_csv_array();
global $current_user;
get_currentuserinfo();
$user_email = $current_user->user_email;

//Parses the multidimensional array of Dumped CSV info. Looks up the current user's email and returns a single array of the current user's SalesForce information.  
function parse_array($multiarray, $user_email){
    $array = (array) $multiarray;
    $email = $user_email;       
        if(is_user_logged_in() === true ) {
            foreach($array as $subarray) {
                if(isset($subarray['Email']) && $subarray['Email'] == $email) {
                    $data = $subarray;
                }   
            }
        }
        else {
            return NULL;
        }
return $data;
}

$user = parse_array($multiarray, $user_email);

//Defines Shortcodes and their Functions
function hoursearned_func($user) {
    $key = 'Service Hours Earned';
    if(isset($user[$key])) {
        $result = $user[$key];
    }
return "Service Hours Earned: ".$result." Hours";
}


function hoursawarded_func($user) {
$key = 'Service Hours Awarded';
    if(isset($user[$key])) {
        $result = $user[$key];
    }
return "Service Hours Awarded: ".$result." Hours";
}

function rank_func($user) {
$key = 'Rank';
    if(isset($user[$key])) {
        $result = $user[$key];
    }
return "Rank: ".$result;
}

function memstatus_func($user) {
    $key = 'Membership Status';
    if(isset($user[$key])) {
        $result = $user[$key];
    }
return "Status: ".$result;
}

function register($atts) {
    add_shortcode( 'hoursearned', 'hoursearned_func');
    add_shortcode( 'hoursawarded', 'servicehours_func');
    add_shortcode( 'rank', 'rank_func');
    add_shortcode( 'memstatus', 'memstatus_func');
}
add_action('init', 'register');
var_dump(hoursearned_func($user));

【问题讨论】:

  • 您的变量$result 似乎没有设置。传递到$user 的内容是否包含键为“获得的服务时间”的数组?
  • @Fleuv 是的,$user 是一个数组。例如var_dump($user) 将返回:array (size=10) 'First Name' => string 'John' (length=6) 'Last Name' => string 'Doe' (length=6) 'Email' => string 'spammanabc@website.com' (length=21) 'Phone' => string '(123) 456-7890' (length=14) 'Grad Year' => string '2000' (length=4) 'School' => string 'School' (length=7) 'Service Hours Earned' => string '200' (length=3) 'Service Hours Awarded' => string '10' (length=1) 'Rank' => string 'MasterChief' (length=7) 'Membership Status' => string 'Active Member' (length=13)
  • @Fleuv 我也不确定 $result 没有被设置(对不起,我对 php 非常陌生)。既然我已经在函数中声明了 $result ,难道不应该设置它吗?

标签: php wordpress shortcode


【解决方案1】:

尝试创建带有属性的简码,(或)检索简码函数内的$user 数据并将它们设置为默认值。

function example_func($atts) {
    $user = get_user_array(); //Retrieve the data
    $atts = shortcode_atts($user, $atts);
    return 'example: ' . $atts['foo'] . ' ' . $atts['bar'];
}
add_shortcode('example', 'example_func');

看看Shortcode API,它基本上解释了一切。

【讨论】:

  • 非常感谢!通过使用global $user 在简码函数中调用$user,简码可以传递返回的变量。
猜你喜欢
  • 1970-01-01
  • 2018-11-30
  • 2022-09-23
  • 2015-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-09
相关资源
最近更新 更多