How to get variable from functions.php to page

Issue

I’m trying to get $cert_url and use it in my custom page to create a sharable link to Facebook. However, I have no idea to get the variable functions.php and pass it to custom page.

Functions.php

add_action( 'woocommerce_thankyou', 'get_cert_url' );
function get_cert_url( $order_id ) {
    global $wpdb;
    $msf_campaign_status = array('wc-completed');
    $msf_campaign_detail = get_post_meta($order_id);
    $msf_campaign_fund = $msf_campaign_detail['msf_charity_fund'][0];
    $order = wc_get_order( $order_id );
    if( $order->has_status('processing') ){
        $name_on_e_cert = str_replace(" ", "+", $msf_campaign_detail['name_on_e_cert'][0]);
        $msf_charity_name = get_option('wc_fields_billing');
        if($msf_campaign_detail['charity_organizations'][0] == 'msf_charity_1'){
            $charity_name = $msf_charity_name['charity_organizations']['options']['msf_charity_1'];
        }
        elseif ($msf_campaign_detail['charity_organizations'][0] == 'msf_charity_2') {
            $charity_name = $msf_charity_name['charity_organizations']['options']['msf_charity_2'];
        }
        elseif ($msf_campaign_detail['charity_organizations'][0] == 'msf_charity_3') {
            $charity_name = $msf_charity_name['charity_organizations']['options']['msf_charity_3'];
        }
        elseif ($msf_campaign_detail['charity_organizations'][0] == 'msf_charity_4') {
            $charity_name = $msf_charity_name['charity_organizations']['options']['msf_charity_4'];
        }
       $donateto = str_replace(" ", "+", $charity_name);
        $subtotal = $order->get_subtotal();
        $amount = (($subtotal*25)/100);
        $request = wp_remote_retrieve_body(wp_remote_get('https://www.api_website.com.my/api/c/product.ashx?key=api_key&orderid='.$order_id.'&name='.$name_on_e_cert.'&donateto='.$donateto.'&amount='.$amount.''));
        $data = json_decode($request);
    
        $result[]= $data;
        
        foreach($result[0] as $line) {
          $cert_url = $line;
        }

        return $cert_url;
    }
}

The code below is to create a share button to share the link to Facebook.

Custom page

<a href="https://www.facebook.com/sharer?u='.$cert_url.'&amp;t=Digital-Certificate" target="_blank" rel="noopener noreferrer">Share this on Facebook</a>

May I know how can I get the $cert_url and use it on custom page? Thanks in advance !

Solution

Thank you for your answer. I solved the problem by using add_query_arg().

Answered By – Boon

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published