This step by step guide shows you how to develop your own add-on, allowing you to insert a dynamic HTML block in your emails with data pulled from an other plugin.
You may want to send to your users the latest products you have in store this month, the upcoming events they can book, or your latest articles built with an other plugin.
To do this, you can either copy your content title, image, description, etc... or you can use one of our add-ons.
If we don't have any add-on yet for the plugin you're using, you can create it using this guide, which will add a new block type in our email editor:
As an example, here is a simple WordPress post inserted in an email:
For this step by step guide, we will create an integration with WordPress posts that lets you:
search and insert a post
insert posts by category
Base file
Before continuing, make sure you created a custom add-on by following this guide:
If you return null in this method, your integration won't be shown. This gives you the possibility to add conditions here.
At this point your new block type should be visible in the editor if your custom plugin is activated:
To define the insertion options that will be shown in the right panel of the editor, you will need to add this use statement at the top of the plugin.php file:
You will need to modify multiple parts of this method:
line 4, we get the categories to be able to filter posts.
For plugins not using the WordPress category system, you will need to set an array of objects to the categories variable. Each object must have an id, parent_id and title.
if your plugin's main parent category ID is 0, you need to add $this->rootCategoryId = 0; into the add-on's construct method. This will help AcyMailing display the categories correctly on the listing
line 10 we define the most common options shown in the right panel, you may want to modify the lines 16 to 21 if you want to have more data to display in your email
lines 64 to 67, we define the possible columns used to order the posts when multiple ones are inserted by category
You will also need to add this method to prepare the listing showing the content you can insert:
line 3, we get the columns needed to show the content that can be inserted. You should always at least get the id and the title
line 4, we define in which table we get the content that can be inserted.
we can filter the content shown in the insertion options by adding conditions in the filter line 6. In this case, we chose to only show posts that are published
line 8, we get the columns on which we want the search field to be applied when searching for a content to insert
line 9, we define the column used to order the results. We chose to show the posts by id (recent posts first)
line 10, we define the table containing the information we need. We apply the alias "post" on the main table to make the query shorter
lines 11 and 38, we define the column name containing the id (often "id", but some plugins can have "ID", "content_id", etc...)
lines 16 and 17, we add a condition if a category is selected in the category filter. In this case, the category id of a file is stored in a relationship table so we need to join it
lines 23, 27 and 32 must use the column names that are selected on line 3. These will be shown on the posts listing. You can add as many columns as you want, but you should always make sure that the total size is always 12. In this example, the title has a size of 7 (line 25), the publishing date has a size of 4 (line 29) and the id has a size of 1 (line 34)
At this point you should see your insertion options on the right panel after dragging your new block type into an email:
Dynamically insert a content one by one in an email
Now that we have our insertion options, we can at the code inserting our content into the email. For this, add this method to your plugin.php file:
line 3, you need to make an SQL query that gets all the data you need to display in your email. The id of the selected content can be found in the variable $tag->id.
on line 15 you need to get the URL that shows the content on the front-end of your website. It will be added to the title and image of the inserted content
lines 22 to 48, we look at the options checked in $tag->display and prepare the data we want to display. All the options selected in the editor's insertion options are stored in $tag, and the data of the content to insert is is $element
lines 50 to 56, we create an object used by our code to automatically generate the HTML to insert in the email. Note that you could simply return the HTML you want, but using our code makes life easier to handle most of our options automatically (shorten the description, resize/remove images, add a link to the title/image, etc...)
You should now be able to insert a single element with your new block type:
Dynamically insert content by category in an email
Now that our custom add-on lets us insert posts one by one, we can make it insert them in bulk.
Start by modifying the method replaceContent that we previously added, and call the function replaceMultiple like this, just before calling replaceOne:
This basically builds an SQL query that gets the post IDs that should be inserted in the email, based on what you selected in the insertion options:
line 15 we select the id of the posts
on lines 21 and 22, we make sure only the posts in the selected categories are inserted. Since the category relationship is stored in an other table we need to adapt the query and join on this other table
lines 25 and 27 we make sure only published posts are inserted
the lines 28 to 38 are only useful if you want to use your integration with automatic campaigns. This makes sure that the auto-campaign is only sent if the minimum number of posts inserted is met, and that the same post isn't inserted in multiple campaigns
At this point your integration is ready and you should be able to insert the content either one by one or by category in your emails!