How to Self-Host Google Fonts in Astra?

How to Self-Host Google Fonts in Astra?

Astra theme introduces a new feature called Self-Hosted Google Fonts available with the free theme version 3.6.0. This feature brings CLS and performance improvements by loading and saving the Google Fonts locally in the 「woff2」 format. You can navigate to Customizer > Performance and enable the 「Load Google Fonts Locally」 option to use the feature. Learn more about Self-Hosted Google Fonts in our Blog article.

Though the feature brings great improvements, you might need to make some modifications in specific situations, where you need to use different font file formats or in case you use Astra』s white-labeling feature.

This document will show you how to do these modifications easily.

How To Modify the Feature

One of the cases where you will need to modify the 「Self-Hosted Google Fonts」 feature is when the 「woff2」 font file format doesn』t suit your needs the best. For this reason, we added a filter to change the format of the font files, which will be locally saved. 

Also, this new feature creates a new local fonts folder and local fonts CSS file. Both have the Astra markings in their names which can be changed if you use the white-labeling feature. You can add a filter to remove Astra markings and assign the names as needed.

By using these filters, you will be able to make the mentioned changes on your website related to this feature while still benefiting from all the improvements it brings.

Before we move to the filters, it』s important to mention that it is advisable to have a Child Theme installed when adding any custom code to your website. If you need more details using a Child Theme check out this article. Also, we suggest you try doing this using a Staging environment and not your live (production) website directly.

How to Load Font File Formats Other Than 「woff2」?

Though the 「woff2」 comes with its advantages, it might not be a perfect fit for every website. 

The 「woff2」 format will work with around 96% of the currently used browsers. However, some browsers like Internet Explorer and Opera Mini do not support it. The solution for this is to load different font file formats.

In this case, you would just need to change the font file format to 「woff」, which is a supported format with these browsers. 

To change the font file format to 「woff」, please add the following filter to the functions.php file of your Child Theme:

// Load font formats other than "woff2"

add_filter( 'astra_local_google_fonts_format', 'update_astra_local_fonts_format' );
function update_astra_local_fonts_format( $font_format ) {
$font_format = 'woff'; // This will change format to "woff" - you can also use other format
return $font_format;
}

You can change the font file format to any other too using the same filter – just replace the 「woff」 with another format in this line of the filter code:

$font_format = 'woff'; // This will change format to "woff"

How to White-Label This Feature?

As mentioned, the 「Self-Hosted Google Fonts」 feature will create the font folder and local fonts CSS file, containing the traces of Astra affiliation in their names. If your clients inspect the website code or the website folders, they will be able to see the mentioned Astra markings (and not your own brand). 

If you wish to change the names of the font folder and/or local fonts CSS file, you can do this by adding the following two filters to the functions.php file of your Child Theme:

To rename the font folder name use this code:

// Update 'astra-local-fonts' directory name where font CSS will be located.

add_filter( 'astra_local_fonts_directory_name', 'update_astra_local_fonts_file_directory_name' );
function update_astra_local_fonts_file_directory_name( $folder_name ) {
$folder_name = 'mysite'; // So finally your local font directory name will be 'mysite'.
return $folder_name;
}

To rename the local fonts CSS file name use this code:

// Update 'astra-local-fonts.css' file name.

add_filter( 'astra_local_font_file_name', 'update_astra_local_fonts_file_name' );
function update_astra_local_fonts_file_name( $file_name ) {
$file_name = 'mysite-font'; // So finally your local font file name will be 'mysite-font.css'.
return $file_name;
}

Once you added the codes, please navigate to Customizer > Performance and flush the local fonts cache by clicking on the 「Flush Local Font Files」 button. This will make the effects of the above-mentioned codes immediately visible.

Filter to Add Global Button Settings Support for WooCommerce Buttons

Filter to Add Global Button Settings Support for WooCommerce Buttons

Are you looking to add Global Button Settings Support for the WooCommerce Buttons? Now with the Astra Theme version 2.5.0, will be resolved –

For New Users:

On your fresh or new installations, compatibility of Global Button Settings will be provided for the WooCommerce Buttons by default.

For Existing Users:

You can use the following filter in the child theme』s functions.php to provide Global Button』s Support for the WooCommerce buttons.

add_filter( 'astra_global_btn_woo_comp' , '__return_true' );

Note

Refer this doc on How to Add Custom PHP Code?

Deprecated Google Fonts Filter

Deprecated Google Fonts Filter

From the Astra theme version 2.5.0, we are making some changes on how the Google Fonts file will be served. Previously, this used to cause a rare error with WP_Filesystem() on some servers.

For the same, we are deprecating the filter: astra_google_fonts_json_file

Now, instead of the above filter, users will now be using new filter: astra_google_fonts_php_file

Note: For users who are getting this PHP Notice after the update –

Deprecated: astra_google_fonts_json_file is deprecated since version 2.5.0! Use instead. `astra_google_fonts_php_file`.

If you need to switch their Google Fonts file from JSON to PHP you can simply do it using this new filter –

add_filter('astra_google_fonts_php_file', 'asrta_add_new_google_php_file');

function asrta_add_new_google_php_file() {
return get_stylesheet_directory() . '/google-new-fonts.php';
}

And here』s the link to the demo fonts file and the array how you should add and insert new Google fonts. You can modify the file path in the above filter based on the file name – google-new-fonts.php.

Note: Add the above code into the child theme』s functions.php file.

Add Title attribute to Header Background Image as a Substitute for Alt Text

Add Title attribute to Header Background Image as a Substitute for Alt Text

Astra Pro Addon offers the option to set a background image for the header. This image is added using CSS background-image Property. This property does not have an option to add an alt attribute to the image.

So as an alternative to the alt attribute, we can add a title attribute to the div. This will act as an alt text in the background-image property.

The following filter will add a title attribute to the div of the header, where the background image is added using CSS property.

add_filter( 'astra_attr_main-header-bar', 'astra_function_to_add_title' );

/**
* Add Attribute to the Header.
*
* @param array $attr Default attributes.
* @return array $attr Modified attributes.
*/
function astra_function_to_add_title( $attr ) {
$header_bg_obj = astra_get_option( 'header-bg-obj-responsive' );
$image_alt_text = get_post_meta( attachment_url_to_postid( $header_bg_obj['desktop']['background-image'] ), '_wp_attachment_image_alt', true );
$attr['title'] = $image_alt_text;
return $attr;
}

Alt text added for image –

Alt text will appear as title for the image in HTML markup –

How to Design Bullets & Lists?

How to Design Bullets & Lists?

WordPress Block Editor by default provides a simple design for bullets and lists. Below is the screenshot of how you can add a simple code for lists with WordPress Block Editor. We used the 「HTML」 blocks for adding the codes, and the 「Paragraph」 blocks for adding the regular text:

This is a screenshot of how this would look on the frontend:

If you would like to add some style to bullets and lists, you can use the following CSS. Below are the steps that show how to use CSS.

Step 1 – Add the following CSS classes to ol (ordered lists) and ul (unordered list) with the following HTML codes:

Code for ol (ordered lists) – 『ast-content-ol-list』

  1. One
  2. Two
  3. Three
  4. Four
  5. Five

Code for ul (ordered lists) – 『ast-content-ul-list』

  • Red
  • Blue
  • White
  • Yellow
  • Green

You can modify the number of list items by removing lines or adding new ones (

  • ITEM_NAME
  • ).

    Step 2 – Now it』s time to style your lists. Navigate to the Customizer and copy the following custom CSS to your website』s 「Additional CSS」.

    /* Order list style */ .entry-content ol.ast-content-ol-list {
    counter-reset: my-ol-counter;
    margin-left:1.4em;
    }
    .entry-content ol.ast-content-ol-list li {
    position: relative;
    padding-left: 30px;
    list-style: none;
    }
    .entry-content ol.ast-content-ol-list li:before {
    position: absolute;
    top:50%;
    left: 0;
    width: 20px;
    height: 20px;
    margin-top: -10px;
    color: #fff;
    text-align:center;
    background-color: #0274be;
    content: counter(my-ol-counter,decimal);
    counter-increment: my-ol-counter;
    font-size: 11px;
    -webkit-border-radius: 50%;
    border-radius: 50%;
    }

    /* Unordered list style */
    .entry-content ul.ast-content-ul-list {
    margin-left:1.4em;
    }
    .entry-content ul.ast-content-ul-list li {
    position: relative;
    padding-left: 30px;
    list-style: none;
    }
    .entry-content ul.ast-content-ul-list li:after{
    position: absolute;
    top: -webkit-calc( 50% - -1px);
    top: calc( 50% - -1px);
    left: 0; width: 20px;
    height: 20px;
    margin-top: -10px;
    padding-top: 2px;
    color: #fff;
    text-align:center;
    background-color: #0274be;
    content: "e900";
    font-size: 10px;
    font-family: 'Astra';
    -webkit-border-radius: 50%;
    border-radius: 50%;
    -webkit-transform: rotate(270deg);
    -ms-transform: rotate(270deg);
    transform: rotate(270deg);
    }

    If you』re not sure how to do this, please check this article on adding s custom CSS.

    The result of adding the above CSS is the following look of your bullets and lists:

    You can modify the color, font, spacing, and size of the elements by modifying the appropriate lines of the CSS code.

    Fix Spacing Issues in Gutenberg Blocks

    Fix Spacing Issues in Gutenberg Blocks

    Starter Templates plugin offers an option to import Gutenberg Templates and Blocks Patters. You can directly import templates inside the block editor.

    Sometimes, after importing a template, you might observe some spacing or alignment changes compared to the original template you previewed before importing.

    If you are experiencing some spacing differences after importing Gutenberg templates or block patterns, you can fix it quickly.

    All you have to do is use a simple custom code.

    Paste the following code in your child theme』s functions.php file.

    add_filter( 'astra_gutenberg_patterns_compatibility', '__return_true' );

    Note: If you are an existing Astra user and updating it from previous versions to v3.3, then only you will need this. If you start using Astra from version 3.3 and above you will not need to add this code.

    Change the Astra Header Breakpoint Width

    Change the Astra Header Breakpoint Width

    In Astra Theme you can modify the screen size when you want to trigger the mobile header using the filter astra_header_break_point The default breakpoint is 921px. That means the mobile header will be triggered at 921px screen size.

    Note:

    If you are using {Astra』s Header Footer Builder} the below filter will not work, but to change the breakpoint you can go with updating tablet breakpoint as mentioned in this article. [Link here]

    To change the default breakpoint use filter. In the below example the mobile header will be triggered at 1120px screen size. Change 1120 to the screen size of your choice.

    /**
    * Change the breakpoint of the Astra Header Menus
    *
    * @return int Screen width when the header should change to the mobile header.
    */
    function your_prefix_change_header_breakpoint() {
    return 1120;
    };

    add_filter( 'astra_header_break_point', 'your_prefix_change_header_breakpoint' );

    Check out the doc that explains how to add custom PHP code using the child theme』s functions.php file. 

    Note:

    Astra version 1.4.0 and above provides an option to change the breakpoint. You don』t have to use the above filter. Refer to the document here.Further, after Astra 3.0.0 in the Astra』s Header Footer builder, we have provided a Mobile header that can be added for the Mobile devices. [Learn More]

    How to Add Custom PHP Code?

    How to Add Custom PHP Code?

    In many cases, you will need to add custom PHP code in the theme. In this article, let us explain some ways how you can do so:

    1. Custom Layouts module in Astra Pro (Easiest)

    Astra Pro Addon is a premium plugin that extends features of Astra theme. If you have purchased Astra Pro Addon, use the Custom Layouts module to add custom PHP code. Below are the steps that show how to add code –

    Activate the Custom Layouts module from Astra Pro.Insert the code in the Code Editor.Select Hooks layout and choose the appropriate action / wp_head action.Choose to display it on the Entire WebsitePublish the layout.

    Here is an example that shows how to add Google Analytics code. Similarly, you can add any custom code.

    2. With Third-Party Plugin

    There are free plugins available on WordPress Repository that you can use to add custom PHP code. Below are two of them:

    Code SnippetsMy Custom Functions

    3. In Child Theme

    You can add a filter or custom PHP code through the WordPress Dashboard, follow the steps on how you can do the same –

    Step 1: Install and Activate the Astra Child Theme from the WordPress Dashboard > Appearance > Themes > Astra Child Theme.

    Step 2: Open the Theme Editor under the Appearance. Not seeing Theme Editor, troubleshoot the same.

    Step 3: Navigate to the child theme』s functions.php, from the right-hand section and select functions.php.

    Step 4: Add your code at the bottom of the file and click on Update File at the bottom.

    Note: If you have the Parent theme active and visit the Theme Editor, you will see the following message, which informs you about how directly editing your theme could crash your website. Click on 「I understand」. 

    Or if you have access to cPanel of your website, you can use FTP software like FileZilla. Below are the steps that explain how to do so –

    Step 1: Install and activate the Astra Child Theme from the WordPress Dashboard.

    Step 2: Open your WordPress site via FTP. We would recommend you to use FileZilla.

    Step 3: Navigate to wp-content > themes > astra-child

    Step 4: Right-click functions.php and select View/Edit

    Step 5: Add your code at the bottom of the file and save it.

    Change the 「Shopping Cart」 Text for WooCommerce & EDD Mobile Header Cart

    Change the 「Shopping Cart」 Text for WooCommerce & EDD Mobile Header Cart

    If you have the WooCommerce or EDD cart added to your websites』 mobile header, you might want to modify the cart title. Since Astra Theme version 3.5.0, this can easily be done.

    With Astra Theme versions 3.5.0 and above you can change the default 「Shopping Cart」 text to anything you need to. 

    To do this, you would need to add a filter to the functions.php file in your Astra Child Theme. Please add the following code:

    add_filter( 'astra_header_cart_flyout_shopping_cart_text', function() {
    return 'My Shop Flyout Cart';
    } );

    You can change the title to any text you want – just modify My Shop Flyout Cart text in the code above.

    Note:

    For additional details on how to add a custom code to the functions.php file please see this article: How to Add Custom PHP Code?