John Davidson

php - Wordpress shortecode

0 comments
Message:


I am using myCred on my site. Specially myCred transfer. There is an existing shortcode for this particular plugin.
[mycred_transfer pay_to=1]
where pay_to is the recipient wordpress user id. I can only manually add a user id. But so I need to manually create a shortcode for every created user and it is no go :D.
so therefore I created this function to gather user id of the current post.


// Shortcode to output custom PHP
function author_id( $atts ) {
echo $user_id = get_the_author_meta( 'ID' );
}
add_shortcode( 'my_author_id', 'author_id');

and it works for the user ID. but do not know how to replace it "1" in [mycred_transfer pay_to=1]
because if I replace "1" with a shortcode it does not work.
Thanks for ideas



My Answer:
in the existing shortcode [mycred_transfer pay_to=1] with the dynamic user ID obtained from the author_id function.

You can modify the existing shortcode to use the dynamic user ID by replacing the static user ID with the shortcode attribute. Here's how you can do it:

php
// Shortcode to output custom PHP
function author_id( $atts ) {
$user_id = get_the_author_meta( 'ID' );
return '[mycred_transfer pay_to=' . $user_id . ']';
}
add_shortcode( 'my_author_id', 'author_id');


Now, when you use the [my_author_id] shortcode in your post or page, it will dynamically generate the [mycred_transfer pay_to=USER_ID] shortcode with the current post author's user ID.

Rate this post

4 of 5 based on 1977 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us