Below, you’ll find a detailed guide on how to add the Notificationx Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Notificationx Plugin shortcode not to show or not to work correctly.
Before starting, here is an overview of the Notificationx Plugin and the shortcodes it provides:
"NotificationX is a versatile plugin designed for FOMO, Social Proof, WooCommerce Sales Popups & Notification Bars. Seamlessly integratable with Elementor, it boosts customer engagement and sales."
- [notificationx_inline]
Notificationx [notificationx_inline] Shortcode
The NotificationX Inline shortcode is used to display notifications from the NotificationX plugin directly in your post or page content. This shortcode allows you to specify the ID of the notification to display. It checks if the notification is enabled and if the user has the right permissions to view it. If the settings match, it renders the notification. The shortcode also sorts the notifications based on their timestamp and replaces placeholders with actual data.
Shortcode: [notificationx_inline]
Parameters
Here is a list of all possible notificationx_inline shortcode parameters and attributes:
id
– The unique identifier for the notificationproduct_id
– The unique identifier for the product related to the notificationpost_type
– The type of the post related to the notificationshow_link
– Determines whether to display a link or not
Examples and Usage
Basic example – Display a specific notification by referencing its ID.
[notificationx_inline id=123 /]
Advanced examples
Display a specific notification by referencing its ID and disable the link for the notification.
[notificationx_inline id=123 show_link=false /]
Display a notification related to a specific product by referencing the product ID and the notification ID.
[notificationx_inline id=123 product_id=456 /]
Display a notification for a specific post type. Here ‘post_type’ can be any valid post type in your WordPress installation.
[notificationx_inline id=123 post_type='product' /]
PHP Function Code
In case you have difficulties debugging what causing issues with [notificationx_inline]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'notificationx_inline', array( $this, 'shortcode_inline' ), 999 );
Shortcode PHP function:
function shortcode_inline( $atts, $content = null ) {
$atts = shortcode_atts( array(
'id' => '',
'product_id' => '',
'post_type' => '',
'show_link' => true,
), $atts, 'notificationx_inline'
);
$nx_id = $atts['id'];
if ( empty( $nx_id ) ) {
if ( ! current_user_can( 'administrator' ) ) {
return;
}
return '<p class="nx-shortcode-notice">' . __( 'Choose a Notification from the dropdown.', 'notificationx-pro' ) . '</p>';
}
if ( ! PostType::get_instance()->is_enabled( $nx_id ) ) {
if ( ! current_user_can( 'administrator' ) ) {
return;
}
return '<p class="nx-shortcode-notice">' . __( 'Make sure you have enabled the notification which ID you have given.', 'notificationx-pro' ) . '</p>';
}
do_action( 'nx_inline' );
$settings = PostType::get_instance()->get_post($nx_id);
if($settings['type'] == 'inline'){
$settings['shortcodeinline'] = true;
/**
* @var WooInline|EDDInline
*/
$extension = \NotificationX\Extensions\ExtensionFactory::get_instance()->get($settings['source']);
$output = $extension->show_inline_notification( $atts, $settings);
if( !empty( $output ) ) {
$output = "<div id='notificationx-shortcode-inline-{$atts['id']}' class='notificationx-shortcode-inline-wrapper nx-shortcode-notice'>$output</div>";
}
return $output;
}
$result = FrontEnd::get_instance()->get_notifications_data( [ 'shortcode' => [ $nx_id ] ] );
$output = '';
if ( ! empty( $result['shortcode'][ $nx_id ]['entries'] ) ) {
$entries = $result['shortcode'][ $nx_id ]['entries'];
$entries = array_values( $entries );
$settings = $result['shortcode'][ $nx_id ]['post'];
$logged_in = is_user_logged_in();
$show_on_display = isset($settings['show_on_display']) ? $settings['show_on_display'] : '';
if ( ! ( ( $logged_in && 'logged_out_user' === $show_on_display ) || ( ! $logged_in && 'logged_in_user' === $show_on_display ) ) ) {
$col = array_column( $entries, 'timestamp' );
if ( count( $col ) == count( $entries ) ) {
array_multisort( $col, SORT_ASC, $entries );
}
$entry = end( $entries );
$template = Inline::get_instance()->get_template( $settings );
$_template = $template;
foreach ( $entry as $key => $val ) {
if ( ! is_array( $val ) ) {
if ( 'rating' === $key ) {
$count = $val;
$val = "<span style='white-space: nowrap'>";
for ( $i = 1; $i <= 5; $i++ ) {
$val .= $i <= $count
? '<span style="color:#ffc107">★</span>'
: '<span style="color:#eeeeee">★</span>';
}
$val .= '</span>';
} elseif (
! empty( $entry['link'] ) &&
in_array( $key, [ 'plugin_name', 'product_title', 'post_title', 'plugin_theme_name', 'course_title', 'title' ], true ) &&
'form' !== $settings['type'] &&
'email_subscription' !== $settings['type'] &&
'page_analytics' !== $settings['type']
) {
$link = $entry['link'];
$target = ! empty( $settings['link_open'] ) ? 'target="_blank"' : '';
if ( 'false' === $atts['show_link'] ) {
$target = '';
$link = 'javascript:void(0)';
}
$val = "<a href='$link' $target>$val</a>";
}
$_template = str_replace( "{{{$key}}}", $val, $_template );
}
if ( ! empty( $entry['timestamp'] ) && strpos( $_template, '{{time}}' ) !== false ) {
$timestamp = $entry['timestamp'];
if ( $timestamp ) {
$diff_for_humans = sprintf(
/* translators: time */
_x( '%s ago', 'Inline Shortcode', 'notificationx-pro' ),
human_time_diff( $timestamp )
);
$_template = str_replace( '{{time}}', $diff_for_humans, $_template );
}
}
}
$output .= $_template;
// $this->shortcode_nx_ids[] = $atts['id'];
$output = "<div id='notificationx-shortcode-inline-{$atts['id']}' class='notificationx-shortcode-inline-wrapper nx-shortcode-notice'>$output</div>";
}
}
return $output;
}
Code file location:
notificationx/notificationx/includes/Features/ShortcodeInline.php
Conclusion
Now that you’ve learned how to embed the Notificationx Plugin shortcode, 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