I have the following code in PHP:
<?php if (function_exists("insert_audio_player")) {insert_audio_player("[audio:|titles=]"} ?>
Which renders an audio player to my page in WordPress. I need to call a custom field inside this code. My custom field code is also coded in PHP:
<?php print_custom_field('tc_filename'); ?>
Something like:
<?php if (function_exists("insert_audio_player")) {insert_audio_player("[audio:<?php print_custom_field('tc_filename'); ?>|titles=<?php print_custom_field('tc_title'); ?>]"} ?>
How can I use or integrate the second block of code with the first?
You can make this easier to read using
sprintf()
Edit: based on your comment, the print_custom_field actually echo’s the field, and doesn’t return it, if there is no return function you can use, you need to use Output Buffering.
You can use a new function, which calls the print function but returns it instead of printing it to the screen:
And use
Edit: As stated by the OP, the
print_custom_field()
function usesecho
rather thanreturn
, so this answer will not work for this particular situation. See @Jacob’s answer for a better solution.Try this:
then :