Enrique René
Enrique René

Reputation: 598

wp_enqueue_style doesn't work

First of all, I tried this and didn't work: "https://wordpress.stackexchange.com/questions/89494/how-to-enqueue-the-style-using-wp-enqueue-style/89495#89495".

What I want to do is a plugin with a unique page (options page) and I'm trying to insert in head tag a <link href="my_dir/css/style.css"/>. The prefix of my plugin is easydm and has in the main file the followingo code:

define( 'EASYDM_VERSION', '1.0' );
define( 'EASYDM__MINIMUM_WP_VERSION', '2.1' );
define( 'EASYDM_DIRECTORY_NAME', 'easy-downloader-manager/' );

define( 'EASYDM_ROOT_DIRECTORY', substr( plugin_dir_path( __FILE__ ), 0, strpos( plugin_dir_path( __FILE__ ), EASYDM_DIRECTORY_NAME ) ) );

define( 'EASYDM_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'EASYDM_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );

define( 'EASYDM_SETTINGS_URL', plugin_dir_url( __FILE__ ).'settings/' );
define( 'EASYDM_SETTINGS_DIR', plugin_dir_path( __FILE__ ).'settings/' );
define( 'EASYDM_SETTINGS_PATH', 'settings/' );

define( 'EASYDM_CSS_URL', plugin_dir_url( __FILE__ ).'css/' );
define( 'EASYDM_CSS_DIR', plugin_dir_path( __FILE__ ).'css/' );
define( 'EASYDM_CSS_PATH', 'css/' );    

require_once EASYDM_SETTINGS_DIR.'easydm-functions.php';

register_activation_hook( __FILE__, 'easydm_activation');

add_action( 'wp_enqueue_scripts', 'easydm_add_link_tag_to_head' );

My easydm-functions.php has:

function easydm_add_link_tag_to_head() {
    wp_enqueue_style( 'style', get_plugin_directory_uri().'/'.EASYDM_DIRECTORY_NAME.EASYDM_CSS_PATH.'style.css' );
}

This is my last try, but I tried some other arguments on wp_enqueue_style() function.

Some help?

Thanks

--- editing ---

Here's a possible way to solve. Indeed, I solved partially the problem. Now I need to cut a string which is appended to my link. See that:

https://wordpress.stackexchange.com/questions/219911/enqueue-script-style-plungin-development/219914?noredirect=1#comment322394_219914

--- editing again ---

I found this http://wpcrux.com/wordpress-enqueue-functions-version/ what solve the above new problem... but still not styling my page... I checked if the path is correct, my file is there, is not empty or any error like these.

Any idea?

Upvotes: 1

Views: 1850

Answers (2)

Leon
Leon

Reputation: 139

Thanks for you sharing. I meet the same issue when adding some styles and scripts but I think the issue you pointed out should not be the root cause, actually, my problem is I should add it to wp_enqueue_scripts action like below

add_action('wp_enqueue_scripts','nz_footer_cumstom_func');

I tried your solution, I think both could work. I mean wp_enqueue_style() could accept a relative path as well as a full file path.

Upvotes: 1

Enrique Ren&#233;
Enrique Ren&#233;

Reputation: 598

SOLVED!!!

The documentation for wp_enqueue_style function is clear to say that the second argument nedd to be relative path to the root installation WordPress. But as I viewed many examples in tutorials using full path I ignored it. Now, defining this constant and all previous steps everything runs ok.

define( 'EASYDM_CSS_PATH' , str_replace( site_url().'/', '', plugin_dir_url( __FILE__ ) ).'css/' );

and in function:

wp_enqueue_style( 'easydm-style', '/'.EASYDM_CSS_PATH.'style.css', array(), null, 'all' );

So, if someone is tired out to looking for and don't wanna read this all discussion, the final code is:

define( 'EASYDM_CSS_PATH' , str_replace( site_url().'/', '', plugin_dir_url( __FILE__ ) ).'css/' );
add_action( 'admin_enqueue_scripts', 'easydm_add_link_tag_to_head' );

functions file:

function easydm_add_link_tag_to_head() {
    wp_enqueue_style( 'easydm-style', '/'.EASYDM_CSS_PATH.'style.css', array(), null, 'all' );
    wp_enqueue_style( 'easydm-manager', '/'.EASYDM_CSS_PATH.'manager.css', array(), null, 'all' );
}

Where I add two styles. I really hope this help someone. Thank you everyone!

Upvotes: 1

Related Questions