Important WordPress Functions You Should Know - SkillBakery Studios

Breaking

Post Top Ad

Post Top Ad

Monday, December 21, 2020

Important WordPress Functions You Should Know

 WordPress is now used by bloggers and companies because it is easy to use. Also, customizing it is a big part of the platform’s appeal. That is why a large portion of the internet’s websites are built on this system. learning how to personalize that part of your WordPress site accurately requires you first need to understand what functions are and what they can do to your website. We will look into the definition of function and talk about some useful tricks regarding them. Ready to learn? In this guide, you will understand it properly. 

What are WordPress functions?

Before we get started on some key WordPress functions, we need to understand how they work and why they are relevant. WordPress is built on PHP, a general-purpose scripting language that meets web development needs. PHP is especially useful to create content management systems like WordPress because it enables interaction with a database and data fetching, outputting it for the user as HTML. WordPress’ tags and functions are all built on PHP, and they are what makes this CMS so great. Anyone knowing just a bit of coding can easily change PHP scripts, which are highly flexible. That allows owners to adjust each element on their WordPress websites.

5 WordPress functions you need to learn right now

1.Add Google Fonts to your WordPress

Google Fonts are a great way to customize your website. It implements different fonts in your WordPress installation. It can help you reproduce your company’s branding, as used on other marketing materials. There are a few plugins you can use to customize the fonts of your website, but you can also do so by editing your theme’s functions.php file. You first need to visit the Google Fonts website, where you can find various fonts — as well as their weights — that you need to customize your website. Select those you are interested in and add them to your cart.

Then, click on the “Embed” tab and copy the href value. That address shows where your font is and the weights you selected when visiting Google Fonts. Make sure you also take note of the CSS rules that apply to your font family. You will need to update them for the fonts to work on your website.

Source: Laborator

Now, all you have to do is visit your theme’s function.php page and add the following code, replacing the href with your font’s href.

<?php
function google_fonts() {
    wp_enqueue_style( 'google-fonts', 'https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,400;0,700;1,400;1,700&display=swap', false );
}
add_action( 'wp_enqueue_scripts', 'google_fonts' );

Having done that, add to your CSS the rules defined by Google Fonts. They will look something like this:

font-family: ‘Open Sans’, sans-serif;

Save changes, and you are all set to use the Google Font of your choosing.

2. Remove scripts from header to footer

Sometimes, when you buy a WordPress theme, some styles, scripts, and other complements come pre-installed, but they might not be useful to your site. These complements can make your website load slower, which in turn can affect your SEO efforts negatively. One option is to remove all the scripts from header to footer with a simple WordPress function. To do that, all you will need is to dequeue some extra WordPress JavaScripts on the parent theme.You can also do that by using a plugin, although a function works as well. Just add this into the page template:

/**
 * Dequeue the Parent Theme scripts.
 *
 * Hooked to the wp_print_scripts action, with a late priority (100),
 * so that it is after the script was enqueued.
 */
function my_site_WI_dequeue_script() {
    wp_dequeue_script( 'comment-reply' ); //If you're using disqus, etc.
    wp_dequeue_script( 'jquery_ui' ); //jQuery UI, no thanks!
    wp_dequeue_script( 'fancybox' ); //Nah, I use FooBox
    wp_dequeue_script( 'wait_for_images' );
    wp_dequeue_script( 'jquery_easing' );
    wp_dequeue_script( 'swipe' );
    wp_dequeue_script( 'waypoints' );
}

add_action( 'wp_print_scripts', 'my_site_WI_dequeue_script', 100 );

/**
 * Dequeue the Parent Theme styles.
 *
 * Hooked to the wp_enqueue_scripts action, with a late priority (100),
 * so that it runs after the parent style was enqueued.
 */

function give_dequeue_plugin_css() {
    wp_dequeue_style('additional-parent-style');
    wp_deregister_style('additional-parent-style');
}
add_action('wp_enqueue_scripts','give_dequeue_plugin_css', 100);

3. Disable “continue reading” for your WordPress blog posts

Many templates use the “Continue reading” feature to make sure your blog posts will not crowd a page. But, sometimes, you do not want that to appear on your website.To remove it using the functions.php file, all you need to do is add the following code in your theme, replacing “twentyeleven” with your theme file name:

function twentyeleven_continue_reading_link() {
    return ' <a href="'. esc_url( get_permalink() ) . '">' . __( '[...]', 'twentyeleven' ) . '</a>';
}
endif;

function twentyeleven_auto_excerpt_more( $more ) {
    return '' . twentyeleven_continue_reading_link();
}
add_filter( 'excerpt_more', 'twentyeleven_auto_excerpt_more' );

4. Pull all metadata from a post with a function

Sometimes, you will need the metadata of your website for a specific post or page. That is much harder to do with a plugin, compared to a simple change in your functions.So, if you need to obtain metadata from your posts, add the following code to your theme, replacing post_id with your unique post ID:

get_post_meta( int $post_id, string $key = ‘’, bool $single = false )

5. Detect when the reader is in a mobile device

Most of the time, if your user is viewing your website from a mobile device, they will face a couple of challenges if the page loads like it would on a computer.To make sure the mobile version of your webpage is loading, a simple code snippet in your functions.php file should do the trick. Just add the following code to test if the current browser runs on a mobile device:

wp_is_mobile()

Changing the appearance of your site with these most commonly used WordPress functions does not mean you can’t use plugins on your website.It just means that you will get a lighter installation, one that loads faster because most of its functions are already embedded in the functions.php file, which no WordPress can run without.To make these changes, though, make sure to create a child theme from the original theme to place the changes in. Besides that, have a backup of your WordPress installation. That will increase hosting security and guarantee you won’t lose your website if you make a mistake while uploading these changes.

No comments:

Post a Comment

Post Top Ad