0

I have a shortcode that fetches some values, first_name, last_name and email from a remote database. I'm trying to write a shortcode that will allow me to print 2 or more of those values within output between shortcode tags. For example, I may call my shortcode like this:

[user_data]{{firist_name}} {{last_name}}'s email address is {{email}}[/user_data] 

or maybe

[user_data]The email address is {{email}} belongs to {{firist_name}} {{last_name}}.[/user_data] 

I know I can write a shortcode that prints 1 of the individual values and call it 3 times like this:

[user_data field="first_name"] [user_data field="last_name"]'s email address is [user_data field="email"] 

But that seems rather inefficient to me. Suppose i have the shortcode:

function user_data($atts, $content=null) { $a = shortcode_atts( array( 'uid' => 0 ), $atts ); // get an array with all of user's data $fetched_data = my_data_fetching_function($a['uid']); $out = $content; return $out; } 

Is there any way to make $fetched_data somehow available to $content?

4
  • 1
    the question can be phrased using pure generic PHP, no WP based solution is needed. It's just string manipulation. Loop through your fetched data, and for each key, do a search replace. You can do it using standard PHP string functions
    – Tom J Nowell
    CommentedSep 14, 2020 at 13:05
  • I understand the concept of looping through and doing a search and replace, but I"m not quite sure where I need to do it. Something like $content = str_replace('##' . $key . '##', $val, $content); return $content;? or am i way off?
    – Daveh0
    CommentedSep 14, 2020 at 13:16
  • That's headed in the right direction yes
    – Tom J Nowell
    CommentedSep 14, 2020 at 14:25
  • cool - i'll start writing some actual code and see how far i get. Thanks for the pointer
    – Daveh0
    CommentedSep 14, 2020 at 14:44

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.