Below, you’ll find a detailed guide on how to add the Newsletter Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Newsletter Plugin shortcodes not to show or not to work correctly.
Before starting, here is an overview of the Newsletter Plugin and the shortcodes it provides:
"Newsletter – Send Awesome Emails from WordPress Plugin is an excellent tool for creating, sending, and managing emails right from your WordPress dashboard. Stay connected with your audience effortlessly."
- [newsletter]
- [newsletter_replace]
- [newsletter_profile]
- [newsletter_form]
- [newsletter_field]
Newsletter [newsletter] Shortcode
The ‘newsletter’ shortcode is used to display newsletter-related messages on a WordPress site. The function ‘shortcode_newsletter’ handles the shortcode’s behavior. It prevents loops by checking if it’s already executing. It fetches the user and message key from the request, then allows modules to provide their own text. It also replaces placeholders in the message with actual user and email data. If an ‘alert’ request is present, it appends a JavaScript alert to the message. The function then returns the final message to be displayed.
Shortcode: [newsletter]
Examples and Usage
Basic example – The following shortcode will display the newsletter based on the user and email details fetched from the request.
[newsletter]
PHP Function Code
In case you have difficulties debugging what causing issues with [newsletter]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('newsletter', array($this, 'shortcode_newsletter'));
Shortcode PHP function:
function shortcode_newsletter($attrs, $content) {
static $executing = false;
// To avoid loops
if ($executing) {
return '';
}
$executing = true;
$message_key = $this->get_message_key_from_request();
if ($message_key === 'confirmation') {
$user = $this->get_user_from_request(false, 'preconfirm');
} else {
$user = $this->get_user_from_request();
}
// Lets modules to provie its own text
$message = apply_filters('newsletter_page_text', '', $message_key, $user);
$message = do_shortcode($message);
$email = $this->get_email_from_request();
$message = $this->replace($message, $user, $email, 'page');
if (isset($_REQUEST['alert'])) {
// slashes are already added by wordpress!
$message .= '<script>alert("' . esc_js(strip_tags($_REQUEST['alert'])) . '");</script>';
}
$executing = false;
return $message;
}
Code file location:
newsletter/newsletter/plugin.php
Newsletter [newsletter_replace] Shortcode
‘newsletter_replace’ is a shortcode that modifies the content of the newsletter. It fetches user and email details from the request and replaces the content accordingly.
Shortcode: [newsletter_replace]
Examples and Usage
Basic example – Utilizes the shortcode to replace placeholders in the content with user-specific details.
[newsletter_replace attr="value"]Your content here[/newsletter_replace]
Advanced examples:
Using the shortcode to replace placeholders in the content with user and email details. The details are fetched from the request and replaced in the content.
[newsletter_replace user="username" email="user@email.com"]Hello {user}, Your email is {email}[/newsletter_replace]
Another advanced example involves using the shortcode to replace multiple placeholders in the content. This can be useful when you want to personalize newsletter content for each recipient.
[newsletter_replace user="username" email="user@email.com" first_name="John" last_name="Doe"]Hello {first_name} {last_name}, Your username is {user} and your email is {email}[/newsletter_replace]
PHP Function Code
In case you have difficulties debugging what causing issues with [newsletter_replace]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('newsletter_replace', [$this, 'shortcode_newsletter_replace']);
Shortcode PHP function:
function shortcode_newsletter_replace($attrs, $content) {
$content = do_shortcode($content);
$content = $this->replace($content, $this->get_user_from_request(), $this->get_email_from_request());
return $content;
}
Code file location:
newsletter/newsletter/plugin.php
Newsletter [newsletter_profile] Shortcode
‘newsletter_profile’ shortcode is a tool used to fetch the profile form of a subscriber. It initially checks if the user exists. If not, it either returns a message stating ‘Subscriber not found’ or the content within the shortcode. If the user is found, it returns the profile form of the user.
Shortcode: [newsletter_profile]
Examples and Usage
Basic example – Displays the newsletter profile of the current user.
[newsletter_profile /]
PHP Function Code
In case you have difficulties debugging what causing issues with [newsletter_profile]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('newsletter_profile', [$this, 'shortcode_newsletter_profile']);
Shortcode PHP function:
function shortcode_newsletter_profile($attrs, $content) {
$user = $this->check_user();
if (empty($user)) {
if (empty($content)) {
return __('Subscriber not found.', 'newsletter');
} else {
return $content;
}
}
return $this->get_profile_form($user);
}
Code file location:
newsletter/newsletter/profile/profile.php
Newsletter [newsletter_form] Shortcode
`newsletter_form` is a shortcode that generates different newsletter subscription forms based on the attributes provided. It supports minimal type, custom forms using shortcodes, hand-coded forms saved in options, and new syntax forms. If no specific form is defined, it returns a default subscription form.
Shortcode: [newsletter_form]
Parameters
Here is a list of all possible newsletter_form shortcode parameters and attributes:
type
– decides the style of the form, ‘minimal’ for a basic design.content
– allows customization of the form with [newsletter_field] shortcodes.form
– loads a custom form saved in the custom forms option.number
– alternative syntax for loading a custom form.
Examples and Usage
Basic example – A simple newsletter form can be embedded in your post or page using the ‘newsletter_form’ shortcode without any attributes.
[newsletter_form /]
Advanced examples
Display a minimalistic newsletter form. This form will have a simple, clean design with minimal elements, suitable for a clean and modern website design.
[newsletter_form type="minimal" /]
Display a custom form by using the ‘form’ attribute. Here, you can specify the form number which you have created and saved in your newsletter plugin options.
[newsletter_form form=2 /]
Another way to display a custom form is by using the ‘number’ attribute. This is similar to the ‘form’ attribute and it also refers to the form number that you have created and saved in your newsletter plugin options.
[newsletter_form number=3 /]
Lastly, you can create a custom form by specifying the content within the shortcode. This will allow you to customize the form fields according to your needs.
[newsletter_form]Your custom form content here[/newsletter_form]
PHP Function Code
In case you have difficulties debugging what causing issues with [newsletter_form]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('newsletter_form', array($this, 'shortcode_newsletter_form'));
Shortcode PHP function:
function shortcode_newsletter_form($attrs, $content) {
if (isset($attrs['type']) && $attrs['type'] === 'minimal') {
return $this->get_subscription_form_minimal($attrs);
}
// Custom form using the [newsletter_field] shortcodes
if (!empty($content)) {
return $this->get_subscription_form_custom($attrs, $content);
}
// Custom form hand coded and saved in the custom forms option
if (isset($attrs['form'])) {
return $this->get_form((int) $attrs['form']);
}
// Custom hand coded form (as above, new syntax)
if (isset($attrs['number'])) {
return $this->get_form((int) $attrs['number']);
}
return $this->get_subscription_form(null, null, $attrs);
}
Code file location:
newsletter/newsletter/subscription/subscription.php
Newsletter [newsletter_field] Shortcode
The ‘newsletter_field’ shortcode is used to create custom fields in your newsletter form. It provides flexibility to add fields such as email, first name, last name, preferences, gender, and custom fields. This PHP code enables the creation of unique IDs for each field, handles field validation, and manages the display of these fields. It also allows for the customization of field attributes like placeholder and button label. In addition, it handles the newsletter preferences, allowing users to select from multiple lists or preferences. The code also manages the privacy checkbox, ensuring users agree to your terms before subscribing.
Shortcode: [newsletter_field]
Parameters
Here is a list of all possible newsletter_field shortcode parameters and attributes:
id
– Unique identifier for each field in the form.name
– Determines the type of field to be displayed (e.g., email, first_name, surname, preference, list, lists, sex, profile, privacy).placeholder
– Text displayed in the field before user input.button_label
– The text displayed on the submit button.number
– Specifies the list number for ‘preference’ or ‘list’ name, or the custom field number for ‘profile’ or ‘customfield’ name.hidden
– If set, the field will be hidden.checked
– If set, the checkbox will be pre-checked.label
– The label displayed for the field.layout
– Determines the layout of the lists (e.g., dropdown).first_option_label
– The label for the first option in a dropdown list.size
– Specifies the size of the input field for profile custom fields.url
– The URL for the privacy policy page.
Examples and Usage
Basic example – Display an email input field with a placeholder text
[newsletter_field name="email" placeholder="Enter your email here"]
Advanced examples
Display a name input field with a custom label and make it required
[newsletter_field name="first_name" placeholder="Enter your first name here" button_label="Your First Name" required]
Display a dropdown list of newsletter preferences
[newsletter_field name="preferences" layout="dropdown" first_option_label="Select your preference"]
Display a gender selection field with custom labels for the options
[newsletter_field name="sex" sex_none="Prefer not to say" sex_female="Female" sex_male="Male"]
Display a custom field input with a specific number and placeholder
[newsletter_field name="customfield" number="1" placeholder="Enter your custom info here"]
Display a privacy checkbox with a custom label and link to the privacy policy
[newsletter_field name="privacy" label="I agree to the privacy policy" url="https://www.example.com/privacy-policy/"]
PHP Function Code
In case you have difficulties debugging what causing issues with [newsletter_field]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('newsletter_field', array($this, 'shortcode_newsletter_field'));
Shortcode PHP function:
function shortcode_newsletter_field($attrs, $content = '') {
// Counter to create unique ID for checkbox and labels
static $idx = 0;
$idx++;
$attrs['id'] = 'tnp-' . $idx;
$name = $attrs['name'];
$buffer = '';
if ($name == 'email') {
$buffer .= '<div class="tnp-field tnp-field-email">';
$buffer .= $this->_shortcode_label('email', $attrs);
$buffer .= '<input class="tnp-email" type="email" name="ne" id="' . esc_attr($attrs['id']) . '" value=""';
if (isset($attrs['placeholder'])) {
$buffer .= ' placeholder="' . esc_attr($attrs['placeholder']) . '"';
}
$buffer .= ' required>';
if (isset($attrs['button_label'])) {
$label = $attrs['button_label'];
$buffer .= ' <input class="tnp-submit" type="submit" value="' . esc_attr($label) . '" style="width: 29%">';
}
$buffer .= "</div>\n";
return $buffer;
}
if ($name == 'first_name' || $name == 'name') {
$buffer .= '<div class="tnp-field tnp-field-firstname">';
$buffer .= $this->_shortcode_label('name', $attrs);
$buffer .= '<input class="tnp-name" type="text" name="nn" id="' . esc_attr($attrs['id']) . '" value=""';
if (isset($attrs['placeholder'])) {
$buffer .= ' placeholder="' . esc_attr($attrs['placeholder']) . '"';
}
if ($this->get_form_option('name_rules') == 1) {
$buffer .= ' required';
}
$buffer .= '>';
$buffer .= "</div>\n";
return $buffer;
}
if ($name == 'last_name' || $name == 'surname') {
$buffer .= '<div class="tnp-field tnp-field-surname">';
$buffer .= $this->_shortcode_label('surname', $attrs);
$buffer .= '<input class="tnp-surname" type="text" name="ns" id="' . esc_attr($attrs['id']) . '" value=""';
if (isset($attrs['placeholder'])) {
$buffer .= ' placeholder="' . esc_attr($attrs['placeholder']) . '"';
}
if ($this->get_form_option('surname_rules') == 1) {
$buffer .= ' required';
}
$buffer .= '>';
$buffer .= '</div>';
return $buffer;
}
// Single list
if ($name == 'preference' || $name == 'list') {
if (!isset($attrs['number'])) {
return $this->build_field_admin_notice('List number not specified.');
}
$number = (int) $attrs['number'];
$list = $this->get_list($number);
if (!$list) {
return $this->build_field_admin_notice('List ' . $number . ' is not configured, cannot be shown.');
}
if ($list->status == 0 || $list->forced) {
return $this->build_field_admin_notice('List ' . $number . ' is private or enforced cannot be shown.');
}
if (isset($attrs['hidden'])) {
return '<input type="hidden" name="nl[]" value="' . esc_attr($list->id) . '">';
}
$idx++;
$buffer .= '<div class="tnp-field tnp-field-checkbox tnp-field-list"><label for="tnp-' . $idx . '">';
$buffer .= '<input type="checkbox" id="tnp-' . $idx . '" name="nl[]" value="' . esc_attr($list->id) . '"';
if (isset($attrs['checked'])) {
$buffer .= ' checked';
}
$buffer .= '> ';
if (isset($attrs['label'])) {
if ($attrs['label'] != '') {
$buffer .= esc_html($attrs['label']) . '</label>';
}
} else {
$buffer .= esc_html($list->name) . '</label>';
}
$buffer .= "</div>\n";
return $buffer;
}
// All lists
if ($name == 'lists' || $name == 'preferences') {
$list_ids = $this->get_form_option('lists');
if (!empty($list_ids)) {
$checked_ids = $this->get_form_option('lists_checked');
if (isset($attrs['layout']) && $attrs['layout'] === 'dropdown') {
$buffer .= '<div class="tnp-field tnp-lists">';
// There is not a default "label" for the block of lists, so it can only be specified in the shortcode attrs as "label"
$buffer .= $this->_shortcode_label('lists', $attrs);
$buffer .= '<select class="tnp-lists" name="nl[]" required>';
if (!empty($attrs['first_option_label'])) {
$buffer .= '<option value="" selected="true" disabled="disabled">' . esc_html($attrs['first_option_label']) . '</option>';
}
foreach ($list_ids as $list_id) {
$list = $this->get_list($list_id);
if ($list && $list->is_private()) {
continue;
}
$buffer .= '<option value="' . esc_attr($list->id) . '">' . esc_html($list->name) . '</option>';
}
$buffer .= '</select>';
$buffer .= '</div>';
} else {
$buffer .= '<div class="tnp-field tnp-lists">';
// if (!empty($attrs['label'])) {
// $buffer .= '<p>' . $attrs['label'] . '</p>';
// }
foreach ($list_ids as $list_id) {
$list = $this->get_list($list_id);
if ($list && $list->is_private()) {
continue;
}
$idx++;
$buffer .= '<div class="tnp-field tnp-field-checkbox tnp-field-list"><label for="nl' . $idx . '">';
$buffer .= '<input type="checkbox" id="nl' . $idx . '" name="nl[]" value="' . esc_attr($list->id) . '"';
if (in_array($list_id, $checked_ids)) {
$buffer .= ' checked';
}
$buffer .= '> ' . esc_html($list->name) . '</label>';
$buffer .= "</div>\n";
}
$buffer .= '</div>';
}
}
return $buffer;
}
if ($name === 'sex' || $name === 'gender') {
$buffer .= '<div class="tnp-field tnp-field-gender">';
$buffer .= $this->_shortcode_label('sex', $attrs);
$buffer .= '<select name="nx" class="tnp-gender" id="tnp-gender"';
if ($this->get_form_option('sex_rules')) {
$buffer .= ' required ';
}
$buffer .= '>';
if ($this->get_form_option('sex_rules')) {
$buffer .= '<option value=""></option>';
}
$buffer .= '<option value="n">' . esc_html($this->get_form_text('sex_none')) . '</option>';
$buffer .= '<option value="f">' . esc_html($this->get_form_text('sex_female')) . '</option>';
$buffer .= '<option value="m">' . esc_html($this->get_form_text('sex_male')) . '</option>';
$buffer .= '</select>';
$buffer .= "</div>\n";
return $buffer;
}
if ($name === 'profile' || $name === 'customfield') {
if (!isset($attrs['number'])) {
return $this->build_field_admin_notice('Extra profile number not specified.');
}
$number = (int) $attrs['number'];
$profile = $this->get_customfield($number);
if (!$profile) {
return $this->build_field_admin_notice('Custom field ' . $number . ' is not configured and cannot be shown.');
}
if ($profile->is_private()) {
return $this->build_field_admin_notice('Custom field ' . $number . ' is private and cannot be shown.');
}
$size = isset($attrs['size']) ? $attrs['size'] : '';
if (!isset($attrs['label'])) {
$attrs['label'] = $profile->name;
}
$buffer .= '<div class="tnp-field tnp-field-profile">';
$buffer .= $this->_shortcode_label('profile_' . $profile->id, $attrs);
$placeholder = isset($attrs['placeholder']) ? $attrs['placeholder'] : $profile->placeholder;
// Text field
if ($profile->type == TNP_Profile::TYPE_TEXT) {
$buffer .= '<input class="tnp-profile tnp-profile-' . $number . '" id="tnp-profile_' . $number . '" type="text" size="' . esc_attr($size) . '" name="np' . $number . '" placeholder="' . esc_attr($placeholder) . '"';
if ($profile->is_required()) {
$buffer .= ' required';
}
$buffer .= '>';
}
// Select field
if ($profile->type == TNP_Profile::TYPE_SELECT) {
$buffer .= '<select class="tnp-profile tnp-profile-' . $number . '" id="tnp-profile_' . $number . '" name="np' . $number . '"';
if ($profile->is_required()) {
$buffer .= ' required';
}
$buffer .= '>';
if (!empty($placeholder)) {
$buffer .= '<option value="" selected disabled>' . esc_html($placeholder) . '</option>';
}
foreach ($profile->options as $option) {
$buffer .= '<option>' . esc_html(trim($option)) . '</option>';
}
$buffer .= "</select>\n";
}
$buffer .= "</div>\n";
return $buffer;
}
if (strpos($name, 'privacy') === 0) {
if (!isset($attrs['url'])) {
$attrs['url'] = $this->get_privacy_url();
}
if (!isset($attrs['label'])) {
$attrs['label'] = $this->get_form_text('privacy');
}
$buffer .= '<div class="tnp-field tnp-field-checkbox tnp-field-privacy">';
$idx++;
$buffer .= '<input type="checkbox" name="ny" required class="tnp-privacy" id="tnp-' . $idx . '"> ';
$buffer .= '<label for="tnp-' . $idx . '">';
if (!empty($attrs['url'])) {
$buffer .= '<a target="_blank" href="' . esc_attr($attrs['url']) . '">';
}
$buffer .= $attrs['label'];
if (!empty($attrs['url'])) {
$buffer .= '</a>';
}
$buffer .= '</label>';
$buffer .= '</div>';
return $buffer;
}
}
Code file location:
newsletter/newsletter/subscription/subscription.php
Conclusion
Now that you’ve learned how to embed the Newsletter Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply