Lista senaste kampanjer från mailchimp

<?php

// Mailchimp API credentials
$api_key = 'YOUR_API_KEY';
$dc = 'YOUR_DC'; // Replace with your data center (e.g., 'us2')

// Mailchimp API URL to get sent campaigns
$url = "https://$dc.api.mailchimp.com/3.0/campaigns?status=sent";

// Initialize cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Authorization: Bearer $api_key",
    "Content-Type: application/json"
]);

// Execute cURL and get response
$response = curl_exec($ch);
curl_close($ch);

// Decode JSON response
$data = json_decode($response, true);

// Check for campaigns
if (!empty($data['campaigns'])) {
    echo "<h2>Sent Mailchimp Campaigns</h2><ul>";

    // Reverse the order so latest campaigns appear first
    $campaigns = array_reverse($data['campaigns']);

    foreach ($campaigns as $campaign) {
        $sent = date("Y-m-d H:i", strtotime($campaign['send_time'])); // Format date
        $title = htmlspecialchars($campaign['settings']['title']);
        $archive_url = htmlspecialchars($campaign['archive_url']);

        echo '<li><a href="' . $archive_url . '" target="_blank">' . $title . ' (' . $sent . ')</a></li>';
    }
    echo "</ul>";
} else {
    echo "<p>No sent campaigns found.</p>";
}
?>

Med senaste 30 dagarna, filter och sök:

<?php
// Mailchimp API credentials
$api_key = 'YOUR_API_KEY';
$dc = 'YOUR_DC'; // Replace with your data center (e.g., 'us2')

// Mailchimp API URL to get sent campaigns
$url = "https://$dc.api.mailchimp.com/3.0/campaigns?status=sent";

// Initialize cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Authorization: Bearer $api_key",
    "Content-Type: application/json"
]);

// Execute cURL and get response
$response = curl_exec($ch);
curl_close($ch);

// Decode JSON response
$data = json_decode($response, true);

// Define filters
$cutoff_date = strtotime("-30 days"); // Only show last 30 days
$search_query = isset($_GET['search']) ? strtolower(trim($_GET['search'])) : ''; // Get search term

// Pagination settings
$items_per_page = 5;
$current_page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$offset = ($current_page - 1) * $items_per_page;

// Filter and sort campaigns
$campaigns = array_reverse($data['campaigns']); // Latest first
$filtered_campaigns = [];

foreach ($campaigns as $campaign) {
    $sent_time = strtotime($campaign['send_time']);
    $title = htmlspecialchars($campaign['settings']['title']);
    $archive_url = htmlspecialchars($campaign['archive_url']);

    // Filter campaigns within the last 30 days and matching search
    if ($sent_time >= $cutoff_date && ($search_query === '' || stripos($title, $search_query) !== false)) {
        $filtered_campaigns[] = [
            'title' => $title,
            'archive_url' => $archive_url,
            'sent' => date("Y-m-d H:i", $sent_time) // Format date
        ];
    }
}

// Get total filtered items and slice for pagination
$total_campaigns = count($filtered_campaigns);
$paginated_campaigns = array_slice($filtered_campaigns, $offset, $items_per_page);

// Display search form
echo '<form method="GET" action="">
        <input type="text" name="search" value="' . htmlspecialchars($search_query) . '" placeholder="Search campaigns...">
        <button type="submit">Search</button>
      </form>';

if (!empty($paginated_campaigns)) {
    echo "<h2>Sent Mailchimp Campaigns (Last 30 Days)</h2><ul>";

    foreach ($paginated_campaigns as $campaign) {
        echo '<li><a href="' . $campaign['archive_url'] . '">' . $campaign['title'] . ' (' . $campaign['sent'] . ')</a></li>';
    }

    echo "</ul>";

    // Pagination controls
    $total_pages = ceil($total_campaigns / $items_per_page);
    if ($total_pages > 1) {
        echo "<div>";
        for ($i = 1; $i <= $total_pages; $i++) {
            $active = ($i == $current_page) ? 'style="font-weight:bold;"' : '';
            echo '<a ' . $active . ' href="?page=' . $i . '&search=' . urlencode($search_query) . '">Page ' . $i . '</a> ';
        }
        echo "</div>";
    }
} else {
    echo "<p>No campaigns found.</p>";
}
?>

Som shortcode

function get_mailchimp_campaigns($atts) {
    // Default shortcode parameters
    $atts = shortcode_atts(
        array(
            'api_key' => '', // API key as a parameter
            'dc' => '',      // Data center as a parameter
        ),
        $atts,
        'mailchimp_campaigns'
    );

    // Extract values from the shortcode attributes
    $api_key = $atts['api_key'];
    $dc = $atts['dc'];

    // Check if API key and data center are provided
    if (empty($api_key) || empty($dc)) {
        return '<p>Please provide the API key and data center in the shortcode.</p>';
    }

    // Mailchimp API URL to get sent campaigns
    $url = "https://$dc.api.mailchimp.com/3.0/campaigns?status=sent";

    // Initialize cURL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Authorization: Bearer $api_key",
        "Content-Type: application/json"
    ]);

    // Execute cURL and get response
    $response = curl_exec($ch);
    curl_close($ch);

    // Decode JSON response
    $data = json_decode($response, true);

    // Check for campaigns and prepare output
    if (!empty($data['campaigns'])) {
        $output = "<h2>Sent Mailchimp Campaigns</h2><ul>";

        // Reverse the order so latest campaigns appear first
        $campaigns = array_reverse($data['campaigns']);

        foreach ($campaigns as $campaign) {
            $sent = date("Y-m-d H:i", strtotime($campaign['send_time'])); // Format date
            $title = htmlspecialchars($campaign['settings']['title']);
            $archive_url = htmlspecialchars($campaign['archive_url']);

            $output .= '<li><a href="' . $archive_url . '">' . $title . ' (' . $sent . ')</a></li>';
        }

        $output .= "</ul>";
    } else {
        $output = "<p>No sent campaigns found.</p>";
    }

    return $output;
}
add_shortcode('mailchimp_campaigns', 'get_mailchimp_campaigns');

shortcode:

[mailchimp_campaigns api_key="your-api-key" dc="us6"]

Författare: Erik

Erik har jobbat med webb professionellt sedan 2008. Från 2005 till 2008 studerades webb på ING/JTH och dessförinnan skapades webb på all fritid. Första sajten byggdes någon gång mellan 1996-1998.