So, I am trying to use the Rubik Mono One font on a website of mine, but it won't show up for anyone. I use #font face and tried clearing my cache ant it still did not work, here is the css:
#font-face {
font-family: 'Rubik Mono One';
src: url(https://fonts.gstatic.com/s/rubikmonoone/v9/UqyJK8kPP3hjw6ANTdfRk9YSN983TKU.woff2) format('woff2');
}
I just made 3 different tests with 3 different fonts including Rubik Mono One and everything is working fine.
Either you're enqueuing it badly. eg: You're not using <link rel="preconnect" href="https://fonts.gstatic.com">.
Or you're using some kind of framework like Bootstrap which is taking priority over your font.
Understanding dependencies and sequential order
Wordpress let you specify a dependency array upon script enqueuing.
$deps (string[]) (Optional) An array of registered stylesheet handles this stylesheet depends on. Default value: array()
Source # https://developer.wordpress.org/reference/functions/wp_enqueue_style/
Your css enqueuing order should look like this:
framework → google font → stylesheet
If your stylesheet is enqueued last, your defined style will be higher up in the firing sequence.
As I mentioned, we also need to add <link rel="preconnect" href="https://fonts.gstatic.com"> for Google Font to actually work. We can do just that by using style_loader_tag wordpress filter, which will filters the HTML link tag of our enqueued Google Font.
Here is the final code. Do keep in mind that, if you're using a framework you must specify the dependency for the Google Font tag.
<?php
add_action( 'wp_enqueue_scripts', 'theme_fonts' );
function theme_fonts() {
if ( ! is_admin() ) {
/**
* Register & Enqueue gfont_css.
* #link https://developer.wordpress.org/reference/functions/wp_enqueue_style/
*/
wp_enqueue_style( 'gfont_css', 'https://fonts.googleapis.com/css2?family=Rubik+Mono+One&display=swap', [], wp_get_theme()->version, 'all' );
/**
* Add mandatory Google Font rel='preconnect' <link> and required attributes to gfont_css
* Filters the HTML link tag of an enqueued style & add required attributes
* #link https://developer.wordpress.org/reference/hooks/style_loader_tag/
*/
add_filter( 'style_loader_tag', 'data_gfont_css', 10, 3 );
function data_gfont_css( $tag, $handle, $src ) {
if( $handle === 'gfont_css' ) {
$tag = str_replace(
"<link rel='stylesheet'",
"<link rel='preconnect' href='https://fonts.gstatic.com'>" . PHP_EOL . "<link rel='stylesheet'",
$tag
);
};
return $tag;
};
/**
* Register & Enqueue style_css.
* #link https://developer.wordpress.org/reference/functions/wp_enqueue_style/
*/
wp_enqueue_style( 'style_css', get_stylesheet_uri(), [ 'gfont_css' ], wp_get_theme()->version, 'all' );
};
};
?>
Dependencies are specified in betweens the brackets //... [ 'gfont_css' ] which is the equivalent to array( 'gfont_css' ).
Finally we can apply our font to our elements in our style.css, and we can add an !important statement to override as a redundancy. better not to do it tho if you're using multiple fonts.
body {
font-family: 'Rubik Mono One', sans-serif !important;
}
Related
I have the following WooCommerce Payments stylesheet being loaded in my tag and have not been able to find out how to remove it:
<link rel='stylesheet' id='wc-blocks-checkout-style-css' href='https://www.example.com/wp-content/plugins/woocommerce-payments/dist/upe-blocks-checkout.css?ver=1.0' media='all' />
(I only use WooCommerce on certain pages so therefore do not need all of the files to be loaded on most pages) I checked with WooCommerce and was not able to get answers.
I removed the stylesheet from loading in the header successfully within the wp_enqueue_scripts action hook using:
wp_dequeue_style('wc-blocks-checkout-style');
but the following line still appears in the footer and loads the same CSS file:
<link rel='stylesheet' id='wc-blocks-checkout-style-css' href='https://www.example.com/wp-content/plugins/woocommerce-payments/dist/upe-blocks-checkout.css?ver=1.0' media='all' />
I'm looking for this answer too.
The stylesheet is enqueued by this file of the plugin:
woocommerce-payments/includes/class-wc-payments-upe-blocks-payment-method.php
<?php
/**
* Class WC_Payments_Blocks_Payment_Method
*
* #package WooCommerce\Payments
*/
/**
* The payment method, which allows the gateway to work with WooCommerce Blocks.
*/
class WC_Payments_UPE_Blocks_Payment_Method extends WC_Payments_Blocks_Payment_Method {
/**
* Defines all scripts, necessary for the payment method.
*
* #return string[] A list of script handles.
*/
public function get_payment_method_script_handles() {
wp_enqueue_style(
'wc-blocks-checkout-style',
plugins_url( 'dist/upe-blocks-checkout.css', WCPAY_PLUGIN_FILE ),
[],
'1.0'
);
wp_register_script(
'stripe',
'https://js.stripe.com/v3/',
[],
'3.0',
true
);
wp_register_script(
'WCPAY_BLOCKS_CHECKOUT',
plugins_url( 'dist/upe-blocks-checkout.js', WCPAY_PLUGIN_FILE ),
[ 'stripe' ],
'1.0.1',
true
);
wp_set_script_translations( 'WCPAY_BLOCKS_CHECKOUT', 'woocommerce-payments' );
return [ 'WCPAY_BLOCKS_CHECKOUT' ];
}
}
i enqueue wp files for fontawesome and main.js
but fontawesome shows 403 error and main.js returns 404 error although both files exists in server
and it works on local host
but not on server
below is functions.php
function banking_files(){
//css
wp_enqueue_style('google-fonts','https://fonts.googleapis.com/css2?family=Roboto+Slab:wght#300;400;500;700&display=swap');
wp_enqueue_style('google-fonts2','https://fonts.googleapis.com/css2?family=Nunito:wght#300;400;600;800&display=swap');
wp_enqueue_style('google-fonts2','https://fonts.googleapis.com/css2?family=Oswald:wght#300;400;500&display=swap');
//wp_enqueue_style('bootstrap',get_stylesheet_uri().'/assets/css/bootstrap.css',array(),'4.0','all');
wp_enqueue_style('bootstrap',get_template_directory_uri().'/assets/css/bootstrap.css',array(),'4.0','all');
wp_enqueue_style('font-awesome','https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css');
wp_enqueue_style('slick-theme','https://cdn.jsdelivr.net/gh/kenwheeler/slick#1.8.1/slick/slick-theme.css');
wp_enqueue_style('slick-css','https://cdn.jsdelivr.net/npm/slick-carousel#1.8.1/slick/slick.css');
wp_enqueue_style('custom',get_template_directory_uri().'/assets/css/style.css',array(),microtime(),'all');
wp_enqueue_style('banking_main_styles',get_stylesheet_uri(),array(),microtime());
//js
wp_enqueue_script('jquery');
wp_enqueue_script('boot-popper','https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js');
wp_enqueue_script('boot-js','https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js');
wp_enqueue_script('fontawesome-js','https://kit.fontawesome.com/a076d05399.js');
wp_enqueue_script('slick-js','https://cdn.jsdelivr.net/npm/slick-carousel#1.8.1/slick/slick.min.js');
wp_enqueue_script('main-js',get_template_directory_uri().'./assets/js/main.js',array(),microtime(),true);
}
add_action('wp_enqueue_scripts','banking_files');
error screenshot in console
enter image description here
any help will be appreciated ..thanks
The request for fontawesome returns a 403 because for whatever reason fontawesome won't accept requests from the server your website is hosted on. Status 403 means forbidden. Copy it, save it locally and reference that instead.
Your main-js returns a 404 because you're trying to use a relative path, which wp_enqueue_script doesn't like.
get_template_directory_uri().'/assets/js/main.js' is the correct way to retrieve the path, omitting the extra period.
If I'm not mistaking, any fontawesome hosted outside the fontawsome (eg: cloudfare) website in non-official. You have to go through the proper request from the start section on their website https://fontawesome.com/start. Any non-official hosting is prone to problem (eg: legal action, termination of service)
The 4.7 version isn't the youngest, we're now at 5.15.3 public and 6.0.0 pro.
It seems you're trying to load 2 different version and are doing it so without specifying any crossorigin attributes crossorigin="anonymous", which is apparently required.
Remove all fontawesome related scritps and style.
Go through the proper channel https://fontawesome.com/start and get your CDN from there.
Enqueue it with specifying a crossorigin="anonymous" attribute. (see the following snippet).
As icons are usually part of the front line, above the fold, it's a good idea to preload/prefetch them. (see the following snippet).
Setup a local fallback, if the CDN isn't available. (see the following snippet).
Everything has been tested and is working locally and online.
<?php
add_action( 'wp_enqueue_scripts', 'wpso66883439' );
if ( ! function_exists( 'wpso66883439' ) ) {
function wpso66883439() {
if ( ! is_admin() ) {
/**
* Register then enqueue font_awesome_js.
* #link https://developer.wordpress.org/reference/functions/wp_register_script/
* #link https://developer.wordpress.org/reference/functions/wp_enqueue_script/
*
* Check if CDN's url is valid, if not return fallback.
* #link https://www.php.net/manual/en/function.fopen.php
*
* Add rel='preload prefetch' <link> and required attributes to font_awesome_js.
* Filters the HTML link tag of an enqueued style & add required attributes.
* #link https://developer.wordpress.org/reference/hooks/script_loader_tag/
*/
$url_font_awesome_js = 'https://kit.fontawesome.com/13234810930.js'; //... your CDN's url goes here
$ver_font_awesome_js = '5.15.3'; //... script current version goes here
$hnd_font_awesome_js = 'font_awesome_js'; /... script handle
if ( false !== #fopen( $url_font_awesome_js, 'r' ) ) { //... test CDN and continue if success
wp_register_script( $hnd_font_awesome_js, $url_font_awesome_js, array(), $ver_font_awesome_js, true ); //... register via CDN
} else { //... fallback to local if CDN's fail
wp_register_script( $hnd_font_awesome_js, trailingslashit( get_template_directory_uri() ) . 'assets/js/13234810930.js', array(), $ver_font_awesome_js, true ); //... register via local hosting
};
wp_enqueue_script( $hnd_font_awesome_js ); //... enqueue font_awesome_js
add_filter( 'script_loader_tag', 'data_font_awesome_js', 10, 3 );
if ( ! function_exists( 'data_font_awesome_js' ) ) {
function data_font_awesome_js( $tag, $handle, $src ) { //... here we catch the <script> tag before it's loaded and we make some changes to it, add the crossorigin="anonymous" and the rel='preload prefetch' <link>
if ( 'font_awesome_js' === $handle ) {
$tag = str_replace(
array(
"<script",
"></script>",
),
array(
"<link rel='preload prefetch' href='" . esc_url( $src ) . "' as='script' crossorigin='anonymous' /><script",
" crossorigin='anonymous'></script>",
),
$tag
);
};
return $tag;
};
};
};
};
};
?>
Current script is bogus. Request CDN's script # https://fontawesome.com/start.
How can I make my website theme's font the same as Wordpress Gutenberg visual editor default font family ?
Everytime I publish a new post, my website font will default back to the theme's font.
I want to use the Visual Editor font in Wordpress. How to do this?
If I'm not mistaking, Gutenberg default font is based out of your OS. This approach to fonts is used to effectively reset browsers default styling.
font-family: -apple-system,system-ui,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif
One of the possible approach would be using enqueue_block_editor_assets action hook to fire inside the editor and overwrite the font-family selector.
We can use .editor-styles-wrapper to overwrite the font-samilly and set it to whatever we want.
In the following example I chose the Ubuntu font-family from the Google Font website.
Now of course for production use, you would base your font out of your theme's font.
<?php
/**
* Fires after block assets have been enqueued for the editing interface.
* #link https://developer.wordpress.org/reference/hooks/enqueue_block_editor_assets/
* In the function call you supply, simply use wp_enqueue_script and wp_enqueue_style to add your functionality to the block editor.
*/
add_action( 'enqueue_block_editor_assets', function() {
/**
* Register & Enqueue gfont_css.
* #link https://developer.wordpress.org/reference/functions/wp_enqueue_style/
*/
wp_enqueue_style( 'gfont_css', 'https://fonts.googleapis.com/css2?family=Ubuntu:wght#400;700&display=swap', [], wp_get_theme()->version, 'all' ); //... replace by any font, if your theme isn't using Google Font just enqueue a font font from your theme's directory instead and remove the data_gfont_css function below.
/**
* Add mandatory Google Font rel='preconnect' <link> and required attributes to gfont_css
* Filters the HTML link tag of an enqueued style & add required attributes
* #link https://developer.wordpress.org/reference/hooks/style_loader_tag/
*/
add_filter( 'style_loader_tag', 'data_gfont_css', 10, 3 );
function data_gfont_css( $tag, $handle, $src ) {
if( $handle === 'gfont_css' ) {
$tag = str_replace(
"<link rel='stylesheet'",
"<link rel='preconnect' href='https://fonts.gstatic.com'>" . PHP_EOL . "<link rel='stylesheet'",
$tag
);
};
return $tag;
};
} );
/**
* Fires in head section for all admin pages.
* Overwrite default Wordpress Gutenberg default font-familly.
* #link https://developer.wordpress.org/reference/hooks/admin_head/
*/
add_action( 'admin_head', function() {
/**
* Get the current screen object.
* If the screen object is the Gutenberg editor then inject our overwrite.
* #link https://developer.wordpress.org/reference/functions/get_current_screen/
*/
if ( get_current_screen()->is_block_editor() )
echo "<style>.editor-styles-wrapper{font-family:'Ubuntu',sans-serif!important}</style>";
} );
?>
PHP > 7.1 required, anonymous functions used.
I've locally compiled the fontawesome.less to my main.min.css, and also changed the font path in variables.less, but I still having problem with loading the icons? Can't figure out why. I've successfully loaded it through this way on my local html file but can't get through on WordPress. BTW, I'm using WinLess as my LESS compiler.
You have to make sure that your final minified CSS is linking to the fonts. It sounds like you're missing the path to the fonts – check your inspector tool.
Another alternative is to load from a CDN, e.g.
/**
* Enqueue javascript
*
*/
if( !function_exists( "theme_js" ) ) {
function theme_js(){
wp_register_script( 'fontawesome', 'https://use.fontawesome.com/9a8a083038.js',
array('jquery'),
'4.7.1', true);
wp_enqueue_script( 'fontawesome' );
}
}
add_action( 'wp_enqueue_scripts', 'datonomy_theme_js' );
I want to add font awesome icons into Custom Post Type UI Menu Icon area but I couldn't add. How can I do any idea, guys? Thank you.
if you can paste these files into the functions.php and style.css and know the custom post type class looking at the body class given by wordpress
To use Font Awesome for a WordPress Custom Post Type, you’ll need to write a little CSS: just target a CPT menu item (inspect the WordPress admin sidebar to find the correct CSS ID) like this:
#adminmenu #menu-posts-custom_post_type_name .wp-menu-image:before {
content: "\f135"; //find this by clicking on the individual icon on Font
Awesome's site.
font-family: 'FontAwesome' !important;
font-size: 18px !important;
}
Next, add those styles to the WordPress admin by using the admin_head hook:
function namespaced_admin_styles_function() {
echo '<link href="/link/to/admin-styles.css" rel="stylesheet">';
}
add_action('admin_head', 'namespaced_admin_styles_function');
…and you’re off and running! Well, not quite. You still need to add the Font Awesome stylesheet to both the WordPress admin and the front-end of your site. Fortunately, you can kill two birds with one stone this way:
function FontAwesome_icons() {
echo '<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">';
}
add_action('admin_head', 'FontAwesome_icons');
add_action('wp_head', 'FontAwesome_icons');
Refrence from:https://cnpagency.com/blog/3-ways-to-use-icon-fonts-in-your-wordpress-theme-admin/
in your post type array add fontawsome classes like so :
array(
'menu_icon' => 'dashicons-fa fa-book', /* the icon for the custom post type menu. uses built-in dashicons (CSS class name) */
);
Then add these hooks in your functions.php
add_filter( 'sanitize_html_class', function ( $sanitized, $class, $fallback ) {
if ( strpos( $class, 'fa' )
|| strpos( $class, 'fas' )
|| strpos( $class, 'fal' )
|| strpos( $class, 'fab' )
|| strpos( $class, 'far' )
) {
$class = str_replace( 'dashicons-', '', $class );
return $class;
}
return $sanitized;
}, 0, 3 );
and add font awsome in your admin document:
function fontawesome_dashboard() {
wp_enqueue_style('custom-style', get_template_directory_uri().'/assets/styles/all.min.css');
wp_add_inline_style( 'custom-style', '.fa:before,.fas:before,.fal:before,.fab:before,.far:before{font-family:inherit!important;}' );
}
add_action('admin_init', 'fontawesome_dashboard');
In Custom Post type plugin, You can not add font awesome icons. You must be use dashicon class name or icon image url.
Ref. for dashicon class - https://developer.wordpress.org/resource/dashicons/
you can download icon from different different website and use it.
Ref. - http://www.flaticon.com/packs/font-awesome.
I hope it's usefull to you.
you can download font awesome icons as png and upload in wordpress and write full url http://www.example.com/wp-content/uploads/2014/11/your-cpt-icon.png
like above...
icon size must be 20px...
or you can use dashicon.it automatically supports dashicons.
chck this image http://dev.savivatechnologies.com/hpa/wp-content/uploads/2017/07/dashicon.png
Hopes this will help you...