• Before I begin, let me say that I have already scoured this forum looking for an existing fix for my issue…

    Here’s my dilemma. I’m migrating to the newer version of Flowplayer that uses ‘HTML5’, but I also need the older Flowplayer script to run on older/archived posts created before (August 29th 2015). The problem is that I cannot have both new and old scripts running on the same page because they share the same ‘namespace’ and will create a conflict.

    This is an example of the old script that I need to run in the header for the archived posts:
    <script src="http://www.mydomain.com/blog/flwplayerfiles/flowplayer-3.2.13.min.js"></script>

    Does anyone know of…or have a PHP or WordPress script that will display this older Flowplayer .js code in the header if the post was written before the aforementioned date?

    My WordPress URL/permalink formatting is as follows:
    http://www.mydomain.com/blog/2015/08/30/this-is-an-archived-post/

    I’m sure this is a simple script for someone who knows what they’re doing. Your help would be greatly appreciated!

Viewing 15 replies - 1 through 15 (of 32 total)
  • You could use PHP’s DateTime class. It’s available in PHP 5.2 or later. Then you’d probably hook onto wp_enqueue_scripts():

    function sc_load_flowplayer() {
      global $post;
    
      $post_date = new DateTime( get_the_date( null, $post->ID ) );
      $cutoff_date = new DateTime( '2015-08-29' );
    
      if ( is_singular() && ( $post_date < $cutoff_date ) ) {
        wp_enqueue_script( 'older-flowplayer', 'path/to/older/flowplayer.js' );
      } else {
        wp_enqueue_script( 'newer-flowplayer', 'path/to/newer/flowplayer.js' );
      }
    }
    add_action( 'wp_enqueue_scripts', 'sc_load_flowplayer' );
    Thread Starter LXXVI

    (@lxxvi)

    stephencottontail,

    Thanks for that, but unfortunately I don’t know how to hook into wp_enqueue_scripts(). Though I do follow instructions quite well if you could explain that to me.

    I tried simply placing your code into the header, bound by php opening and closing tags, but I was unable to get the code to work.

    I’m using php version 5.4.24

    You should put the code I posted in your child theme’s functions.php.

    Thread Starter LXXVI

    (@lxxvi)

    I don’t mean to be annoying, but I’m still lost.

    Could you contact me at [email protected]? I run a rather large blog and I need to get this done quickly. If you could knock it out for me, I’d be happy to pay you for your time.

    Joe

    What part did you get lost on? Have you read the Codex’s article on child themes: http://codex.ww.wp.xz.cn/Child_Themes?

    Thread Starter LXXVI

    (@lxxvi)

    Hi,

    I really don’t have time to take a crash course in WP scripting. Can you or someone contact me so that I can outsource this work? I need to get it done quickly.

    Thanks!

    Thread Starter LXXVI

    (@lxxvi)

    I did successfully create a child theme and I used your script in the child’s function.php, but it didn’t do what I want.

    Can you post the contents of your child theme’s functions.php to Pastebin and post the link here?

    Thread Starter LXXVI

    (@lxxvi)

    Could you contact via email so that we can move this to a more private forum?

    No. Forum rules are that all support must be kept on the forum. Can you post the contents of your child theme’s functions.php to Pastebin and post the link here?

    Thread Starter LXXVI

    (@lxxvi)

    Thread Starter LXXVI

    (@lxxvi)

    I need for that javascript link to conditionally post in the head of the page.

    Thread Starter LXXVI

    (@lxxvi)

    For newer posts, nothing should appear.

    You only need to pass the URL to the JavaScript file to wp_enqueue_script(), which will correctly format the <script> tag for you:

    wp_enqueue_script( 'older-flowplayer', 'http://www.SITEURL.com/blog/flwplayerfiles/flowplayer-3.2.6.min.js', $in_footer = false );

    And since you don’t need to load the older script in some conditions, you can remove the rest of the conditional:

    function sc_load_flowplayer() {
      global $post;
    
      $post_date = new DateTime( get_the_date( null, $post->ID ) );
      $cutoff_date = new DateTime( '2015-08-29' );
    
      if ( is_singular() && ( $post_date < $cutoff_date ) ) {
        wp_enqueue_script( 'older-flowplayer', 'http://www.SITEURL.com/blog/flwplayerfiles/flowplayer-3.2.6.min.js', $in_footer = false );
      }
    }
    add_action( 'wp_enqueue_scripts', 'sc_load_flowplayer' );
    Thread Starter LXXVI

    (@lxxvi)

    So, I use this in the header?

    <?php
    wp_enqueue_script( 'older-flowplayer', 'http://www.SITEURL.com/blog/flwplayerfiles/flowplayer-3.2.6.min.js', $in_footer = false );
    ?>
Viewing 15 replies - 1 through 15 (of 32 total)

The topic ‘WordPress Post Date Conditional Script’ is closed to new replies.