1

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?

0

3 Answers 3

2
<?php 
if (function_exists("insert_audio_player")) {
    insert_audio_player("[audio:".print_custom_field('tc_filename')."|titles=".print_custom_field('tc_title')."]"
} ?>

You can make this easier to read using sprintf()

<?php 
if (function_exists("insert_audio_player")) {
    insert_audio_player(sprintf(
        '[audio:%s|titles=%s]', 
        print_custom_field('tc_filename'), 
        print_custom_field('tc_title')
    ));
} 
?>    

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:

function get_custom_field($field) {
    ob_start();
    print_custom_field($field);
    return ob_get_clean();
}

And use

<?php 
if (function_exists("insert_audio_player")) {
    insert_audio_player(sprintf(
        '[audio:%s|titles=%s]', 
        get_custom_field('tc_filename'), 
        get_custom_field('tc_title')
    ));
} 
?> 
Sign up to request clarification or add additional context in comments.

10 Comments

I believe you're missing a closing ).
@Jacob: I believe I missed one as well. You were originally missing two. :)
@Jacob @jnpcl @AbiusX Thanks for your answers. Maybe it's a feature of wordpress or audio player plugin, but then I add any of your code i have tc_filename var before [audio:] tag =\ I don't understand why. Something like: test.mp3[audio:]
@alekseygr I've updated the answer to use output buffering, the print functions you use are echo data and not returning it.
@Jacob Thanks for your answer, but then I use your code I have PHP Parse error: syntax error, unexpected '} at the end of second part of code.
|
2

Edit: As stated by the OP, the print_custom_field() function uses echo rather than return, so this answer will not work for this particular situation. See @Jacob's answer for a better solution.

Try this:

<?php
    if (function_exists("insert_audio_player")) {
        insert_audio_player(
            "[audio:" . print_custom_field('tc_filename') .
            "|titles=" . print_custom_field('tc_title') . "]"
        );
    }
?>

1 Comment

Thanks for helping. Now trying to implement @Jacob's answer)
0
<?php if (function_exists("insert_audio_player")) {insert_audio_player("[audio:".function2()."|titles=".function3()."]"} ?>

then :

function function2()
{
print_custom_field('tc_filename');
}
function function3()
{
print_custom_field('tc_title');
}

5 Comments

you can add if and other logic that way.
perhaps you should mention that, and not use names like function2 and function3.
perhaps, but look at the question.
@AbiusX can you refer to what part of the question asks for that?
i meant the question is too basic too simply concat functions to the string! so i defined new functions for the questionee to understand better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.