How to insert current user ID in a shortcode?

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"]

Topic shortcode variables pages Wordpress

Category Web


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');

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.