-1

I would like to load a css file in my Wordpress plugin by php. First I tried file_get_contents:

$stylesheet = file_get_contents(plugins_url('assets/css/fonts.css', __FILE__ ));

But than I get this error:

failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized

I noticed that one has to enable Allow_url_fopen in php.ini (but I also read about this being a security risk).

Is there a WordPress function to load e.g. a txt file and store its content in a variable?

EDIT: I would like to parse a CSS file and get all its font-families.

<?php
function my_fonts() {
    $stylesheet = file_get_contents('../assets/css/fonts.css');
    $rows       = explode("\n", $stylesheet);
    array_shift($rows);

    $fonts = array();

    foreach($rows as $row => $data)
    {
        // Find all available font-families
        $regex = '#font\-family:(.*)#';
        preg_match($regex, $data, $output);
        if($output){
            $font = str_replace(array('font-family:', '\'', ';'), '', $output[0]);
            $fonts[] = $font;
        }
    }

    // Remove dublicates
    $fonts = array_unique($fonts);

    print_r($fonts);
}
?>
9
  • Must the CSS be generated dynamically or are you just loading a CSS file so it can be linked in your generated HTML? May 19, 2016 at 20:02
  • The CSS is static. I would look to parse the css to see if it contains a certain font-family.
    – user90798
    May 19, 2016 at 20:12
  • Are you able to tell me what it is you're trying achieve in total? I might be able to advise you properly then. May 19, 2016 at 20:15
  • 1
    Don't forget that the CSS might use the shorthand font property. And that @font-face rules let the author set arbitrary font names on fonts. May 19, 2016 at 20:25
  • 1
    And is the relative path correct? We don't know the path to your code. May 19, 2016 at 20:41

2 Answers 2

1

You should use WordPress's built in function wp_enqueue_style for loading styles.

/**
 * Proper way to enqueue scripts and styles
 */
function wpdocs_theme_name_scripts() {
    wp_enqueue_style( 'style-name', get_stylesheet_uri() );
    wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'wpdocs_theme_name_scripts' );

https://developer.wordpress.org/reference/functions/wp_enqueue_style/

3
  • Yep. (I'm out of votes) May 19, 2016 at 20:17
  • Generally yes, but this isn't what the OP wants to achieve (see his comments).
    – Tim Malone
    May 19, 2016 at 20:18
  • @TimMalone, ah. Well that's a completely different question now.
    – tehlivi
    May 19, 2016 at 20:49
0

Looks like I blocked my self out… I had an .htaccess with password protection running that prevented the script from accessing the file.

2
  • Glad you fixed it. May 19, 2016 at 20:50
  • Can you please elaborate your real problem in your question? As it currently stands, I need to close your question as being too localized, raising your flag count, which will lead to flags on badly researched or written future questions. That is something you will want to avoid.
    – kaiser
    May 27, 2016 at 9:39

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