skip to Main Content

I’m Trying to add custom fonts in my WordPress development theme, but I got fatal error here how I add it

/// Register fonts.
    Wp_register_style('font-awesome', get_stylesheet_uri(), [], filemtime get_template_directory_uri().'/fonts/font-awesome-webfont.woff', [], false, 'all');
    wp_register_style('Naskh', get_stylesheet_uri(), [], filemtime get_template_directory_uri().'/fonts/naskh-webfont.woff', [], false, 'all');
    wp_register_style('Roboto', get_stylesheet_uri(), [], filemtime, get_template_directory_uri().'/fonts/roboto-webfont.woff', [], false, 'all');`


`/// Enqueue Fonts.
    wp_enqueue_style('font-awesome-webfont.woff');
    wp_enqueue_style('naskh-webfont.woff');
    wp_enqueue_style('roboto-webfont.woff');`

and in the css i add it like this

`--arabic-fonts:src='\fonts\naskh-webfont.woff';
--english-fonts:src='\fonts\roboto-webfont.woff';`

`[dir="rtl"] .container {font-family:var(--arabic-fonts);}
[dir="ltr"] .container {font-family:var(--english-fonts);}`

Please help me Thanks For Your Reply I appreciate it

I tried Google It But Could Not Found Any Possible Fix

here the error that i got and sorry

Fatal error: Uncaught Error: Undefined constant "filemtime" in C:xampphtdocsMyWebSitewp-contentthemesCopperfunctions.php:16 Stack trace: #0 C:xampphtdocsMyWebSitewp-includesclass-wp-hook.php(324):
copper_enqueue_scripts(”)
#1 C:xampphtdocsMyWebSitewp-includesclass-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array)
#2 C:xampphtdocsMyWebSitewp-includesplugin.php(517): WP_Hook->do_action(Array)
#3 C:xampphtdocsMyWebSitewp-includesscript-loader.php(2265): do_action(‘wp_enqueue_scri…’)
#4 C:xampphtdocsMyWebSitewp-includesclass-wp-hook.php(324): wp_enqueue_scripts(”)
#5 C:xampphtdocsMyWebSitewp-includesclass-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array)
#6 C:xampphtdocsMyWebSitewp-includesplugin.php(517): WP_Hook->do_action(Array)
#7 C:xampphtdocsMyWebSitewp-includesgeneral-template.php(3050): do_action(‘wp_head’)
#8 C:xampphtdocsMyWebSitewp-contentthemesCopperheader.php(13): wp_head()
#9 C:xampphtdocsMyWebSitewp-includestemplate.php(810): require_once(‘C:xampphtdocs…’)
#10 C:xampphtdocsMyWebSitewp-includestemplate.php(745): load_template(‘C:xampphtdocs…’, true, Array)
#11 C:xampphtdocsMyWebSitewp-includesgeneral-template.php(48): locate_template(Array, true, true, Array)
#12 C:xampphtdocsMyWebSitewp-contentthemesCopperindex.php(7): get_header()
#13 C:xampphtdocsMyWebSitewp-includestemplate-loader.php(106): include(‘C:xampphtdocs…’)
#14 C:xampphtdocsMyWebSitewp-blog-header.php(19): require_once(‘C:xampphtdocs…’)
#15 C:xampphtdocsMyWebSiteindex.php(17): require(‘C:xampphtdocs…’)
#16 {main} thrown in C:xampphtdocsMyWebSitewp-contentthemesCopperfunctions.php on line 16
There has been a critical error on this website.

and the problem is that VS Code doesn’t show any error in the editor

2

Answers


  1. When you use word without $ like here filemtime PHP treat it like a CONSTANT, so you need to defined it first.

    The fourth argument of this function defined version of your script. You can set there FALSE (which is default behaviour) or defined the constant first. For example on the top of your file where you try to put this code:

    define('VERSION', '1.0.0' );

    then

    wp_register_style('Naskh', get_stylesheet_uri(), [], VERSION, get_template_directory_uri().'/fonts/naskh-webfont.woff', [], false, 'all');
    
    Login or Signup to reply.
  2. You used the filemtime function incorrectly.

    try this:

    // Register fonts.
    wp_register_style('font-awesome', get_template_directory_uri().'/fonts/font-awesome-webfont.woff', [], filemtime(get_template_directory().'/fonts/font-awesome-webfont.woff'), 'all');
    wp_register_style('Naskh', get_template_directory_uri().'/fonts/naskh-webfont.woff', [], filemtime(get_template_directory().'/fonts/naskh-webfont.woff'), 'all');
    wp_register_style('Roboto', get_template_directory_uri().'/fonts/roboto-webfont.woff', [], filemtime(get_template_directory().'/fonts/roboto-webfont.woff'), 'all');
    
    // Enqueue Fonts.
    wp_enqueue_style('font-awesome');
    wp_enqueue_style('Naskh');
    wp_enqueue_style('Roboto');
    

    and css use the "url" instead of using "src". use the correct path to your file and remove unecessary back slashes

    :root {
      --arabic-fonts: url('/fonts/naskh-webfont.woff');
      --english-fonts: url('/fonts/roboto-webfont.woff');
    }
    
    [dir="rtl"] .container {
      font-family: var(--arabic-fonts);
    }
    
    [dir="ltr"] .container {
      font-family: var(--english-fonts);
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search