是否可以在主题中自定义简码 - 或者 - 结合两个简码制作新的简码

is it possible to customize shortcode in theme -or- make new shortcode combining two shortcodes

如果我问的是愚蠢的问题,我很抱歉,因为我不是代码/php 专家?

我使用 gravityview 从值中筛选和显示重力。

[gravityview id="111" search_field="11" search_value="xyzes"]

现在我想将 search_value 动态更改为 "current logged in user"

所以我尝试制作新的 SHORTCODE 但效果不佳或不是好主意

add_shortcode( 'customcode' , 'wp_get_current_user_func' );

function wp_get_current_user_func( $atts ) { 
    $current_user = wp_get_current_user();

    return do_shortcode('[gravityview id="1111" search_field="16" search_value="$current_user = wp_get_current_user();"]');
}

我的请求请告诉我什么是正确的代码,或者在后端有什么方法可以改变主题文件吗?

非常感谢您

你已经足够接近了。以下是您应该如何操作:

add_shortcode( 'customcode' , 'wp_get_current_user_func' );

function wp_get_current_user_func( $atts ) { 
$current_user = wp_get_current_user();
$user_login = $current_user->user_login;
//return do_shortcode('[gravityview id="1111" search_field="16" search_value=".$user_login."]');
$shortcode = sprintf(
'[gravityview id="%1$s" search_field="%2$s" search_value="%3$s"]',
"1111",
"16",
$user_login
);
echo do_shortcode( $shortcode );
}

您未能从函数 wp_get_current_user()

中检索 $current_user->user_login
$user_login = $current_user->user_login;

您还可以像本例一样打印出其他值:

<?php
    $current_user = wp_get_current_user();
    /**
     * @example Safe usage: $current_user = wp_get_current_user();
     * if ( !($current_user instanceof WP_User) )
     *     return;
     */
    echo 'Username: ' . $current_user->user_login . '<br />';
    echo 'User email: ' . $current_user->user_email . '<br />';
    echo 'User first name: ' . $current_user->user_firstname . '<br />';
    echo 'User last name: ' . $current_user->user_lastname . '<br />';
    echo 'User display name: ' . $current_user->display_name . '<br />';
    echo 'User ID: ' . $current_user->ID . '<br />';
?>