title: Wordpress custom shortcode function with variable string (Newbie)
body: <p>I am trying to create a simple Wordpress shortcode based function that will take a defined number and convert it to the end user's local currency. Where I am stuck is how to search the post for the shortcode because the defined number is subject to change. If someone could please let me know how best to extract the number as a variable, I can then run it through the exchange rate function (which works fine, I have tested it with custom field stored data).</p>
  
  <pre><code>{customShortcode - priceAdditions [400]}
  </code></pre>
  
  <p>I have tried to <code>explode()</code> it around the <code>[]</code> and that seems to show promise but I can't work out how to extract it, also with the prospect of more than one instance of this being used with different numbers. I think <code>regex</code> or <code>preg_match</code> may be the way to go but I don't quite understand that just yet.</p>
  
  <p>If you need more info, please let me know. Thanks in advance.</p>
  
  <p>Dan</p>
  
  <p>Edit - The function for the shortcode that works - </p>
  
  <pre><code>$thePostContent =   get_the_content($post-&gt;ID); 
  $thePostContent =   str_replace('{customShortcode - price}',thePrice(),$thePostContent);
  </code></pre>
  
  <p>The function -</p>
  
  <pre><code>function thePrice(){
  $pageURL = 'http';
  if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
  $pageURL .= "://";
  if ($_SERVER["SERVER_PORT"] != "80") {
   $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  } else {
    $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  }
  $Exploded_URL = explode("/",$pageURL);
  if      ($Exploded_URL[4] == ''){$thePostIsIn   =   217;}
  elseif  ($Exploded_URL[4] == 'productA'){$thePostIsIn   =   347;}
  elseif  ($Exploded_URL[4] == 'productB'){$thePostIsIn   =   345;}
  else    {$thePostIsIn   =   217;}
  if      (empty($_COOKIE['userLocate']) || $_COOKIE['userLocate'] == 'US' || ($_COOKIE['userLocate'] != 'EU' &amp;&amp; $_COOKIE['userLocate'] != 'AU' &amp;&amp; $_COOKIE['userLocate'] != 'GB')){
      $currencyCode   =   'USD';
      $currencyPrefix =   '$';
  }
  elseif  ($_COOKIE['userLocate'] == 'EU'){
      $currencyCode   =   'EUR';
      $currencyPrefix =   '&amp;euro;';
  }
  elseif  ($_COOKIE['userLocate'] == 'AU'){
      $currencyCode   =   'AUD';
      $currencyPrefix =   'A$';
  }
  elseif  ($_COOKIE['userLocate'] == 'GB'){
      $currencyCode   =   'GBP';
      $currencyPrefix =   '&amp;pound;';
  }
  else {
      $currencyCode   =   'USD';
      $currencyPrefix =   '$';
  }
  
  $args=array(
  'post_type' =&gt;'page',
  'post__in' =&gt; array($thePostIsIn)
  );
  $recent_posts = get_posts($args);
  foreach( $recent_posts as $recent ){    
  
      $mypages        =   get_post( $recent-&gt;ID );
      $theBaseRate    =   get_post_meta($recent-&gt;ID, "Payment Cost",1);
  
      if(get_post_meta($recent-&gt;ID, "Payment Period",1)){
          $payPeriod  =   get_post_meta($recent-&gt;ID, "Payment Period",1);
      }
      else{
          $payPeriod  =   "per month";
      }
  
      $rssFeedUrl     =   "http://themoneyconverter.com/rss-feed/GBP/rss.xml";
      $rss            =   simplexml_load_file($rssFeedUrl);
      foreach($rss-&gt;channel-&gt;item as $feedItem){
          $currency   =   explode('/',$feedItem-&gt;title);
          if(strpos($currency[0], $currencyCode )!== false){
              $content    =   $feedItem-&gt;description;
              $content    =   explode('= ',$content);
              $content    =   substr($content[1],0,7);
              $theCost    =   $theBaseRate * $content;
              $theCost    =   number_format($theCost, 2, '.', '');
          }
      }
  }
  echo '&lt;p class="rentCost"&gt;&lt;span class="rentalCost"&gt;'.$currencyPrefix.$theCost.' '.$payPeriod.'&lt;/span&gt;&lt;/p&gt;&lt;div class="clear"&gt;&lt;/div&gt;';
  }
  </code></pre>