Ml Slider Shortcode

Below, you’ll find a detailed guide on how to add the Ml Slider 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 Ml Slider Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Ml Slider Plugin and the shortcodes it provides:

Plugin Icon
Slider, Gallery, and Carousel by MetaSlider – Responsive WordPress Slideshows

"Slider, Gallery, and Carousel by MetaSlider is a versatile WordPress plugin. It allows you to create stunning responsive slideshows, beautiful image galleries, and engaging carousels with ease."

★★★★☆ (682) Active Installs: 600000+ Tested with: 6.3.2 PHP Version: 7.0
Included Shortcodes:
  • [metaslider]

Ml Slider [metaslider] Shortcode

The MetaSlider shortcode is used to embed a slideshow in a WordPress page or post. It accepts parameters like ‘id’, ‘title’, ‘restrict_to’, and ‘theme’. If ‘id’ or ‘title’ is not provided, it exits. If ‘title’ is given, it fetches the corresponding ‘id’. It also allows restricting the slider to a specific page or the homepage. If the specified slider is not found or not published, it returns a comment.

Shortcode: [metaslider]

Parameters

Here is a list of all possible metaslider shortcode parameters and attributes:

  • id – The unique ID of the MetaSlider.
  • title – The title of the MetaSlider, used to fetch the ID.
  • restrict_to – Limits display of the MetaSlider to specific pages.
  • theme – The theme applied to the MetaSlider.

Examples and Usage

Basic example – Displaying a MetaSlider slideshow by referencing its ID.

[metaslider id=123 /]

Advanced examples:

Displaying a MetaSlider slideshow by referencing its title. The slideshow will load by title, and if not found, it will return false.

[metaslider title="My Slideshow" /]

Restricting a MetaSlider slideshow to a specific page by referencing the ID of the slideshow and the page. The slideshow will only display on the specified page.

[metaslider id=123 restrict_to="home" /]

Applying a specific theme to a MetaSlider slideshow by referencing the ID of the slideshow and the theme. The specified theme will be applied to the slideshow.

[metaslider id=123 theme="dark" /]

Combining multiple parameters to display a MetaSlider slideshow. The slideshow will load by title, apply a specific theme, and restrict its display to a specified page.

[metaslider title="My Slideshow" theme="dark" restrict_to="home" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [metaslider] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('metaslider', array($this, 'register_shortcode'));

Shortcode PHP function:

function register_shortcode($atts)
        {
            $atts = shortcode_atts(
                [
                    'id' => false,
                    'title' => false,
                    'restrict_to' => false,
                    'theme' => null
                ],
                $atts,
                'metaslider'
            );


            // If no id and no title, exit here
            if (! $atts['id'] && ! $atts['title']) {
                return false;
            }

            // If there is a title, get the id from the title
            $id = $atts['id'];
            if ($atts['title']) {
                global $wpdb;

                // Run a custom query because get_page_by_title() includes "trash" posts
                // Also, be sure just to get 1 post, in case they have multiple
                $id = $wpdb->get_var(
                    $wpdb->prepare(
                        "
                            SELECT ID
                            FROM $wpdb->posts
                            WHERE post_title = %s
                            AND post_type = 'ml-slider'
                            AND post_status = 'publish'
                            LIMIT 1
                        ",
                        $atts['title']
                    )
                );

                // If no posts were returned, $id will be NULL
                if (is_null($id) || ! (bool)$atts['title']) {
                    return false;
                }
            }

            // handle [metaslider id=123 restrict_to=home]
            if ('home' === $atts['restrict_to'] && ! is_front_page()) {
                return false;
            }
            if ($atts['restrict_to'] && 'home' !== $atts['restrict_to'] && ! is_page($atts['restrict_to'])) {
                return false;
            }

            // Attempt to get the ml-slider post object
            $slider = get_post($id);
            // check the slideshow is published and the ID is correct
            if (! $slider || 'publish' !== $slider->post_status || 'ml-slider' !== $slider->post_type) {
                return "<!-- MetaSlider {$atts['id']} not found -->";
            }

            // Set up the slideshow and load the slideshow theme
            $this->set_slider($id, $atts);
            MetaSlider_Themes::get_instance()->load_theme($id, $atts['theme']);
            $this->slider->enqueue_scripts();
            return $this->slider->render_public_slides();
        }

Code file location:

ml-slider/ml-slider/ml-slider.php

Conclusion

Now that you’ve learned how to embed the Ml Slider 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.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *