Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Ojoma a11n

    (@geraltrivia)

    Hello there @plord12 ,

    Thank you for your question and for checking our guide on creating a custom shortcode. I understand you’d like to go a step further by extracting specific data from a post contained in your email.

    While our guide shows how to create a custom shortcode, extracting post-specific details like event dates usually requires working with the post’s metadata. The exact approach will depend on how the event details are stored in your site. For example, if you are using an events plugin, the start date and time might be saved as custom fields (post meta) or provided through that plugin’s own functions.

    To achieve this, you’d likely need to extend the shortcode code from our article so it can:

    1. Identify the post or event you want to pull the data from.
    2. Retrieve the start date and time from the correct post meta or plugin function.
    3. Output that information in your email where the shortcode is placed.

    Since this involves working with PHP and potentially a specific events plugin’s API, it may require the help of a developer to write or adjust the code for your setup. Our guide can serve as the base, but the event date and time retrieval logic would need to be added based on how your events are stored.

    I hope this is helpful.

    Thread Starter plord12

    (@plord12)

    Many thanks for your response.

    The first issue is how can I use the mailpoet API to identify the (first) post contained in the email. I was expecting to use something like :

    function mailpoet_custom_shortcode($shortcode, $newsletter, $subscriber, $queue, $newsletter_body, $arguments) {
    if ($shortcode !== '[custom:date]') return $shortcode;

    // whats the API to return the first post contained in the newsletter ?
    $firstpost = $newsletter->getPosts()[0];

    // once we've got access to the first post, probabally need to check
    // its an event and then find the event's metadata
    $startdate = $firstpost->getStartDate();

    return $startdate;
    }

    Or perhaps I need to get the first post’s id and find it in the database ?

    Thanks.

    • This reply was modified 8 months ago by plord12.
    Plugin Support Ojoma a11n

    (@geraltrivia)

    Hello again @plord12 ,

    Thank you so much for your thoughtful follow-up and for sharing the code example you’re working on. I completely understand why you’d like to pull the first post from a newsletter and use its metadata in your shortcode.

    MailPoet’s API does not currently provide a direct method like $newsletter->getPosts() to access post objects within a newsletter. The posts are stored as content inside the newsletter body rather than as objects that can be programmatically queried in that way. Because of this, there isn’t a built-in API call you can use to directly extract the first post and its metadata when generating a shortcode.

    The approach you mentioned, retrieving the post ID from the database and then using WordPress functions like get_post_meta() to fetch the event metadata, would likely be the right path forward. Since this involves custom development and integration with the specific events plugin you’re using, it goes beyond what we’re able to support directly under our support policy.

    If you’re comfortable with PHP, you could extend the shortcode by parsing the newsletter content to find the first post’s ID and then use WordPress functions to fetch the event start date or other custom fields. Alternatively, a developer familiar with MailPoet and your events plugin should be able to help you craft a solution tailored to your setup.

    If you need help with custom code, we recommend reaching out to someone from Codeable (affiliate link).

    I’m really sorry we can’t provide a ready-made code fix here, but I hope this clarifies the limitations and points you in the right direction for building out the functionality.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘custom shortcode extract data from post selection’ is closed to new replies.