• Resolved sdoss

    (@sdoss)


    Let me start by saying the following: I am NOT a php programmer. However, I do write applications in other languages, and have created several websites using WordPress so am pretty familiar with that. I also understand programming principals, logic etc. So please, don’t be gentle.

    I have a web application written in Cobol (yes, Cobol) that works with a home-grown CGI wrapper. The app is an on-line order system. We took the front end, up to actually ordering something, and put it into WordPress to be used as a content management system. The WordPress pages consist only of images, text, and links behind the images to the next level of page display. This all works appropriately.

    I have figured out how to send an ‘order number’ from the Cobol/CGI app to WordPress – and have WordPress retain that value by passing from page to page via defining my own variable so that when WordPress queries the variables, it is brought into the page. It’s then output as a hidden variable to the next page, and the loop continues. This is now working very nicely.

    My problem is, I have to be able to pass that ‘order number’ from any of the WordPress pages back to the Cobol/CGI app. The Cobol app currently works based on a get method (first time into a page), then post for whatever action is taken on that page.

    Because the link to the Cobol app is behind the image on the WordPress page, as in

    <a href="http://aamorders.curreyadkins.com/aam1000.cgi?PAGE=3"></a>
    I need to either a) append an order number, as in

    a href="http://aamorders.curreyadkins.com/aam1000.cgi?PAGE=3&ORDER={some variable value}"></a>

    so the CGI will GET the value,
    OR, somehow, link back to the appropriate page (PAGE=3) and pass the order (some variable value).

    Maybe what I’m asking, is, is there a way to build a link on the fly and have it applied to a specific image on a WordPress page??

    Would it be easier to POST the page and order number on the WordPress page and have the CGI execute based on a POST?

    Please don’t hesitate to ask questions. This is very perplexing for the php noob 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter sdoss

    (@sdoss)

    Phil

    (@owendevelopment)

    I did something like this a few years ago, had to pass a variable from a post onto the end of a url.

    If memory serves, I did this by adding a custom field to the post, say ‘order_no’. This gets called in the template where you have a button or link which takes you to the other application/site, use something like:

    <a class="buttonlink" href="http://aamorders.curreyadkins.com/aam1000.cgi?PAGE=3&ORDER=/<?php getCustomField('order_no'); ?>">Order Item Here</a>

    You need to use getCustomField inside the loop though.

    You can also wrap it in a ‘If custom field exists … show it, else don’t show it’ etc.

    See more info here: http://codex.wordpress.org/Custom_Fields

    Phil

    Thread Starter sdoss

    (@sdoss)

    Phil, thanks for your response. I haven’t spent a lot of time on this, but, today I dedicated a few hours and here is what I’ve come up with.

    I’ve added into my functions.php file code to ‘register’ a query parm:

    add_filter('query_vars', 'lg_queryvars' );
    function lg_queryvars( $qvars ) {
      $qvars[] = 'ORDERNUMBER';
      return $qvars;
      }

    and, in the page template, added a global variable $ORDER_NUMBER and the following code:

    <?php $ORDER_NUMBER = $wp_query->query_vars['ORDERNUMBER'];
      echo $ORDER_NUMBER; ?>

    (the echo is just so I can see that I’m retreiving it)

    so that now, in the cgi I can craft a url that includes, at the end, the code ?ORDERNUMBER=12345 and the first wordpress page I go to can pick it up out of the url and display it. My thought is I’d like to keep the consistency in place, and simply pass the order number as part of the query string in all of the pages…

    Now, I’m trying to figure out how to append the ?ORDERNUMBER=12345 on each url after that.

    I’ve tried crafting the href as follows:

    <a href="<?php echo add_query_arg('ORDERNUMBER',$ORDER_NUMBER,'http://allamericanmulch.curreyadkins.com/products/landscaping/rubber-mulch/shredded/'); ?>">

    and the resulting html is broken, as when I view the source, I can see:

    <a href="<?php echo add_query_arg('ORDERNUMBER',$ORDER_NUMBER,'http://allamericanmulch.curreyadkins.com/products/landscaping/rubber-mulch/shredded/'); ?>“>

    with, of course, the ‘“’ resulting in the character ‘ ” ‘, and everything to the tag disappearing from the page 🙁

    The site is really quite simple, mostly images, with a link to another page under each image. The href code above was entered via the admin page; again, being a php dunce, I ask the following questions:

    a) does anyone familiar with the add_query_arg function see what I’ve done wrong above, and if so, could you give me a clue?

    b) can the php function be embedded in the href?

    and

    c) since I’m trying to keep the site simple, with only one page template, am I out of my bloody mind? (that’s just a lil’ frustration, there)

    Thanks in advance…..

    Thread Starter sdoss

    (@sdoss)

    Ok, so progress report…
    a) The add_query_arg function works just fine. If all three parameters are literal, and not variable, the function will append what you tell it to appropriately.
    b) No, you cannot embed your own php UNLESS you are using a plug-in that allows you to do so. To that end, I’ve applied the following plugin:

    Allow PHP in Posts and Pages

    and, indeed, it allows you to very easily put php code on your page.
    c) No, I’m not out of my mind…I’m just ignorant. So, here is where I now am:

    I’m trying to append a variable order number to a link on the page. The variable order number, ideally, will be passed from page to page via a url query parameter.

    As mentioned above, I can retrieve the order number quite easily in the page template and place it in what I believed was a global variable.

    So, I went to a page, and changed
    <a href="http://aamorders.curreyadkins.com/aam1000.cgi?PAGE=4">
    to
    <a href="http://aamorders.curreyadkins.com/aam1000.cgi?PAGE=4&ORDERNUMBER=[php]echo $ORDER_NUMBER[/php]">
    and voila – the code was broken, and I end up with an actual </br>(break), so no href.

    I can even change the php at the ‘echo’ part to utilize the add_query_arg function (in place of the global variable), and as long as everything in the function is literal, I’m ok. But, as soon as I change anything to be a variable, the html is broken.

    Again: php noob, html half-wit. But I have a good heart 🙂 If anyone has experienced this, whether or not in the Allow PHP in Posts and Pages plugin, please…..help?

    Thread Starter sdoss

    (@sdoss)

    Next update: after trying many, many, many different things, I’ve been able to retrieve the query parameter, and on each page, build the link to go to the next page and tack the query parameter on the end. Works ok (for a noob), and I’m sure there’s a much, much easier way to do this, but here’s what I did:

    On each page, (again, using the Allow PHP in Posts and Pages plugin) I inserted the following code:

    [php]global $order;
          global $href_1;
          $order=get_query_var(ORDERNUMBER);
          $href_1='"wood-mulch?ORDERNUMBER='.$order.'">';
     [/php]

    which sets up the variables I need and pulls the value of the order in from the url. Then, in the html portion of the page, I changed the hardcoded url to:

    <a href=[php]global $href_1;global $order;echo $href_1;[/php]</a>

    (with all the other stuff that goes with the href tag). This worked great, and we were all doing the happy dance in our chairs.

    Then, we decided that gosh! The first time into the site, the user won’t HAVE an order number, so the links really shouldn’t include the ?ORDERNUMBER= language. So, back to the editor, and we changed the code to this:

    [php]global $order;
          global $href_1;
          $order=get_query_var(ORDERNUMBER);
          if ($order=="") {
            $href_1='"wood-mulch"';
          } else {
            $href_1='"wood-mulch?ORDERNUMBER='.$order.'">';
          }
     [/php]

    and, immediately, everything is broken. We decided that the php code, as php code, is correct (and Please! tell me if I’m wrong), so, thought maybe the ‘}’ was botching things, and changed to escape it using the ‘\’ backslash. No luck.

    So, removed the if statement, and placed it into a ‘snippet’, along with the global variable definition. Still no luck.

    (sigh)….so, so close….

    Thread Starter sdoss

    (@sdoss)

    So, this morning, life is simply grand. And, as an html half-wit, I’ve learned the value of paying attention to closing tags (or lack thereof).

    Following is (if anyone wants to know) my solution to building a link on a page on the fly:

    1) Install a plug-in that allows the use of php code in pages/posts. I used this one:
    Allow PHP in Posts and Pages
    2) Add a function to your functions.php file that will register any url query variables you may need:

    add_filter('query_vars', 'function_name' );
    function function_name( $array_name ) {
      $array_name[] = 'query_var_1';
      return $array_name;
      }

    Here is a link to documentation for this function: add_filter
    3)On your page, under the html tab of the wysiwyg editor, using the plug-in syntax, and before your html code, put:

    [php]
    global $temp_var;
    global $href_1;
    global $href_2;
    $href_1='"http://yourdomain.com/linktopage1';
    $href_2='"http://yourdomain.com/linktopage2';
    $temp_var=get_query_var('query_var_1');
    if ($temp_var == "") {
      $href_1 = $href_1 . '">';
      $href_2 = $href_2 . '">';
      } else
        {
          $href_1 = $href_1 . '?query_var_1=' . $temp_var . '">';
          $href_2 = $href_2 . '?query_var_1=' . $temp_var . '">';
        }
    [/php]

    This defines the variables you will be using, sets your ‘on-the-fly’ links to a beginning value, pulls in the value of ‘query_var_1’ from the url (if present) and places it in $temp_var, then, if $temp_var is empty, closes the a href portion of the link. If $temp_var is not empty, it concatenates the query_var_1 name, =, and the value it pulled from the url, and closes the a href portion of the link. Here’s the get_query_var function.

    Then, in your html, where you open your href tag:

    <a href=[php]global $order; global $href_1; echo $href_1;[/php]
    <plus other stuff like src, etc </a>

    This code simply echoes what you formatted up above.

    I am very sure that there is an easier way to do this. I just couldn’t find a post anywhere that said, “do it like this!”.

    Maybe it will help someone else. It’s amazing what a good night’s sleep and a strong cup of coffee will do for you. 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Passing a variable value from WordPress pages to an external cgi application’ is closed to new replies.