admin管理员组

文章数量:1122832

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.

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.

Share Improve this question edited Aug 9, 2017 at 20:27 Geo asked Aug 9, 2017 at 19:18 GeoGeo 111 silver badge3 bronze badges 3
  • show us what you've tried. This is possible, but we need to have a place start. – rudtek Commented Aug 9, 2017 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 Commented Aug 9, 2017 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 Commented Aug 10, 2017 at 8:38
Add a comment  | 

1 Answer 1

Reset to default 0

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)

本文标签: functionsPass php dynamic variable to shortcode