Conditional header script to enable ad units

Could you help with this?

I think I have all the correct code from various people it’s just how to check it’s optimised and then to actually execute it in the right place/way.

Basically what it should do is check for a logged in, currently paid-up subscriber and then NOT display MONUMETRIC ads for such people. Ie if you don’t subscribe you get ads.

  1. Working scenario

I believe that header.php is at the top of every wordpress blog post.

In my header.php file I have this line in red from my ad provider (MONUMETRIC) which works as-is and has done for months. I’m not sure if the synchronous execution is correct.

<?php wp_head(); ?> … does nothing???

<script type="text/javascript" async="false" src="//monu.delivery/site/d/f/7accb3-ae6e-44dd-adf8-d5f3ea059f56.js" data-cfasync="false"></script>
  1. REPLACEMENT Code given to me by user subscription manager software company
function pmsc_enqueue_ads_script() {
  if (is_user_logged_in()) {
    $user_id = get_current_user_id();

    if ($user_id != 0 && function_exists('pms_get_member_subscriptions')) {
                       
      $subscription = pms_get_member_subscriptions(array('user_id' => $user_id));

      if (!empty($subscription) && (!empty($subscription[0]->expiration_date) && time() <= strtotime($subscription[0]->expiration_date)))
        return;
      else
        wp_enqueue_script('ads-script', plugin_dir_url( __FILE__ ) . 'assets/js/script.js', array('jquery'));
      }
        } else {
          wp_enqueue_script('ads-script', plugin_dir_url( __FILE__ ) . 'assets/js/script.js', array('jquery'));
          return;
        }
}
add_action( 'wp_enqueue_scripts', 'pmsc_enqueue_ads_script' );
  1. What I did 1 –

I disabled the existing script. <!script ……. <!/script>

  1. What I did (2)

I changed the ‘assets/js/script.js’ for =’//monu.delivery/site/d/f/7accb3-ae6e-44dd-adf8-d5f3ea059f56.js’
And then inserted the 12 or so lines of code exactly as shown

  1. What I did (3)

wordpress didn’t seem to like the function as shown above so I wrapped it in php brackets.

<?php   
    function pmsc...etc.etc.
?>
  1. What was shown:

After that code was inserted and caches cleared, the ads NEVER loaded. I tried it in an inprivate session with firefox and with IE11 both were logged out.

  1. So can you tell me why it’s not working at all? i guess i am including the script in the wrong way and making a really basic error. it’s a javacript script going into a php hader AFAIK

Many thanks !

This is a crucial part of the theme. It is a hook that is used by the WP core and by many plugins to insert functions.

Where did you put this function - in the functions.php file?

Did you make sure that the js script is enqueued properly in the functions.php file?

ty @webmachine for your reply.

  1. i did not change :: <?php wp_head(); ?> ie it was still there and executing before the code i inserted
  2. i put the new .js into the header.php file, as below. I don’t seem to have a functions.php file. The ad company MONUMETRIC added the existing script to the header.php as i indicated above. so i assumed that was the place to put the replacement.
  3. I just changed the .js full path name as described.

thank you !!!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.