Custom Code Snippets – Astra https://wpastra.com Fast, Lightweight & Customizable WordPress Theme for Any Website Wed, 10 May 2023 09:40:22 +0000 en-US hourly 1 https://wordpress.org/?v=6.2.2 https://wpastra.com/wp-content/uploads/2020/01/favicon-astra2x.png Custom Code Snippets – Astra https://wpastra.com 32 32 Introducing New Filter to Enable/Disable Rank-Math Theme Support https://wpastra.com/docs/introducing-new-filter-to-enable-disable-rank-math-theme-support/ https://wpastra.com/docs/introducing-new-filter-to-enable-disable-rank-math-theme-support/#respond Tue, 09 May 2023 06:59:29 +0000 https://wpastra.com/?post_type=docs&p=151788 Introducing New Filter to Enable/Disable Rank-Math Theme Support Read More »

]]>
We have received multiple requests about the breadcrumb option in the Rank Math SEO plugin. It is observed that while using Astra 3.6.4 and above, many of the useful features of the plugin do not work. In this article, we will explain how you can fix that issue.

Why this is happening?

Basically, we have added theme support for Rank math breadcrumb in Astra which is why the plugin makes the breadcrumb setting disable in the latest version of the Astra theme.

How to fix this?

To fix this issue, you will need to add the following filter to the functions.php of your website. If you are not sure how to add custom PHP codes to your website, here is an article that will guide you.

add_filter( 'astra_rank_math_theme_support', '__return_false' );

Please note that we highly recommend you take a backup of your site before making any changes to your functions.php. Here is an article that will help you take a quick backup of your website.

]]>
https://wpastra.com/docs/introducing-new-filter-to-enable-disable-rank-math-theme-support/feed/ 0
Changing the Post Title’s Tag of the Related Post Section https://wpastra.com/docs/changing-the-post-titles-tag-of-the-related-post-section/ https://wpastra.com/docs/changing-the-post-titles-tag-of-the-related-post-section/#respond Fri, 09 Dec 2022 07:42:43 +0000 https://wpastra.com/?post_type=docs&p=142007 Changing the Post Title’s Tag of the Related Post Section Read More »

]]>
If you have enabled related posts on your Blog posts, you might have noticed that the Post Title’s Tag of the Related Post Section has the <h3> heading tag assigned by default.

You can modify this tag to any other HTML tag by adding a filter to the functions.php file of your Astra Child Theme. If you don’t have a Child Theme installed and activated on your website, please check this article on how to do this

How Can I Do This?

You can use the filter below to change the Post Title’s Tag of the Related Post Section to any other tag. Please follow these steps:

Step 1 – Navigate to Dashboard > Appearance > Theme Editor and select the Child Theme;

Step 2 – Add the filter below to the functions.php file of your Child Theme. You can check this article on adding code to your Child Theme to help you out;

add_filter( 'astra_related_post_title_tag', 'update_related_posts_title_tag' );
function update_related_posts_title_tag(){
    return 'span';
}

Step 3 – The span in the code above can be changed to any preferred tag. If you add return ‘h4’; , click the “Update File” button to apply changes. The filter will change the default <h3> tag to the <h4> tag.

Default Tag

Post Title’s Tag of the Related Post Section after adding the Filter

]]>
https://wpastra.com/docs/changing-the-post-titles-tag-of-the-related-post-section/feed/ 0
Show Password eye icon not visible https://wpastra.com/docs/show-password-eye-icon-not-visible/ Wed, 21 Sep 2022 10:50:34 +0000 https://wpastra.com/?post_type=docs&p=133549 Show Password eye icon not visible Read More »

]]>
This document will help you to toggle the visibility of a password input by using a custom filter.

If you have a WooCommerce website built using the Astra theme, and whenever you are logging into the WooCommerce – My Account, you need to provide the Username/ email address and Password. A password field provides a way for users to securely input a password by showing the * character instead of the actual characters.

However, it is likely that some users will type the wrong password. To help them see the password that they’re currently entering, the eye icon beside the password field will toggle the password visibility. This is a very useful feature you should have on your login form.

Previously the eye icon for show password was not visible.

Show Password eye icon not visible

So, to make the show password eye icon visible, you need to place the following code in the functions.php file.

add_filter( 'astra_get_option_woo-show-password-icon', '__return_true' );

After adding the above filter, the Show Password eye icon will be visible.

Show Password eye icon not visible

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?

]]>
Revert support for Single Product’s legacy Gallery layouts https://wpastra.com/docs/revert-support-for-single-products-legacy-gallery-layouts/ https://wpastra.com/docs/revert-support-for-single-products-legacy-gallery-layouts/#respond Wed, 21 Sep 2022 06:06:26 +0000 https://wpastra.com/?post_type=docs&p=133511 Revert support for Single Product’s legacy Gallery layouts Read More »

]]>
We have listed down a filter that will help you Structure the WooCommerce Single Product’s legacy Gallery layouts.

This is a premium feature available with the Astra Pro Addon plugin. To use these Pro features, you need to have the Astra theme along with the Astra Pro Addon installed on your website.

Prior to the Astra Pro v3.9.2, you can see 5 Gallery layout options for WooCommerce Single Product. Have a glance at the below-attached screenshot once for a better understanding.

On a fresh new installation, with Astra Pro v3.9.2, the first two Gallery Layout are missing.

This is because we have deprecated legacy layouts. To get the Single Product’s legacy Gallery layouts back, you can make use of the below-attached filter. On adding the following filter in the functions.php file, you can see the 5 Gallery layout options.

add_filter( 'astra_get_option_astra-product-gallery-layout-flag', '__return_false' );

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?

]]>
https://wpastra.com/docs/revert-support-for-single-products-legacy-gallery-layouts/feed/ 0
Revert support for old Astra cart icon https://wpastra.com/docs/revert-support-for-old-astra-cart-icon/ Tue, 20 Sep 2022 11:20:39 +0000 https://wpastra.com/?post_type=docs&p=133426 Revert support for old Astra cart icon Read More »

]]>
When using WooCommerce with the Astra theme installed, you can customize the Cart icon. You can select the cart icon of your choice from the available options in the customizer settings.

Navigate to Appearance > Customize > Header builder > Cart. Here, you can select the cart icon from the available 3 options.

In case you want to add the CSS Astra cart icons, you need to add the following custom code to the functions.php file.

add_filter( 'astra_woocommerce_cart_icon', '__return_false' );

After adding the above filter, the cart icons customizing options would look like this.

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?

]]>
How to remove horizontal & vertical gallery layouts from a single product page? https://wpastra.com/docs/how-to-remove-horizontal-vertical-gallery-layouts-from-a-single-product-page/ https://wpastra.com/docs/how-to-remove-horizontal-vertical-gallery-layouts-from-a-single-product-page/#respond Mon, 12 Sep 2022 08:35:27 +0000 https://wpastra.com/?post_type=docs&p=132804 How to remove horizontal & vertical gallery layouts from a single product page? Read More »

]]>
In the Astra Pro update v3.9.0, we have released 2 new gallery layout options for a single product, i.e. Horizontal & vertical gallery. But as these are custom gallery Astra layouts, You have faced an issue when gallery layouts are not working or look improper with theme builder single product layouts.

If you previously prepared any “Single Product” layout (v3.9.0), you could face the gallery layouts missed on the front-end single product page. Keeping in mind the popular page builder themer layouts, we fixed this issue in our last v3.9.2 update. But if you still come across the situation where this gallery layout is not required on your single product layout on the front end, you can use the newly added filter.

So here is the filter to disable those custom galleries and let the layout/custom layout work as per their way.

add_filter( 'astra_addon_override_single_product_layout', '__return_false' );
]]>
https://wpastra.com/docs/how-to-remove-horizontal-vertical-gallery-layouts-from-a-single-product-page/feed/ 0
Transparent Header Menu Color not working with Mobile Breakpoint https://wpastra.com/docs/transparent-header-menu-color-not-working-with-mobile-breakpoint/ https://wpastra.com/docs/transparent-header-menu-color-not-working-with-mobile-breakpoint/#respond Mon, 12 Sep 2022 08:14:08 +0000 https://wpastra.com/?post_type=docs&p=132809 Transparent Header Menu Color not working with Mobile Breakpoint Read More »

]]>
Astra offers an option to change Menu Breakpoint for Mobile Header ( setting – Layout > Header > Primary header > Mobile header ). When you increase this breakpoint, a transparent menu color does not work on devices with the width from 768px to Menu Breakpoint width.

To fix this add the following code to child theme’s functions.php file.

/**
* Function to disable the transparent header below 800px screen
*
* @return void
*/
function disable_transparent_header_mobile() {
    ?>
    <script type="text/javascript" >
    jQuery(window).on('resize', function(){
        if ( '800' >= jQuery( window ).width() ) {
            if ( jQuery( 'body' ).hasClass( 'ast-theme-transparent-header' ) ) {
                jQuery( 'body' ).removeClass( 'ast-theme-transparent-header' );
            }
        } else {
            if ( !jQuery( 'body' ).hasClass( 'ast-theme-transparent-header' ) ) {
                jQuery( 'body' ).addClass( 'ast-theme-transparent-header' );
            }
        }
    });
    jQuery( document ).ready(function() {
        if ( '800' >= jQuery( window ).width() ) {
            if ( jQuery( 'body' ).hasClass( 'ast-theme-transparent-header' ) ) {
                jQuery( 'body' ).removeClass( 'ast-theme-transparent-header' );
            }
        } else {
            if ( !jQuery( 'body' ).hasClass( 'ast-theme-transparent-header' ) ) {
                jQuery( 'body' ).addClass( 'ast-theme-transparent-header' );
            }
        }
    });
    </script>
    <?php
}

add_action( 'wp_footer', 'disable_transparent_header_mobile' );

Important Note: Please replace 800 with the Menu Breakpoint width you setup in the customizer.

]]>
https://wpastra.com/docs/transparent-header-menu-color-not-working-with-mobile-breakpoint/feed/ 0
How to add custom CSS and JavaScript to Astra theme https://wpastra.com/docs/how-to-add-custom-css-and-javascript-to-astra-theme/ https://wpastra.com/docs/how-to-add-custom-css-and-javascript-to-astra-theme/#respond Tue, 30 Aug 2022 11:07:26 +0000 https://wpastra.com/?post_type=docs&p=131568 How to add custom CSS and JavaScript to Astra theme Read More »

]]>
It’s crucial for designers and developers to know how to properly include JavaScript and CSS files in WordPress themes and plugins. Not just because they should function properly but also for the reason that they should not affect the theme or any of the plugins on your website.

Now, there are several ways that you can use to insert custom CSS and javascript in your website; however, in this document, we will share some of the best practices.

What is custom CSS?

Cascading Style Sheets, or CSS, is a way to add styling directives to the back-end code of your website. It’s a crucial component of your website that establishes its appearance and feel. Your website will just be a collection of unattractive scripts without CSS.

When you install a theme or import a template, a Style.PHP file is created on your website. This is where the code that tells WordPress on how your website should look, will be saved. When you override the existing look of your website by using customized code and save it in the style. css file, those codes are called custom CSS.

What is a JavaScript Query?

Javascript is a scripting language that enables you to do just about everything, including creating dynamically updated material and managing multimedia. It is remarkable what you can accomplish with a few lines of JavaScript code. To answer simply. Javascript adds the functions to your website.

Method 1: Inserting the Custom CSS and JavaScript to your website directly

By default, WordPress creates <Style> tags for the CSS and <Script> tags for the JavaScript Queries. So, if you are inserting any internal CSS, you will need to wrap that up within the <style > tag and for the internal JavaScript queries, you will need to wrap it up within the <script> tag. For example, the WordPress will understand that the following code is a CSS and will make all the header 1 and header 2 have a background color of the color code #7bdcb5.

Add_action( ‘admin.head’, ‘custom_internal_css’);
Function custom_internal_css(){
<style>
.h1, h2 {
background-color: #7bdcb5;
}
</style>;
}

Now if this is a JavaScript query, we will need to wrap it up with the <script> and </script> tags. For example, 

add_action( 'astra_entry_before', function() {
	<script>
	add_filter( 'astra_get_option_enable-related-posts', '__return_true' );
	</script>;
} );

WordPress will understand that this is a Javascript code and it enables the related post option on your website.

If you are inserting an external code which is not there on your theme or website, you must use the action hook

wp_enqueue_scripts

You can not just print <script> or <link> tags inside these hooks, you have to use special functions, such as

  • wp_register_script() / wp_register_style()
  • wp_deregister_script() / wp_deregister_style()
  • wp_enqueue_script() / wp_enqueue_style()
  • wp_localize_script()
  • wp_add_inline_script()

There are more functions like this on the WordPress directory; however, generally using these hooks are enough. For an example, 

add_action( 'wp_enqueue_script', function() {
	Wp_enqueue_script( add_filter
{'astra_get_option_enable-related-posts', '__return_false' 
};
} );

Method 2: Using the Astra Hooks plugin

You can also do this using a simple plugin named Astra Hooks that we have developed. You can download this amazing free plugin from here. Astra uses the WordPress Hooks API to insert actions throughout the theme. This plugin allows you to “hook” your custom Content, Shortcodes or Java Script code in the various hook locations. You can learn more about this plugin here.

]]>
https://wpastra.com/docs/how-to-add-custom-css-and-javascript-to-astra-theme/feed/ 0
Remove default stretched block layout spacing https://wpastra.com/docs/remove-default-stretched-block-layout-spacing/ https://wpastra.com/docs/remove-default-stretched-block-layout-spacing/#respond Sun, 24 Jul 2022 07:49:38 +0000 https://wpastra.com/?post_type=docs&p=129019 Remove default stretched block layout spacing Read More »

]]>
With the latest update of the Astra theme, we have added a spacing around the full width streched layout for elegant looks of the streched layout.

Remove default stretched block layout spacing

If you are looking to disable the spacing and would like to get the old look of the streched layout, you will need to add the following filter to the function.php file of your child theme.

add_filter( 'astra_stretched_layout_with_spacing', '__return_false' );

This is how it will look after adding the above code to the function.php file of your child theme.

ast-full width streched layout spacing removed

If you are not sure how to add the code to the function.php file, here is an article that will guide you through. If you are not aware what is a child theme and how to install a child them, here is an article that will be helpful.

]]>
https://wpastra.com/docs/remove-default-stretched-block-layout-spacing/feed/ 0
How to Remove Google Fonts Suggestions in Astra Theme? https://wpastra.com/docs/remove-google-fonts-suggestions-in-astra-theme/ https://wpastra.com/docs/remove-google-fonts-suggestions-in-astra-theme/#respond Tue, 05 Apr 2022 02:51:31 +0000 https://wpastra.com/?post_type=docs&p=120715 How to Remove Google Fonts Suggestions in Astra Theme? Read More »

]]>
Astra theme comes with built-in Google Font Families. Astra’s build process automatically checks all Google Fonts and lists them in Customizer options.

Astra typography settings

How To Remove Google Fonts Suggestions?

To remove Google Fonts suggestions from the Customizer.

You can insert the following filter in your child theme’s functions.php file –

add_filter( 'astra_google_fonts', '__return_empty_array' );

If you don’t have your Child Theme installed, please check this article on how to do it.

If you are not sure how to add this code, please check this article.

]]>
https://wpastra.com/docs/remove-google-fonts-suggestions-in-astra-theme/feed/ 0