I am not sure if there is a simple solution to my problem. The requirement looks simple but unable to find answer even after breaking my head for 6 hours. I have to pass variable from a php function to a shortcode and this shortcode has to pass on the this variable to another shortcode. Shortcode-1 will open a popup and the content of the popup is created by shortcode-2, content has to be changed based on the variable. How can I acheive this?

Code in my template..

 <a href="#" class="open-popup-1" variable= <?php getid(); ?>"> <?php  echo "Click here"?> </a> 

popup shortcode:

[modifycontent id=$variable] 

My shortcode function

function modifycontent($atts = [], $content = null)
{
// do something to $content

 $var = atts['id']; 

 $content = gettherightcontent($var); 
 return $content;
}
add_shortcode('mc', 'modifycontent');

I tried like this, I managed to call my shortcode but unable to pass the variable.

  • show us what you've tried. This is possible, but we need to have a place start. – rudtek Aug 9 '17 at 19:27
  • okay. adding an answer. I'm not sure if I completely understood, but i'll clarify if I missed, so let me know. – rudtek Aug 9 '17 at 21:15
  • Thanks for corrections and sorry for not being clear. My problem is in passing the value from html to shortcode. In the example, I have mentioned html code (href) calls shortcode [open-popup] which calls my shortcode [mc]. [open-popup] shortcode is provided by plugin Popupbuilder. What works now, is when I click on "Click here" my shortcode[mc] gets executed. What I am not able to do is pass the variable from my template(html code) to my shortcode[mc] using popupbuilder in between. – Geo Aug 10 '17 at 8:38

I see a few errors in your code...

  1. on the html, there was no quotation in front of variable

  2. on the php you say you were calling the short code [modifycontent] but by your php you should have been using [mc].

All the same, here's how I would do it:

function modifycontent ($atts, $content = null ) {
    $a = shortcode_atts( 
    array(
         'id' => '1',
        ), 
      $atts );

    return '<a href="#" class="open-popup-1" variable="'.$a['id'].'">Click here</a>';
}
add_shortcode( 'mc-shortcode', 'modifycontent' );

this would use the short code [mc-shortcode] to return:

<a href="#" class="open-popup-1" variable="1">Click here</a>

If you used this for the short code: [mc-shortcode id='1']

it would return:

<a href="#" class="open-popup-1" variable="2">Click here</a>

(Notice the variable changes)

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.