admin管理员组文章数量:1124673
I have a shortcode from a plug-in called Pods that is used like
[pods-form name="user" id="" fields="my_field, my_field_2, my_field_3"]
Parameter name
contains the name of the custom post type (here: enhanced the WP user post type). Parameter id
now shall receive the user ID of the currently logged in user. The page containing this shortcode is only available to logged-in users. How to add the current user ID as a variable to this shortcode?
I'm looking for something like this
[pods-form name="user" id="{@user_ID}" fields="my_field, my_field_2, my_field_3"]
I have a shortcode from a plug-in called Pods that is used like
[pods-form name="user" id="" fields="my_field, my_field_2, my_field_3"]
Parameter name
contains the name of the custom post type (here: enhanced the WP user post type). Parameter id
now shall receive the user ID of the currently logged in user. The page containing this shortcode is only available to logged-in users. How to add the current user ID as a variable to this shortcode?
I'm looking for something like this
[pods-form name="user" id="{@user_ID}" fields="my_field, my_field_2, my_field_3"]
Share
Improve this question
asked Nov 22, 2016 at 21:27
BunjipBunjip
3719 silver badges20 bronze badges
2
|
1 Answer
Reset to default 1You didn't say how you were going to implement the code: If you are using page templates or custom post pages, you could do this:
<?php
$user_id = get_current_user_id();
if ($user_id == 0) {
echo 'You are currently not logged in.';
} else {
echo do_shortcode('[pods-form name="user" id="'.$user_id.'" fields="my_field, my_field_2, my_field_3"]');
}
?>
are you looking to add this after your content or before on all pages you could add this to your functions.php
function 247070_before_after($content) {
$user_id = get_current_user_id();
if ($user_id == 0) {
$formstatement = 'You are currently not logged in.';
} else {
$formstatement = do_shortcode('[pods-form name="user" id="'.$user_id.'" fields="my_field, my_field_2, my_field_3"]');
}
$addlcontent = $formstatement;
$fullcontent = $content . $addlcontent; //move $addlcontent in front of $content to add your from to the front.
return $fullcontent;
}
add_filter('the_content', '247070_before_after');
本文标签: pagesHow to insert current user ID in a shortcode
版权声明:本文标题:pages - How to insert current user ID in a shortcode? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736636455a1945894.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
id=@__current_user_id
andadd_filter('the_content', function($c){ return str_replace( '@__current_user_id', get_current_user_id(), $c ); });
or look atshortcode_atts_pods-form
filter if the plugin usesshortcode_atts()
to parse attributes. – Ismail Commented Nov 22, 2016 at 22:55