I am creating a theme by myself in wordpress. But suddenly my functions.php file stop to work. I am enqueuing a stylesheet file. First time it worked. But now its not working. whats wrong with it? (I am new in wordpress) My code is
<?php
function get_external_files(){
wp_enqueue_style('style', 'get_stylesheet_uri()');
}
add_action('wp_enqueue_scripts', 'get_external_files');
You've put get_stylesheet_uri() inside quotes '' turning it into a string instead of a function call... This should work:
<?php
function get_external_files(){
wp_enqueue_style('style', get_stylesheet_uri());
}
add_action('wp_enqueue_scripts', 'get_external_files');
Related
I've added the following code in functions.php but it doesn't seem to work. What is wrong?
function include_styles(){
wp_enqueue_style ( 'main-style', get_theme_file_uri('/style.css'));
}
add_action('wp_enqueue_scripts', 'include_styles');
I would suggest to use get_template_directory_uri() instead of get_theme_file_uri
Is there a way to get "sly" (this javascript plugin: http://darsa.in/sly/) in a Wordpress site? I really like it but i can't find the way to put it in a wordpress site. Or maybe do you know something very similar?
you can download your script.js .Put your script.js into your js file of your theme folder and then you can enqueue your js file like this:
function new_scripts() {
wp_register_script('my_new_script', get_template_directory_uri() . '/js/new_script.js', array('jquery'),'1.1', true);
wp_enqueue_script('my_new_script');
}
add_action( 'wp_enqueue_scripts', 'new_scripts' );
You can read more here : https://developer.wordpress.org/reference/functions/wp_enqueue_script/
My stylesheet is enqueued if i use
<link rel = "stylesheet" type = "text/css" href = "<?php echo get_template_directory_uri();?>/style.css">
but it's not included when i use
function include_css(){
wp_enqueue_style('main_css',get_stylesheet_uri());
}
add_action('wp_enqueue_scripts','include_css');
You may correct the code like this :
function include_css(){
wp_enqueue_style('main_css',get_template_directory_uri().'/style.css');
}
add_action('wp_enqueue_scripts','include_css');
The issue is that you are trying to enqueue the css file without specifying it's complete url the code you use i.e
> wp_enqueue_style('main_css',get_stylesheet_uri());
This means that you are adding the style.css file which is always on the root directory but still you are giving name as main_css first make sure which css you want to add then someone will able to properly answer your question and the css you are trying to add is in which directory.
Please refer to this link get_stylesheet_uri
To enqueue a stylesheet use this in functions.php
1.we need to register our stylesheet.
2.we should enqueue it.
3.Hook to an action
4.call the function where you want to enqueue it.
function include_css()
{
wp_register_style('main_css',get_template_directory_uri().'/style.css');
wp_enqueue_style('main_css');
}
add_action('wp_enqueue_scripts','include_css');
call,
<?php wp_head();?>
in the place you want to enqueue it.(mostly in header.)
Please add this code in theme's functions.php
function enqueue_scripts() {
wp_enqueue_style('style', get_stylesheet_directory_uri() . '/style.css', array());
}
add_action( 'wp_enqueue_scripts', 'enqueue_scripts' );
I would like to ask why my stylesheet is not showing on the page source of the webpage. Please see the code below, that's the code in my functions.php.
`function theme_styles() {
wp_enqueue_style( 'main', get_template_directory_uri() . '/style.css' );
}
add_action( 'wp_enqueue_scripts', 'theme-styles' );`
I believe I have entered the code correctly but whenever I right click on the webpage and look for the source code, my stylesheet is not showing. I am using WebMatrix by the way and my style.css is under the themes folder that I am currently working on. I am new in WordPress and I'm trying to create my own custom theme. Any help will be highly appreciated.
Thank you so much,
Gilbert Jacob
Try This :
wp_enqueue_style( 'themename-style', get_stylesheet_uri() );
Try this:
function my_theme_load_resources() {
$theme_uri = get_template_directory_uri();
wp_register_style('style_css', $theme_uri.'/style.css', false, '0.1');
wp_enqueue_style('style_css');
}
add_action('wp_enqueue_scripts', 'my_theme_load_resources');
Never mind, I noticed that my theme_styles is using an underscore and the one I use in action is theme-styles. Thanks!
I have been looking for an answer for this in SOF but didn't find a clear answer
I have a plugin that forces pages to be shown when certain conditions are met. but when i try to include css files for styling i get no response .
I tried to include the file using normal html and this was a failure
then tried the wp_register_style and wp_enqueue_style as such:
function rw_add_style(){
$rw_path = plugins_url('kawaleb/style.css');
wp_register_style('testili',plugins_url('kawaleb/style.css'));
wp_enqueue_style( 'testili' );
}
add_action ('wp_enqueue_scripts','rw_add_style');
wp_enqueue_style( 'testili' );
}
I placed this code on the page that should be shown when the conditions are met
What I don't know here is how to procede after enqueing !
do I need to use html to include the stylesheet file ( and then what is the use of enqueing ?) or does it do that by itself (and then what I am missing here ? )
In the doc of codex they dont go further than telling you to register the style then enqueue it !!!
Thank you all :)
You don't need to register the style, you can just enqueue it. Also, you mentioned that you've put the code in the file where you'd like it to display, you should put it in the index file of your plugin, so in /your-plugin/index.php or whatever the main file is called, add this code:
function rw_add_style() {
wp_enqueue_style( 'testili', plugins_url( 'kawaleb/style.css' ) );
}
add_action( 'wp_enqueue_scripts', 'rw_add_style' );
If you need it only on a certain page then you should add your conditional within the function, so you could do this for example:
function rw_add_style() {
global $post;
if ( $post->post_name == 'post_name' ) {
wp_enqueue_style( 'testili', plugins_url( 'kawaleb/style.css' ) );
}
}
add_action( 'wp_enqueue_scripts', 'rw_add_style' );
And you can work out what the post name is for the page you need to enqueue it for by temporarily adding the following code to the page template:
global $post;
echo $post->post_name;
To be clear, you don't need to add any html <link> to include the CSS as you're right, there would be no point in enqueuing it then. Just add the enqueue as I described above in the main index file of your plugin and it will be automatically included in the wp_head() in your header and output just before the </head>.
I hope this helps. Good luck. =)