skip to Main Content

Hellow.
I’m not only a php beginner and but a wordpress beginner.
I try to integrate 3rd-party PHP file and Javascript (For Age Verification using Phone. i.e: SMS Certification) into my wordpress site.

this 3rd-party reference

Using ‘add_shortcode’, i managed to add ‘request function’ to my wordpress site. (Reference Url’s STEP 1)

    if (! function_exists ( 'register_mycustom_agecert_page' ) ) {
      function register_mycustom_agecert_page () {
    $mid ='**********';                                 // Given MID(Merchant ID)
    
    $apiKey ='********************************';   // apikey to MID
    $mTxId ='***********';   
    $reqSvcCd ='01';
    
    
    
      // Check if registered merchant or not.
      $plainText1 = hash("sha256",(string)$mid.(string)$mTxId.(string)$apiKey);
      $authHash = $plainText1;
    
    $userName = 'Anonymous';            // User name
    $userPhone = '01011112222';   // user Phone
    $userBirth ='19830000';           // user Birth
    
    $flgFixedUser = 'N';                // When fixing specific user, use below 'Y' setting.
    
    
    if($flgFixedUser=="Y")
    {
    $plainText2 = hash("sha256",(string)$userName.(string)$mid.(string)$userPhone.(string)$mTxId.(string)$userBirth.(string)$reqSvcCd);
    $userHash = $plainText2;
    }
    $foo = '';
    $foo .= '<div align="center" class="age-gate-wrapper">';
    $foo .= '<form name="saForm">';
    $foo .= '<input type="hidden" name="mid" value="' . $mid . '">';
    $foo .= '<input type="hidden" name="reqSvcCd" value="' . $reqSvcCd . '">';
    $foo .= '<input type="hidden" name="mTxId" value="' . $mTxId . '">';
    $foo .= '<input type="hidden" name="authHash" value="' . $authHash .'">';
    $foo .= '<input type="hidden" name="flgFixedUser" value="' . $flgFixedUser . '">';
    $foo .= 'input type="hidden" id="userName" name="userName"';
    $foo .= '<input type="hidden" id="userPhone" name="userPhone">';
    $foo .= '<input type="hidden" id="userBirth" name="userBirth">';
    $foo .= '<input type="hidden" name="userHash" value="' . $userHash . '">';
    $foo .= '<input type="hidden" name="directAgency" value="">';
    $foo .= '<input type="hidden" name="successUrl" value="' . esc_url( get_stylesheet_directory_uri() . '/kg/success.php' ) . '">';
    $foo .= '<input type="hidden" name="failUrl" value="'. esc_url( get_stylesheet_directory_uri() . '/kg/success.php' ) . '">';
    $foo .= '</form>';
    $foo .= '<button onclick="callSa()">Proceed to "Age Verification"</button>';
    $foo .= '</div>';
    
    echo $foo;
  }
  add_shortcode( 'register_mycustom_agecert_page', 'register_mycustom_agecert_page');    
}

callSa() script.

function callSa()
    {
        let window = popupCenter();
        if(window != undefined && window != null)
        {
            document.saForm.setAttribute("target", "sa_popup");
            document.saForm.setAttribute("post", "post");
            document.saForm.setAttribute("action", "https://sa.inicis.com/auth");
            document.saForm.submit();
        }
    }

    function popupCenter() {
        let _width = 400;
        let _height = 620;
        var xPos = (document.body.offsetWidth/2) - (_width/2); // Align center
        xPos += window.screenLeft; // For dual monitor

        return window.open("", "sa_popup", "width="+_width+", height="+_height+", left="+xPos+", menubar=yes, status=yes, titlebar=yes, resizable=yes");
    }

And then, i put success.php file in "childtheme-folder/kg/".
(Reference’s Step 2, 3)

success.php file.

<?php


// -------------------- recieving  --------------------------------------



extract($_POST);    

echo 'resultCode : '.$_REQUEST["resultCode"]."<br/>";
echo 'resultMsg : '.$_REQUEST["resultMsg"]."<br/>";
echo 'authRequestUrl : '.$_REQUEST["authRequestUrl"]."<br/>";
echo 'txId : '.$_REQUEST["txId"]."<br/><br/><br/>";

$mid ='********';    // Test MID. You need to replace Test MID with Merchant MID.
 
if ($_REQUEST["resultCode"] === "0000") { 
 $data = array(
    'mid' => $mid,        
    'txId' => $txId
     );

$post_data = json_encode($data);

// Start 'curl' 
$ch = curl_init();                                               
curl_setopt($ch, CURLOPT_URL, $_REQUEST["authRequestUrl"]);      
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);     
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
curl_setopt($ch, CURLOPT_POST, 1);                
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($ch);
curl_close($ch);

// -------------------- Recieve result -------------------------------------------

echo $response;


// Check If user age is under 20 years old or not.

   $pre_age_cert_result = json_decode( $response, true );

   if ( isset ($pre_age_cert_result) && ! empty ( $pre_age_cert_result['userBirthday'] ) ) {
          
      $pre_user_input_date = date ( 'Ymd', strtotime ($pre_age_cert_result['userBirthday']) );
      $user_input_date = new DateTime( $pre_user_input_date );
      $current_date = new DateTime();
      $user_input_date->add( new DateInterval( 'P20Y' ) );

      if ( $current_date > $user_input_date ) {

         $age_cert_check = true;
         $age_checking_msg = 'Age Verification: Success';

      } else {

         $age_cert_check = false;
         $age_checking_msg = 'Age Verification: Failed';

      }

   } else {

      $age_cert_check = false;
      $age_checking_msg = 'Some Problem.';

   }

   echo $age_checking_msg;

   // Add result of age cert into usermeta.

   if ( $age_cert_check === true ) {
      echo 'Success - Process 01';
      echo '<br>';
      if ( is_user_logged_in() ) {

         echo 'Success - Process 02';
         echo '<br>';
         $temp_current_user_id = get_current_user_id();
         echo $temp_current_user_id;

         if ( $temp_current_user_id !== 0 && $temp_current_user_id !== NULL ) {
            
            echo 'Success - Process 03';
            echo '<br>';
            $result_cur_time = date( 'Ymd', strtotime("now") );
            echo $result_cur_time;
            update_user_meta( $temp_current_user_id, 'ageCert', true );
            update_user_meta( $temp_current_user_id, 'ageCertDate', $result_cur_time );

         } else {

            echo 'Failure - Process 03';
            echo '<br>';
            return;

         }

      } else {

          echo 'Failure - Process 02';
          echo '<br>';
          $button_link_03 = esc_url( wp_login_url() );        
          $button_text_03 = esc_html( __( 'You Need to Login.', 'woocommerce' ) );
          echo '<a href="'. $button_link_03.'">'.$button_text_03.'</a>'; 
          return;              
       }

   } else {

      echo 'Failure - Process 01';
      echo '<br>';
      return;

   }
  

}else {   //  if resultCode===0000 is not, display below code.
echo 'resultCode : '.$_REQUEST["resultCode"]."<br/>";
echo 'resultMsg : '.$_REQUEST["resultMsg"]."<br/>";
}

?>

Finally i could get “echo ‘Success – Process 01”. But i can’t display neither ‘success -Process 02’ nor ‘Failure -Process 02’.
I think the cause of this is that success.php is not wordpress page. So success.php doesn’t have access to wordpress function.

How can i achive my goal?

If it’s not possible to fetch usermeta in custom page (like success.php), below can be option ?

  1. Using wordpress Rest-API, send value of $user_input_date. And hook somewhere, like wp_remote_get() ?
  2. Using alternative function which can get usermeta from wordpress.
  3. Grating access to wordpress to success.php page

I would appreciate any reference page or example Code.
Thank you for reading this long article.


Finally i achive my goal – thanks to IT goldman and Json.

i added below 2 lines into success.php

$custom_path = '/wp-load.php file path';
require_once( $custom_path . '/wp-load.php' );

It works !

2

Answers


  1. You can just include "wp-config.php"; as the first line of your php script then you get all wordpress functions, including get_user_meta.

    Login or Signup to reply.
  2. you use use wp_load.php on success.php file

    if your folder structure is like this (/wp-content/themes/child-theme/kg/success.php)

    in the success.php file add this line

    <?php
    
     include_once("../../../../wp-load.php");
    
    echo "test:: " . get_site_url();
    
    

    wp-load.php which loads all the functions and code for wordpress(bootstraps).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search