How to pass data between wordpress pages

前端 未结 2 726
情歌与酒
情歌与酒 2020-12-10 10:11

I am new to wordpress and i want to transfer data from one page to another in wordpress. I used php to post my form data to a wordpress page. My code is:

<         


        
相关标签:
2条回答
  • 2020-12-10 10:33

    Having spent about a day figuring out how to do this, I thought it might be helpful to others in the stackoverflow community.

    GOAL: To take the results from a form field and use it on another page. In this example, my client wanted users to be able to fill in any amount in a text field to make a Paypal payment. I'm using S2Member plugin to create buttons and hook up the Paypal payments, but they did not offer this functionality --only buttons with hard wired amounts. You can see this in action here:
    http://virginiabloom.com/?page_id=250 . But it is LIVE. If you start a paypal payment, it WILL deduct money from your account. Which will make Virginia very happy. HOW I SOLVED IT: First, I created a form just for this field. Enter Other Payment Amount $

    Next, I found a very simple plugin on stackoverflow and adapted it to my needs:

    <?php
    /*
    Plugin name: redirect on post
    Desciption: 
    
    http://stackoverflow.com/questions/13686245/how-to-create-a-custom-url-based-on-dropdown-in-wordpress-form-submission
    I-changed-dropdown-to-field-input
    */ 
    function redirect_on_submit() {
      // check if the post is set
      if (isset($_POST['amount']) && ! empty ($_POST['amount'])) 
    
    {
        header( "Location: yourUrl.com/?
    
    page_id=542&amount=" . $_POST['amount'] );
      }
    }
    add_action('init', redirect_on_submit);
    

    NOTE: Change the page URL and ID information in the header to point to the Wordpress page you want to send the information to. If you don't know how to manually install a plugin, its very simple. Save the snippet as a .php file. Open up your hosting account and find the plugins folder (its in the wp-content folder) Copy the file into the folder. Go back to Wordpress and look at your plugins. You should see it there. If its not activated, the activate it.

    I installed another plugin: Allow PHP in Posts and Pages (Version 3.0.4)

    After installing it, the plugin will appear on the Wordpress menu. Open it and you can use their snippet maker that will put php into your post inside a short code. Configuration:
    Show the snippet not found message: yes (for debugging) Use the old (pre.2.2 code: no Use the advanced filter method: yes Remove all plugin data on install: no You should see the code snippet box appear at the bottom of the page. My snippet was simple. I just wanted to echo the "amount" parameter from the page url. So it looked like this:

    echo $_GET['amount']?>
    

    Note that you have to put the ending php tag, but not the beginning tag. Now, you just use the shortcode [php function=1] on the page wherever you want to get this parameter.

    Here's what it looks like on MY results page.

    <h2>Payment of $ [php function=1]</h2>
    <strong><em>Click Paypal button to start payment or cancel.</em></strong>
    [s2Member-PayPal-Button sp="1" ids="xxx" exp="24" desc="Payment" ps="paypal" lc="" cc="USD" dg="0" ns="1" custom="yoururl.com"
    ra="[php function=1]" image="default" output="button" /]
    

    To make sure that users can only enter in decimal numbers, I used jQuery validation on the form. There are other ways to do it, but this was very simple. Just put this code on the page with the form which I've included below.

     <form id="myform" action="http://virginiabloom.com/?page_id=542" method="post"><span style="font-size:1.5em;">Enter Other Payment Amount $</span><input id="amount" class="left" name="amount" type="text" /><input type="submit" value="Submit" /></form>
    
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://jquery.bassistance.de/validate/jquery.validate.js"></script>
    <script src="http://jquery.bassistance.de/validate/additional-methods.js"></script>
    <script>
    $( "#myform" ).validate({
      rules: {
        amount: {
          required: false,
          number: true
        }
      }
    });
    </script>
    

    That's it! I hope this helps somebody.

    0 讨论(0)
  • 2020-12-10 10:37

    If you need to go to one of your WordPress page, please don't put the direct link in action. Instead of that put the below php code.

    <form method='post' action='<?php bloginfo('url'); ?>/your-page-name/' > 
    

    If you wants to go to a particular file inside your template directory put the below code

    <form method='post' action='<?php bloginfo('template_url'); ?>/your-page.php' > 
    

    Check this WordPress codex page for more about bloginfo.

    <?php bloginfo('url'); ?> 
    

    will generate the base site url.

    0 讨论(0)
提交回复
热议问题