Reputation: 23
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?
Upvotes: 2
Views: 3130
Reputation: 418
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}]"); ?>
Upvotes: 0
Reputation: 2982
Use something like
<?php do_shortcode('[eapi keyword="' . get_the_title() . '"]'; ?>
You'll want get_the_title because it will return the title instead of directly outputting it (as the_title will). As you are already in PHP mode, you don't need any additional
Upvotes: 1
Reputation: 164
I think that you need to modify your eapi plug in to support your requirement.
For example, you can make a rule on your eapi plug to analysis the parameter's keyword.
1) if it is a simple string, return directly
2) if it is a string as similar as "{{{......}}}", use eval function to run it as a php script and then return.
Upvotes: 0