Creating a custom rebrandly link using a wordpress shortcode

I am trying to create a rebrandly short link using a WordPress shortcode. Here’s what I have so far:

add_shortcode( 'shortlink', 'rebrandly_shortcode');
function rebrandly_shortcode( $content = null ) {
    
$domain_data["fullName"] = "rebrand.ly";
$post_data["destination"] = $content;
$post_data["domain"] = $domain_data;
$ch = curl_init("https://api.rebrandly.com/v1/links");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  "apikey: 67c55f8b4e5d4f079cfc866b2b80b4f5",
  "Content-Type: application/json"
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
$result = curl_exec($ch);
curl_close($ch);
$response = json_decode($result, true);

return "Short URL is: " . $response["shortUrl"];
    
}

Following the directions for PHP here – https://developers.rebrandly.com/docs

The shortcode I’m testing is [shortlink]https://example.com[/shortlink] but its not working.

When I change the part: $post_data["destination"] = $content; to $post_data["destination"] = "http://example.com"; it works.

I must be putting in the shortcode content wrong. Any ideas?

Leave a Comment