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
  • You might use a custom string and filter it later, e.g id=@__current_user_id and add_filter('the_content', function($c){ return str_replace( '@__current_user_id', get_current_user_id(), $c ); }); or look at shortcode_atts_pods-form filter if the plugin uses shortcode_atts() to parse attributes. – Ismail Commented Nov 22, 2016 at 22:55
  • In the specification of the shortcode provided by the Pods plugin, there is a parameter id already in existence. However, examples in the documentation only show static values for this parameter, whereas I want to insert the appropriate user's ID dynamically – Bunjip Commented Nov 23, 2016 at 9:10
Add a comment  | 

1 Answer 1

Reset to default 1

You 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