I'm working on a site on which I need to load two Google fonts. In an attempt to keep things moving as efficiently as possible, I would like to combine the loading of both fonts into one request. I've never had a problem with this before. Google generates the path, and I have always just been able to copy it into my functions.php file and register it there. However, doing the same thing this time is not working. The syntax of the string looks a bit different now as well. There used to be a pipe character (|) separating the two fonts in the path that Google would generate, but now there is not. If I split each font into it's own request, everything works fine.
The first snippet provided below is how I'm attempting to register my fonts using the link Google provided with both fonts combined into one request.
function load_google_fonts() {
wp_register_style( 'my-google-fonts', 'https://fonts.googleapis.com/css2?family=Maven+Pro:wght#400;700&family=Open+Sans:wght#300;700' );
wp_enqueue_style( 'my-google-fonts' ); }
add_action( 'wp_enqueue_scripts', 'load_google_fonts' );
I've also tried placing a '|' between 'family' and 'Open' in that string, as I know that's how two fonts in one request used to be separated. No luck there either.
The next snippet is the only way I've gotten the fonts to load properly, which is by splitting them up into multiple requests.
function load_google_fonts() {
wp_register_style( 'my-google-fonts', 'https://fonts.googleapis.com/css2?family=Maven+Pro:wght#400;700' );
wp_register_style( 'my-google-fonts-2', 'https://fonts.googleapis.com/css2?family=Open+Sans:wght#300;700' );
wp_enqueue_style( 'my-google-fonts' );
wp_enqueue_style( 'my-google-fonts-2' );
add_action( 'wp_enqueue_scripts', 'load_google_fonts' );
Is there something wrong with the combined link Google is providing me? If so, what needs to be changed? If not, what am I doing wrong? Has anyone else run into this problem before? Any help would be very much appreciated.
EDIT:
Since the last Google Font update, you're required to use the following line:
<link rel="preconnect" href="https://fonts.gstatic.com">
Upon selecting the required fonts on Google Fonts, here is the embed snippet they're giving us:
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Maven+Pro:wght#400;700&family=Open+Sans:wght#300;700&display=swap" rel="stylesheet">
YOU are currently loading your font using the default Google Fonts url from the url bar. Both are not the same (See right ad wrong bellow).
Right = https://fonts.googleapis.com/css2?family=Maven+Pro:wght#400;700&family=Open+Sans:wght#300;700&display=swap.
Wrong = https://fonts.google.com/specimen/Maven+Pro?query=Maven&sidebar.open=true&selection.family=Maven+Pro:wght#400;700|Open+Sans:wght#300;700.
The proper code snippet will appear on the right side column once you've selected a font on the Google font website.
Loading scripts, stylesheets and fonts from the function.php file is the best practice.
Of course we have to adapt the code snippet given by Google Font to our Wordpress environment.
<?php
/**
* Theme scripts
*/
add_action( 'wp_enqueue_scripts', 'theme_scripts' );
function theme_scripts() {
if ( ! is_admin() ) {
/**
* Register then enqueue google fonts
*/
wp_register_style( 'google_fonts', 'https://fonts.googleapis.com/css2?family=Maven+Pro:wght#400;700&family=Open+Sans:wght#300;700&display=swap' );
wp_enqueue_style( 'google_fonts' );
};
}; ?>
When using CDNs, you want to be sure that the source is available. We can Use #fopen(); to "test" our url and make sure our CDN is working, if not we return a fallback file hosted on our website server.
<?php
/**
* Theme scripts
*/
add_action( 'wp_enqueue_scripts', 'theme_scripts' );
function theme_scripts() {
if ( ! is_admin() ) {
/**
* Register then enqueue google fonts
*
* Check if CDN's url is valid, if not return fallback
*/
$test_google_fonts = #fopen( 'https://fonts.googleapis.com/css2?family=Maven+Pro:wght#400;700&family=Open+Sans:wght#300;700&display=swap', 'r' );
if ( $test_google_fonts !== false ) {
wp_register_style( 'google_fonts', '//fonts.googleapis.com/css2?family=Maven+Pro:wght#400;700&family=Open+Sans:wght#300;700&display=swap' );
} else {
wp_register_style( 'google_fonts', get_stylesheet_uri() . '/assets/font/_my_awesome_font_fallback_' );
};
wp_enqueue_style( 'google_fonts' );
};
}; ?>
On a side note, if you intend to register and enqueue a script straight away, you don't need to do both, you can jump straight to the enqueuing part.
<?php
/**
* Theme scripts
*/
add_action( 'wp_enqueue_scripts', 'theme_scripts' );
function theme_scripts() {
if ( ! is_admin() ) {
/**
* Register then enqueue google fonts
*/
wp_enqueue_style( 'google_fonts', 'https://fonts.googleapis.com/css2?family=Maven+Pro:wght#400;700&family=Open+Sans:wght#300;700&display=swap' );
};
}; ?>
For anybody else that comes across this issue, the problem was that I had not added this snippet to the <head> within header.php.
<link rel="preconnect" href="https://fonts.gstatic.com">
This was not required before, and I'm not sure exactly why it is now, but that solved my problem. All thanks to #amarinediary for pointing it out to me.
Related
I am trying to enqueue an external plain JavaScript file from within my plugin php, to add within my footer of the WP theme. Is this possible?
I've tried the below, but test is failing as no alert is firing...
<?php
/**
Plugin Name: Add Footer JS
*/
add_action( 'wp_footer', 'my_footer_scripts' );
function my_footer_scripts(){
?>
<script>alert( 'Hi Roy' ); </script> // just a test
<?php
}
So basically; the JS would be at:
http://dev.website.com/wp-content/plugins/mykewlplugin/js/script-support.js
And this PHP, at:
http://dev.website.com/wp-content/plugins/mykewlplugin/support.php
And core plugin PHP/functionality at:
http://dev.website.com/wp-content/plugins/mykewlplugin/mykewlplugin.php
Make sure that your currently active theme is making use of the wp_footer() and wp_head() functions, as those are required to utilize the appropriate action hooks. With that said, you should really properly enqueue your assets. Anybody else working on the project (including your future self) will be appreciative of that!
This involves utilizing the wp_enqueue_scripts hook and the wp_enqueue_script() function (which relies on both the wp_head() and wp_footer functions).
The proper way to do this would be like the following:
add_action( 'wp_enqueue_scripts', 'no_spex_footer_js' );
function no_spex_footer_js(){
wp_enqueue_script( 'no-spex-footer-js', '/path/to/your/file.js', array(), null, true );
}
Note all the passed arguments, in order:
a unique handle
the path to your actual file
an array of dependencies by handle (if you rely on jQuery, change this to array('jquery'), etc.
version of the file. I use filemtime( plugin_dir_path( __FILE__ ) . 'assets/file-name.js' typically.
in_footer - This is the important one, and determines if the file should be loaded into the header or footer.
If you're sure your theme is making use of wp_head() and wp_footer(), and the file isn't working - it means your plugin isn't configured/activated properly, at which point I'd recommend another cursory glance at the Writing A Plugin documentation and the File Header Requirements.
For your specific case, something like this should get you started:
mykewlplugin.php:
<?php
/**
* Plugin Name: My Kewl Plugin
* Description: Does Kewl Things
* Version: 0.1
*/
add_action( 'wp_enqueue_scripts', 'my_kewl_plugin_footer_js' );
function my_kewl_plugin_footer_js(){
wp_enqueue_script( 'my-kewl-plugin-footer', plugins_url( '/js/script-support.js', __FILE__ ), array(), filemtime( plugin_dir_path( __FILE__ ) . 'js/script-support.js', true );
}
?>
I want to remove css and js file from a template in WordPress by using wp_dequeue_script and wp_dequeue_style function but my code is not working.
Js File: <script type='text/javascript' src='http://localhost/worpress/wp-content/themes/goliath/theme/assets/js/vendor/jquery.hoverintent.min.js'></script>
Css File: <link rel='stylesheet' id='plsh-bbpress-css' href='http://www.smeadvisor.com/wp-content/themes/goliath/theme/assets/css/bbpress.css' type='text/css' media='all' />
I am using goliath theme and I have added below code in goliath-child theme functions.php file
add_action('wp_print_scripts','example_dequeue_myscript');
function example_dequeue_myscript() {
wp_dequeue_script( 'jquery.hoverintent' );
}
add_action('wp_print_styles','example_dequeue_mystyle');
function example_dequeue_mystyle() {
wp_dequeue_style( 'bbpress' );
}
Please help me in these!
wp_dequeue_script() and wp_dequeue_style() works only if the related CSS and JS files are included in the website by wp_enqueue_script()..
wp_dequeue_script and wp_dequeue_style work by taking the handle as the parameter.
You should have registered as like follows :
wp_register_style('pagination-style', plugins_url('style.css', __FILE__));
wp_enqueue_style('pagination-style');
and script like this :
wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
wp_enqueue_script('jquery');
And then use handle for removing css and js as follows:
wp_dequeue_style('pagination-style');
wp_dequeue_script('jquery');
Hope Remove css and js
Try
add_action('wp_enqueue_scripts','example_dequeue_myscript', 30);
function example_dequeue_myscript()
{
wp_dequeue_script('jquery.hoverintent');
wp_dequeue_style('bbpress');
}
You have to enqueue these file in header or footer, not directly write in script or link tag that is proper method and then enqueue your script in condition. like this
add_action('wp_enqueue_scripts','example_dequeue_mystyle');
function example_dequeue_mystyle() {
if(!is_page('my-page-slug')){
wp_enqueue_style( 'bbpress' );
wp_enqueue_script( 'jquery.hoverintent' );
}
}
Hope you got that.
If the files are enqueued (not hardcoded in title) then the problem may be in wrong script/style names. Try to use this in functios.php first:
function remove_head_scripts() {
global $wp_styles;
var_dump($wp_styles);
global $wp_scripts;
var_dump($wp_scripts);
}
add_action( 'wp_enqueue_scripts', 'remove_head_scripts', 101 );
Then you can see all of the enqueued styles & scripts with their respective slugs printed somewhere inside your html (try to use ctrl+U in browser). Looking in that lists you can locate your desired script & style & find out it's slugs. If you can't see them, try to play with priority (101) or hook (wp_enqueue_scripts)
After you know exact slugs of your script & style, replace snippet above with new one:
function remove_head_scripts() {
wp_dequeue_style( 'exact_style_slug' );
wp_dequeue_script( 'exact_script_slug' );
}
add_action( 'wp_enqueue_scripts', 'remove_head_scripts', 101 );
Again, you can play with priority & hook. It highly depends on where your theme/plugin is enqueueing that script & style.
I have fonts from typography.com that I moved to production and uploaded to my AWS S3 Bucket to use on my Wordpress site. I have done everything that typography.com has told me to do, but the fonts still are not being displayed. Has anyone gone through this before and can point me in the right direction? I added an #import statement in style.css in my theme to the url that typography.com gave me. I also have a wp_enqueue function in functions.php that I have uploaded to the S3 server.
add_action( 'wp_head', 'my_fonts' );
function my_fonts(){ ?>
<link rel="stylesheet" type="text/css" href="//cloud.typography.com/7783912/761788/css/fonts.css">
<?php
}
The fonts are still not being displayed. What am I doing wrong?
The proper way to include stylesheets is to use wp_enqueue_style. Using this function will also allow you to declare this font as a dependency for other stylesheets. You should also use the 'wp_enqueue_scripts' hook, as opposed to 'wp_head':
/**
* Proper way to enqueue scripts and styles
*/
function theme_name_scripts() {
wp_enqueue_style( 'typography', '//cloud.typography.com/7783912/761788/css/fonts.css' );
}
add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
If you're still having issues at this point, make sure you have the proper permissions to GET this file from the Typography cloud server.
I’m building my website on Wordpress + Bootstrap CDN.
I decided to make it on CDN to have no problem with updates in the future plus I read that this way it’s a bit faster.
So the thing is I have a problem with styles.
I imported my local ‘style.css’ to ‘header.php’, but since ‘bootstrap.min.css’ from CDN also has its own parameters I can’t apply some things.
How do I rewrite CDN’s parameters? Or is there a way to edit this exact ‘bootstrap.min.css’ file?
Thanks in advance.
You can simply just use wp_enqueue_style to load those files. Here is an example you can get an idea from.
function enqueue_my_scripts() {
wp_enqueue_script( 'jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', array('jquery'), '1.9.1', true); // we need the jquery library for bootsrap js to function
wp_enqueue_script( 'bootstrap-js', '//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js', array('jquery'), true); // all the bootstrap javascript goodness
}
add_action('wp_enqueue_scripts', 'enqueue_my_scripts');
function enqueue_my_styles() {
wp_enqueue_style( 'bootstrap', '//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css' );
wp_enqueue_style( 'my-style', get_template_directory_uri() . '/style.css');
}
add_action('wp_enqueue_scripts', 'enqueue_my_styles');
and to write the rules just use !important in your style.css file.
When you use wp_enqueue_style, you have the option to take control on where your styles and script be used for further references. Here is an example how to use it.
function my_enqueue_stuff() {
if ( is_page( 'landing-page-template-one' ) ) {
/** Call landing-page-template-one enqueue */
} else {
/** Call regular enqueue */
}
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_stuff' );
#TylerH answered my question and it solved my problem.
A better technique would be to include your own CSS file after you include the CDN file, and just write whichever styles you need to this CSS file of yours. That way they will override the problematic styles in your CDN file.
Thank you very much!
I currently have been customising the standard 2013 theme included with Wordpress, making the standard child theme and adding to the bottom of the style.css stylesheet.
This works fine for all of my pages, however there is a case where I need a custom stylesheet 'news.css' for the News page.
I've tried adding the following code into the header.php file, just before the closing tag, to ensure that it's not overruled by other css files.
<?php
if ( is_page( 'news' ) ) { ?>
<link rel="stylesheet" href="<?php echo get_stylesheet_directory_uri(); ?>/news.css">
<?php } else {
}
?>
The news.css file is in the child theme's root directory, and the url is www.__.com/news/ however I still can't get Wordpress to load this file when on the News page.
What would I need to do to get this stylesheet to load, only for this page?
Thanks in advance.
EDIT: SOLUTION FOUND - The news page (being my posts page) had the .blog class applied to the body tag. Using .blog in the master css file, I can now specifically adjust this page's CSS! Thank goodness!
I see you've already fixed the issue by using a selector in the CSS that make the rules only apply to the page you want them to (in your case the .blog class worked good.
This is pretty much the standard way of doing things these days.
However I just wanted to put this here for reference in case anyone needs it in the future. The function below 'enqueues' the stylesheet and is the correct way of adding additional stylesheets. This function registers and enqueues 2 new stylesheets.
function load_additional_styles() {
wp_register_style( 'second-style', get_template_directory_uri() . '/style2.css' );
wp_enqueue_style ( 'second-style' );
wp_register_style( 'third-style', get_template_directory_uri() . '/style3.css' );
wp_enqueue_style ( 'third-style' );
}
add_action( 'wp_enqueue_scripts', 'load_additional_styles' );
It's also possible to use conditionals within these functions so (using the question above as the example) you could do something like this
function load_additional_styles() {
if ( is_page( 'news' ) ) {
wp_register_style( 'second-style', get_template_directory_uri() . '/style2.css' );
wp_enqueue_style ( 'second-style' );
wp_register_style( 'third-style', get_template_directory_uri() . '/style3.css' );
wp_enqueue_style ( 'third-style' );
}
}
add_action( 'wp_enqueue_scripts', 'load_additional_styles' );
In the above code the stylesheets will only be added when is_page('news') returns true.