将动态短码值传递给PHP值

时间:2016-05-17 18:22:10

标签: php wordpress

此代码的目的是检索特定商店(用户)的测试结果。我们通过硬编码Store ID来检索这些数据,就像下面的第一个例子一样,但这不是一个可行的选择,因为我们有很多商店(用户)设置。我们现在需要动态打印登录用户的商店ID。

下面是手动硬编码的$ StoreId示例,目前适用于我们。但是,我需要使用短代码的值动态打印它。

 //***** Set the Store ID Here *****
$StoreId = 28;

下面的短代码将打印我需要的登录用户元数据。

[pcud-user-data f="surname"]

但是,当我将短代码插入到我的PHP代码中时,它不起作用。

//***** Set the Store ID Here *****    
$StoreId = echo do_shortcode( '[pcud-user-data f="store-id"]' );

要确认,短代码确实有效。如果我使用下面的代码将短代码添加到我的Wordpress页面模板的正文中,它会成功地将用户商店ID打印到页面上。但是,我不希望它在页面上可见,它需要打印到上面的第一个PHP示例中。

<?php echo do_shortcode( '[pcud-user-data f="store-id"]' );?>

我页面顶部的所有PHP代码都在下面。

<?php

/**
 * Template Name: Test Level
 *
 * @package Zephyr
 * @subpackage Test Results
 */

include("Functions/TestDataCalls.php");

//***** Set the Store ID Here *****
$StoreId = echo do_shortcode( '[pcud-user-data f="store-id"]' );

// Create the data access object
$DataCalls = new TestDataCalls();

// Get the form data object
$testDetails = $DataCalls->GetAllTests();

$us_layout = US_Layout::instance();
get_header();
us_load_template( 'templates/titlebar' );
?>

非常感谢您的帮助! :)

更新

短代码的代码如下。

// [pcud-user-data] 
//// print a specific user data
function pcud_data_shortcode( $atts, $content = null ) {
    global $pc_users;

    extract( shortcode_atts( array(
        'f' => ''
    ), $atts ) );

    $index = sanitize_title($f);
    $data = pc_user_logged($index);

    return $pc_users->data_to_human($index, $data);
}
add_shortcode('pcud-user-data', 'pcud_data_shortcode');

0 个答案:

没有答案