• Resolved emsiledock

    (@emsiledock)


    Hi,
    First i want to say that i love your plugin. It’s almost perfect, just one thing:
    It seems like, it’s impossible to load content with some shortcodes. WordPress Playlist shortcode doesn’t work for example (audio do). Playlist shows an empty audio tag. The same thing occurs with custom shortcodes, [waveplayer] for example. Is that normal ? Is that a way to hook into he plugin to make things work ?

    If it is a future update for the premium users, can you tell us when it will be available ? I’ll pay :). Please i can’t wait too long… I need my shortcodes for a kickass social networking platform i’m working on… 😀

    Anyway, Best regards

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Sébastien Dumont

    (@sebd86)

    The playlist shortcode requires Javascript which loads in the footer of the site for you if the shortcode is used.

    As the site has already loaded the initial post and that initial post does not have the shortcode in use, then the Javascript is not loaded.

    You would have to load the Javascript required for the playlist shortcode yourself so the shortcode is supported.

    You can find the source for the Javascript here: https://core.trac.ww.wp.xz.cn/browser/trunk/src/wp-includes/media.php#L1871

    Here is a tutorial that might help you.

    https://gomakethings.com/how-to-conditionally-load-stylesheets-and-javascript-in-wordpress-only-if-a-shortcode-is-used/

    There is also documentation for Auto Load Next Post. https://github.com/seb86/Auto-Load-Next-Post/wiki

    Thread Starter emsiledock

    (@emsiledock)

    Thank you very much !! Your links were very helpful, i am know able to see a playlist with your amazing plugin 😀

    Plugin Author Sébastien Dumont

    (@sebd86)

    Nice. Could you please share what you did to get the playlist shortcode working so I can add it to the documentation for others?

    Thread Starter emsiledock

    (@emsiledock)

    Sure !

    In functions.php inside the function dedicated to include scripts i put:

    if( is_single() ) {
        wp_enqueue_script('backbone'); // BackboneJS required library
        wp_enqueue_style( 'wp-mediaelement' ); // CSS for playlist and medias
    }

    In functions.php

    /**
     * If the site has already loaded the initial post and that initial post.. 
     * does not have the playlist shortcode in use, then the Javascript is not loaded.
     * 
     * We have to load the Javascript, CSS and template files manually for playlist shortcode to work..
     * with auto_load_next_post
     * 
     * @param   -
     * @return  -
    */
    
    function manage_playlist_shortcode_js() {
      // Get the $post object
      global $post;
    
      // Only load scripts and styles when the post contains our special shortcodes 
      if ( !is_a( $post, 'WP_Post' ) || !has_shortcode( $post->post_content, 'playlist' ) ) return;
      ?>
      <script>
        ;function insertAfter(newNode, referenceNode) {
          referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
        }
    
        var WpMediaElementCSS = document.getElementById('wp-mediaelement-css');
    
        var utilScript= document.createElement('script');
        utilScript.type= 'text/javascript';
        utilScript.src= '<?php echo network_site_url() . "/" . WPINC ; ?>/js/wp-util.min.js';
    
        var mediaElementScript= document.createElement('script');
        mediaElementScript.type= 'text/javascript';
        mediaElementScript.src= '<?php echo network_site_url() . "/" . WPINC ; ?>/js/mediaelement/mediaelement-and-player.min.js';
    
        var playlistScript= document.createElement('script');
        playlistScript.type= 'text/javascript';
        playlistScript.src= '<?php echo network_site_url() . "/" . WPINC ; ?>/js/mediaelement/wp-playlist.min.js';
    
        insertAfter( utilScript, WpMediaElementCSS );
        insertAfter(mediaElementScript, utilScript);
        // Avoid Asynchronous issues
        setTimeout(function(){ insertAfter( playlistScript, mediaElementScript ); }, 3000);
      </script>
      <?php
    }
    /* This code is exactly the same that WordPress uses in media.php */
    function load_playlist_templates() {
      // Get the $post object
        global $post;
    
      // Only load scripts and styles when the post contains our special shortcodes 
      if ( !is_a( $post, 'WP_Post' ) || !has_shortcode( $post->post_content, 'playlist' ) ) return;
    
      ?>
      <script type="text/html" id="tmpl-wp-playlist-current-item">
        <# if ( data.image ) { #>
        <img src="{{ data.thumb.src }}" alt="" />
        <# } #>
        <div class="wp-playlist-caption">
                <span class="wp-playlist-item-meta wp-playlist-item-title"><?php
                        /* translators: playlist item title */
                        printf( _x( '“%s”', 'playlist item title' ), '{{ data.title }}' );
                ?></span>
                <# if ( data.meta.album ) { #><span class="wp-playlist-item-meta wp-playlist-item-album">{{ data.meta.album }}</span><# } #>
                <# if ( data.meta.artist ) { #><span class="wp-playlist-item-meta wp-playlist-item-artist">{{ data.meta.artist }}</span><# } #>
        </div>
      </script>
    
      <script type="text/html" id="tmpl-wp-playlist-item">
        <div class="wp-playlist-item">
                <a class="wp-playlist-caption" href="{{ data.src }}">
                        {{ data.index ? ( data.index + '. ' ) : '' }}
                        <# if ( data.caption ) { #>
                                {{ data.caption }}
                        <# } else { #>
                                <span class="wp-playlist-item-title"><?php
                                        /* translators: playlist item title */
                                        printf( _x( '“%s”', 'playlist item title' ), '{{{ data.title }}}' );
                                ?></span>
                                <# if ( data.artists && data.meta.artist ) { #>
                                <span class="wp-playlist-item-artist"> &mdash; {{ data.meta.artist }}</span>
                                <# } #>
                        <# } #>
                </a>
                <# if ( data.meta.length_formatted ) { #>
                <div class="wp-playlist-item-length">{{ data.meta.length_formatted }}</div>
                <# } #>
        </div>
      </script>
      <?php
    }
    
    add_action('alnp_load_after_content', 'load_playlist_templates');
    Thread Starter emsiledock

    (@emsiledock)

    People who have a custom content-partial.php file (like me) have to add these lines in their {my_theme_folder}\auto-load-next-post\content-partial.php:

    <!-- Your Post Content !-->
    <?php do_action('alnp_load_after_content'); // Required Hook ?>
    <?php if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'playlist' ) ) : ?>
         <?php manage_playlist_shortcode_js(); ?>
     <?php endif; ?>
    • This reply was modified 9 years, 2 months ago by emsiledock.
    Plugin Author Sébastien Dumont

    (@sebd86)

    Fantastic. Can’t wait to try it out. So you required the need of the global $post I see. Would that be better if the action hook passed that through so you can keep the template clean and hook the code instead?

    For example:
    <?php do_action('alnp_load_content_after_content', $post); ?>

    Thread Starter emsiledock

    (@emsiledock)

    Well my code isn’t really optimized. The new hook can make it work in a more proper way, without using globals, so i think it’s a good idea 🙂

    The JavaScript can be upgraded too to avoid repetitions

    Plugin Author Sébastien Dumont

    (@sebd86)

    Sorry it is not a new hook. Typo.

    <?php do_action('alnp_load_after_content', $post); ?>

    As for your JavaScript it would be great if it was formatted in jQuery.

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

The topic ‘Auto load next post vs. Shrotcodes’ is closed to new replies.