2

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?

2
  • I think the problem is that you are starting with a single quote which makes the contents literal and prevents you from using variables. You may want to use double quotes and then a backslash before any of the quotes inside. Commented Dec 22, 2017 at 1:41
  • you already started php open tag and close tag, issue is you again started php open tag <?php echo do_shortcode('[eapi keyword="<?php the_title(); ?> " n=25]'); ?> try this <?php echo do_shortcode('[eapi keyword=".$title."]' ) ?> Commented Dec 22, 2017 at 13:11

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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}]"); ?>

5 Comments

If i want to add a shortcode of a similar plugin, which looks like this: <?php echo do_shortcode('[amazon bestseller="KEYWORD" items="10"]'); ?> How can i implement it like you did it above? I tried this: <?php echo do_shortcode('[amazon bestseller=\"" . the_title() . "\" items="10"]'); ?> . but it did not work :/
For the above code you are having opening quote as single and closing as double
<?php echo do_shortcode("[amazon bestseller=\'" . the_title() . "\' items='10']"); ?> Jang Answer is perfect
Hey, thank you very much for the reply. Unfortunately the code : <?php echo do_shortcode("[amazon bestseller=\'" . the_title() . "\' items='10']"); ?> did not work. I am overasked, what else i can add to make this work. I tried to replace the ". the title()" into ". get_the_title()" but it did not work as well
I don't think I am understanding correctly. Are you putting a period after get_the_title() and before the next double quote?
0

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.

Comments

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.