Navid Onpage Navid Onpage - 3 years ago 262
HTML Question

How can I insert a php code into a shortcode in HTML?

I would like to add a php code into a shortcode code in HTML editor.
My shortcode looks like this:

<?php echo do_shortcode('[eapi keyword="KEYWORD" n=25]'); ?>

It works, if I put just a word instead of KEYWORD. Now I would like to add an individual title with this php code:

<?php the_title(); ?>

So I inserted it into the shortcode like this:

<?php echo do_shortcode('[eapi keyword="<?php the_title(); ?>
" n=25]'); ?>

But unfortunately it did not work. How can I successfully insert this php code into the shortcode?

Answer Source

Here is a possible solution but I am not familiar with the plugin/context you are using this in. But maybe this will give you some ideas:

<?php echo do_shortcode("[eapi keyword=\"" . the_title() . "\" n=25]"); ?>

So I removed the inner php tags for one. I switched the outer quotes to double quotes since you can't use functions/variables within single quotes. At the same time I concatenated the title function since I don't think it can be interpreted within the quotes. That also might negate the need for double quotes, but force of habit.

EDIT - Some other use examples outside of the original question for clarity.

Variable in the string requires double quotes:

<?php echo do_shortcode("[eapi keyword='{$variable}' n=25]"); ?>

Variable concatenated can use single or double quotes:

<?php echo do_shortcode("[eapi keyword='" . {$variable} . "' n=25]"); ?>

Keywords and functions must be concatenated:

<?php echo do_shortcode("[".KEYWORD." keyword='".func()."' n=25 x={$var}]"); ?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download