Post Closed as "too broad" by Jacob Peattie, cjbj, bosco, Rick Hellewell, Christine Cooper
4 Removed solution and follow-up question from original question
source | link

I would like to add CSS to a page or a shortcode (preferably a shortcode) that would not be dependent on a theme, a plug-in or anything.

I tried adding wp_enqueue_style('[my-css-url]'); to various PHP files but without success. Yet I would expect more proper ways to do it.

I there a way to do that?


This question is not a duplicate of Enqueue Scripts / Styles when shortcode is present That other question is asked and solved in the context of a plug-in. My question clearly state "independant from plug-ins".


Resulting code from leynmanx's suggestion (I just replaced project name with "my-project"):

In functions.php :

function custom_shortcode_styles() {
    global $post;
    if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'my-project') ) {
        wp_enqueue_style('my-project-css', get_site_url() . '/wp-content/my-project/css.css');
    }
}
add_action( 'wp_enqueue_scripts', 'custom_shortcode_styles');

This one indeed succeed in including the .css file ... but I'm left with on problem, this .css is added before the site's style while I would like it to be added at the very end of .css inclusions. Because this is this more specific piece of .css to be able to override the general ones, not the other way around.

I would like to add CSS to a page or a shortcode (preferably a shortcode) that would not be dependent on a theme, a plug-in or anything.

I tried adding wp_enqueue_style('[my-css-url]'); to various PHP files but without success. Yet I would expect more proper ways to do it.

I there a way to do that?


This question is not a duplicate of Enqueue Scripts / Styles when shortcode is present That other question is asked and solved in the context of a plug-in. My question clearly state "independant from plug-ins".


Resulting code from leynmanx's suggestion (I just replaced project name with "my-project"):

In functions.php :

function custom_shortcode_styles() {
    global $post;
    if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'my-project') ) {
        wp_enqueue_style('my-project-css', get_site_url() . '/wp-content/my-project/css.css');
    }
}
add_action( 'wp_enqueue_scripts', 'custom_shortcode_styles');

This one indeed succeed in including the .css file ... but I'm left with on problem, this .css is added before the site's style while I would like it to be added at the very end of .css inclusions. Because this is this more specific piece of .css to be able to override the general ones, not the other way around.

I would like to add CSS to a page or a shortcode (preferably a shortcode) that would not be dependent on a theme, a plug-in or anything.

I tried adding wp_enqueue_style('[my-css-url]'); to various PHP files but without success. Yet I would expect more proper ways to do it.

I there a way to do that?


This question is not a duplicate of Enqueue Scripts / Styles when shortcode is present That other question is asked and solved in the context of a plug-in. My question clearly state "independant from plug-ins".

3 added 671 characters in body
source | link

I would like to add CSS to a page or a shortcode (preferably a shortcode) that would not be dependent on a theme, a plug-in or anything.

I tried adding wp_enqueue_style('[my-css-url]'); to various PHP files but without success. Yet I would expect more proper ways to do it.

I there a way to do that?


This question is not a duplicate of Enqueue Scripts / Styles when shortcode is present That other question is asked and solved in the context of a plug-in. My question clearly state "independant from plug-ins".


Resulting code from leynmanx's suggestion (I just replaced project name with "my-project"):

In functions.php :

function custom_shortcode_styles() {
    global $post;
    if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'my-project') ) {
        wp_enqueue_style('my-project-css', get_site_url() . '/wp-content/my-project/css.css');
    }
}
add_action( 'wp_enqueue_scripts', 'custom_shortcode_styles');

This one indeed succeed in including the .css file ... but I'm left with on problem, this .css is added before the site's style while I would like it to be added at the very end of .css inclusions. Because this is this more specific piece of .css to be able to override the general ones, not the other way around.

I would like to add CSS to a page or a shortcode (preferably a shortcode) that would not be dependent on a theme, a plug-in or anything.

I tried adding wp_enqueue_style('[my-css-url]'); to various PHP files but without success. Yet I would expect more proper ways to do it.

I there a way to do that?


This question is not a duplicate of Enqueue Scripts / Styles when shortcode is present That other question is asked and solved in the context of a plug-in. My question clearly state "independant from plug-ins".

I would like to add CSS to a page or a shortcode (preferably a shortcode) that would not be dependent on a theme, a plug-in or anything.

I tried adding wp_enqueue_style('[my-css-url]'); to various PHP files but without success. Yet I would expect more proper ways to do it.

I there a way to do that?


This question is not a duplicate of Enqueue Scripts / Styles when shortcode is present That other question is asked and solved in the context of a plug-in. My question clearly state "independant from plug-ins".


Resulting code from leynmanx's suggestion (I just replaced project name with "my-project"):

In functions.php :

function custom_shortcode_styles() {
    global $post;
    if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'my-project') ) {
        wp_enqueue_style('my-project-css', get_site_url() . '/wp-content/my-project/css.css');
    }
}
add_action( 'wp_enqueue_scripts', 'custom_shortcode_styles');

This one indeed succeed in including the .css file ... but I'm left with on problem, this .css is added before the site's style while I would like it to be added at the very end of .css inclusions. Because this is this more specific piece of .css to be able to override the general ones, not the other way around.

2 Explaining why it's not a duplicate question
source | link

I would like to add CSS to a page or a shortcode (preferably a shortcode) that would not be dependent on a theme, a plug-in or anything.

I tried adding wp_enqueue_style('[my-css-url]'); to various PHP files but without success. Yet I would expect more proper ways to do it.

I there a way to do that?


This question is not a duplicate of Enqueue Scripts / Styles when shortcode is present That other question is asked and solved in the context of a plug-in. My question clearly state "independant from plug-ins".

I would like to add CSS to a page or a shortcode (preferably a shortcode) that would not be dependent on a theme, a plug-in or anything.

I tried adding wp_enqueue_style('[my-css-url]'); to various PHP files but without success. Yet I would expect more proper ways to do it.

I there a way to do that?

I would like to add CSS to a page or a shortcode (preferably a shortcode) that would not be dependent on a theme, a plug-in or anything.

I tried adding wp_enqueue_style('[my-css-url]'); to various PHP files but without success. Yet I would expect more proper ways to do it.

I there a way to do that?


This question is not a duplicate of Enqueue Scripts / Styles when shortcode is present That other question is asked and solved in the context of a plug-in. My question clearly state "independant from plug-ins".

1
source | link