0

So I've been messing with this for a while now and I don't understand how I'm unable to pull in my style file properly by registering and calling it at all.

Here is my method:

public static function register_styles()
{
    add_action('wp_enqueue_scripts', function() {
        global $post;

        if (is_page('foodies')) {
            var_dump('Hello!');

            //Adding stylesheets
            wp_register_style('profile-style', '/Users/smajlovs/Sites/newsacfoodies/htdocs/wp-content/mu-plugins/sacfoodies/styles/style.css');

            //Enqueue the style
            wp_enqueue_style('profile-style', '/Users/smajlovs/Sites/newsacfoodies/htdocs/wp-content/mu-plugins/sacfoodies/styles/style.css');
        }

        if ($post->post_type == 'profile') {
            echo 'Hello! I am the register_styles method on the profile custom post type page';
        }

        return;
    });
}

Within my main sacfoodies.php file, I'm calling Profile::register_styles().


The confusing part is, the var_dump that I have inside that function actually dumps out the hello on that page as shown here:

var_dump(wp_enqueue_style('profile-style')); returns null

So does anyone know why my enqueue register and call won't render my style.css file? I have the absolute path in there for testing purposes and nothing.

1
  • 2
    You need to use a URL when enqueuing scripts and styles. You can't use a path. Jan 28, 2020 at 5:14

4 Answers 4

1

Call like this. It may be help you.

wp_register_style('profile-style',content_url().'/mu-plugins/sacfoodies/styles/style.css');
wp_enqueue_style('profile-style');
1
  • 1
    Thanks @Tanway Patel, the content_url() worked.
    – user155484
    Jan 28, 2020 at 21:13
1

If we getting expected result up to var_dump('Hello!'); than can we replace below 2 lines as following..

wp_register_style('profile-style', WPMU_PLUGIN_URL.'/sacfoodies/styles/style.css');
wp_enqueue_style('profile-style');

let me know reflected changes....

Thank you

0

Something like this should work

add_action( 'wp_enqueue_scripts', 'enqueue_styles' );
function enqueue_styles() {

if ( is_page('foodies') ) {

    wp_enqueue_style( 'your-styles', get_stylesheet_directory_uri() . '/css/style.css' );
    }
}

Swap out the get_stylesheet_directory_uri() . '/css/style.css' to match the path to your style sheet.

0

This is my solution, it works both on post and pages. Hope it helps:

add_action( 'wp_enqueue_scripts', 'my_theme_styles' );

function my_theme_styles() {
    if( get_queried_object_id() == 460 ) {
        wp_register_style( 'my-styles', get_stylesheet_directory_uri() . '/my-styles.css');
        wp_enqueue_style( 'my-styles', get_stylesheet_directory_uri() . '/my-styles.css');

    }
}

460 is the ID of a post / page

Your Answer

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