On a multisite environment, get the subdomain value as variable

I'm trying to setup a multisite where everytime a new site is added, a DNS record is also added in Cloudflare. So far, the code is working but I'm having troubles trying to parse the subdomain value onto a variable.

This is the code so far:

function create_cloudflare_dns_record() {

//$current_site = ''.$get_blog_details-siteurl.'';
//$current_site = ''.$substr( $current_site, -8).'';
//$current_site = ''.explode('.', $current_site).'';


/* Cloudflare.com | APİv4 | Api Ayarları */
$apikey     = '778***************************c37'; // Cloudflare Global API
$email      = 'al******@***********.com'; // Cloudflare Email Adress
$domain     = 'mer*****.com';  // zone_name // Cloudflare Domain Name
$zoneid     = 'ecc**********************1e1'; // zone_id // Cloudflare Domain Zone ID
//$current_site   = ''.$get_sites-path.'';
$current_site = explode('.', $_SERVER['HTTP_HOST']);


// $_SERVER['HTTP_HOST']

// A-record oluşturur DNS sistemi için.
$ch = curl_init(https://api.cloudflare.com/client/v4/zones/.$zoneid./dns_records);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, POST);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Auth-Email: '.$email.'',
'X-Auth-Key: '.$apikey.'',
'Cache-Control: no-cache',
// 'Content-Type: multipart/form-data; charset=utf-8',
'Content-Type:application/json',
'purge_everything: true'

));

// -d curl parametresi.
$data = array(

    'type' = 'CNAME',
    'name' = ''.$current_site.'',
    'content' = 'mer***.com',
    'zone_name' = ''.$domain.'',
    'zone_id' = ''.$zoneid.'',
    'proxied' = true,
    'ttl' = '120'
);

$data_string = json_encode($data);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data_string));

$sonuc = curl_exec($ch);

/*
    //print_r($sonuc);
*/

curl_close($ch);



}


add_action( 'wp_insert_site', 'create_cloudflare_dns_record' );

None of the three different attempts for $current_site at the beggining of the function is giving any result.

If I manually replace the value for $current_site, the code is working.

Any help is greatly appreciated.

Topic cloudflare dns multisite Wordpress

Category Web


wp_insert_site recieves WP_Site object, which contains details about the current site. https://developer.wordpress.org/reference/hooks/wp_insert_site/

do_action( 'wp_insert_site', WP_Site $new_site ) Fires once a site has been inserted into the database.

https://developer.wordpress.org/reference/classes/wp_site/

WP_Site Object (
    [blog_id] => 2
    [domain] => localhost
    [path] => /m2/sagres/
    [site_id] => 1
    [registered] => 2018-03-23 13:49:37
    [last_updated] => 2019-03-05 15:52:10
    [public] => 0
    [archived] => 0
    [mature] => 0
    [spam] => 0
    [deleted] => 0
    [lang_id] => 0 
)

So to summarize you can change your function definetion to accept argument.

function create_cloudflare_dns_record(WP_Site $new_site) {

And use $new_site->domain and $new_site->path to get details about the site inserted

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.