0

I am trying to add a stylesheet when users are accessing the blog section of my site from and iPad. The first snippet of code works, however, I don't want to use the absolute path. I want to use wordpress's function.

function ipad_css() {
  if( preg_match('/ipad/i',$_SERVER['HTTP_USER_AGENT'])) {?>
<link rel="stylesheet" type="text/css" href="http://website/wp-content/themes/my_theme/ipad.css" media="screen" /> 
<?php }
  }
  add_action('init','ipad_css');

This doesn't seem to work:

function ipad_css() {
  if( preg_match('/ipad/i',$_SERVER['HTTP_USER_AGENT'])) {?>
<link rel="stylesheet" type="text/css" href="<?php bloginfo('template_directory'); ?>/ipad.css" media="screen" /> 
<?php }
  }
  add_action('init','ipad_css');

I'm confused as to why it won't work. I have trying stylesheet_url as well with no luck.

2 Answers 2

3

Try replacing <?php bloginfo('template_directory'); ?> with <?php echo get_template_directory_uri(); ?>

Also, I wouldn't hook it to the initaction, why don't use wp_print_styles instead? It is a style after all.

2
  • I tried this method and it's still not working. I can't for the life of me figure it out.
    – chris_s
    Jul 28, 2011 at 16:05
  • well I did figure it out. instead of using get_template_directory_uri() I used get_stylesheet_directory_uri because I am using a child theme.
    – chris_s
    Jul 28, 2011 at 19:51
0

You can use this filter to filter out styles with any condition, or alternate output href (so remove/change domain too) string for certain .css files, example:

add_filter( 'style_loader_src', function($href){
if(strpos($href, "name-of-allowed.css") !== false) {
return $href;
}
return false;
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.