Snippets Archives - PluginsForWP https://plugins-for-wp.world/blog/tag/snippets/ Premium WordPress Plugins And Themes For An Affordable Price Tue, 24 Jun 2025 20:12:13 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.3 https://plugins-for-wp.world/wp-content/uploads/2020/01/cropped-Pluginsforwp-Favicon-32x32.jpg Snippets Archives - PluginsForWP https://plugins-for-wp.world/blog/tag/snippets/ 32 32 How to Add Background Music to WordPress (and Elementor) with Autoplay https://plugins-for-wp.world/blog/add-autoplay-background-music-wordpress-elementor/ https://plugins-for-wp.world/blog/add-autoplay-background-music-wordpress-elementor/#respond Tue, 24 Jun 2025 20:12:05 +0000 https://plugins-for-wp.world/?p=550534 Adding background music to your WordPress website can significantly improve visitor behavior by creating a homely atmosphere or setting the right mood. This guide will teach you how to place music on a specific page or throughout your entire site, using easy-to-follow instructions and either the Gutenberg editor or Elementor, without requiring the use of […]

The post How to Add Background Music to WordPress (and Elementor) with Autoplay appeared first on PluginsForWP.

]]>
Adding background music to your WordPress website can significantly improve visitor behavior by creating a homely atmosphere or setting the right mood.

This guide will teach you how to place music on a specific page or throughout your entire site, using easy-to-follow instructions and either the Gutenberg editor or Elementor, without requiring the use of complex plugins.

This tutorial shows how to embed background music with autoplay functionality and control over the audio player visibility.

Adding Background Music to a Single WordPress Page

First, upload your desired audio file to your WordPress media library (or any other cloud storage app, such as Dropbox). 

Once uploaded, follow the steps below to insert background music to a specific page:

  1. Open the WordPress page you want to add music to and click Edit.
  2. Add a new HTML block using the Gutenberg editor. If you use Elementor, you can add an HTML widget instead. Add background music with HTML block
  3. Copy and paste the following HTML audio tag into the block:  <audio class="bg-music" controls src="YOUR_AUDIO_URL_HERE" autoplay loop></audio>
  4. Replace YOUR_AUDIO_URL_HERE With the actual URL of your MP3 file from the media library. Audio HTML Block
  5. Save or update the page and visit it to test the music. You should hear the audio playing automatically in the background, and the audio player controls will appear.

Hiding the Audio Player Controls

If you prefer the music to play silently in the background without showing the audio controls, you can easily hide them. Remove the controls attribute from the audio tag like this:

Remove Controls attribute to hide audio player

WordPress has a built-in CSS rule that hides audio players without controls, so once you remove the controls keyword and save, the music will continue playing, but the player itself won’t be visible.

Alternative Way to Hide the Audio Player Using CSS

If you want to keep the controls attribute for any reason but still hide the player from view, you can do so by adding custom CSS:

  1. Make sure your audio tag has a unique class, for example, bg-music.
  2. Go to Appearance > Customize > Additional CSS (or under the Advanced tab in Elementor) in your WordPress dashboard.
  3. Add the following CSS code to hide the audio player: .bg-music { display: none; }
  4. Publish the changes. The audio player will disappear from the page, but the music will still play in the background.
Hide audio player with css

Adding Background Music to Every Page on Your WordPress Site

Music added directly to a single page will only play on that page. However, if you want background music to play site-wide, you need to add the audio code to the website footer.

The background music will be played on every page where the footer exists.

First, delete the audio player from the single page where you added it (in the previous step), so the music won’t be played twice. Then:

  1. Add the audio code to your footer. You can do this in a couple of ways:
    • Using Elementor’s Code feature: Add a new custom code snippet and execute it after the page is loaded.
    • Add it directly to the footer template file.
    • Using a plugin like Code Snippets: Create a new snippet, paste your audio code under the HTML tab, and set it to load in the footer.
  2. Remember to replace the source URL with the URL of your MP3 file.
  3. Save and activate the snippet or custom code. Add background music to footer
  4. Visit any page on your website—you should now hear the background music playing everywhere, and the audio player will be visible in the footer.

Hiding the Audio Player Site-Wide

Just like with a single page, if you want to hide the audio player from all pages, remove the controls attribute from the audio tag in your footer code snippet or custom code. Save, and the music will continue to play silently across the entire site without showing the player.

Conclusion

Adding background music to WordPress is a quick and effective way to set the right tone for your website. If you want to hear music on a specific page or all pages, the autoplay and loop options of embedding an HTML audio element will enable you to do that on your own.

You can easily manage the visibility of the audio player either by removing the controls attribute or by applying custom CSS.

Plus, using Elementor’s code feature or a snippet plugin makes site-wide implementation straightforward.

Test this on your WordPress website and make your visitors feel more immersed!

The post How to Add Background Music to WordPress (and Elementor) with Autoplay appeared first on PluginsForWP.

]]>
https://plugins-for-wp.world/blog/add-autoplay-background-music-wordpress-elementor/feed/ 0
How To Redirect WooCommerce and EDD Empty Checkout Pages https://plugins-for-wp.world/blog/how-to-redirect-woocommerce-and-edd-empty-checkout-pages/ https://plugins-for-wp.world/blog/how-to-redirect-woocommerce-and-edd-empty-checkout-pages/#respond Fri, 19 Jul 2024 17:16:36 +0000 https://plugins-for-wp.world/?p=7665 The empty checkout page is no fun. Not for the store owner or the website visitor. Besides being unuser-friendly, a cart page without items will ultimately cost you revenue. When the user removes items from the cart, there is a better chance for him to navigate away from your website afterward. To prevent it from […]

The post How To Redirect WooCommerce and EDD Empty Checkout Pages appeared first on PluginsForWP.

]]>
The empty checkout page is no fun. Not for the store owner or the website visitor.

Besides being unuser-friendly, a cart page without items will ultimately cost you revenue.

When the user removes items from the cart, there is a better chance for him to navigate away from your website afterward. To prevent it from happening, it will be better to direct them to some of the products that may interest them.

Please take a look at our website, for example. When you try to load our checkout page plugins-for-wp.world/checkout when it’s empty, you’ll be redirected to our subscription page.

By doing so, I focused your attention on our available plans rather than giving you the option of where to go next, saving you valuable page load time.

This tutorial will redirect WooCommerce and Easy Digital Downloads empty cart pages to different pages.

Both codes below must be pasted in the functions.php file of your child theme or a site-specific plugin. Read our article and learn how to create a custom site-specific plugin in WordPress.

Please apply the functions below to a child theme and remember to back up WordPress before editing core files.

Redirect an Empty Checkout Page in WooCommerce

If you’re using the WooCommerce plugin to sell your goods, use the code below:

add_action("template_redirect", 'redirect_empty_checkout_woocommerce');
function redirect_empty_checkout_woocommerce(){
    global $woocommerce;
    if( is_cart() && WC()->cart->cart_contents_count == 0){
        wp_safe_redirect( site_url( 'shop' ) );
    }
}

The code above will redirect the visitors from an empty checkout or cart page to the archive Shop page.

However, if you prefer to land them on a different page, such as a specific product or the pricing page, change the URL address from 'shop' to 'product/product-name/'.

Redirect an Empty Checkout Page in Easy Digital Downloads

If you’re using the EDD plugin to sell your goods, use the code below

add_action( 'template_redirect', 'redirect_empty_checkout_edd', 11 );
function redirect_empty_checkout_edd(){
	if ( is_page( edd_get_option( 'purchase_page' ) ) && edd_get_cart_quantity() == 0 ) {
        wp_safe_redirect( site_url( 'downloads' ) );
		exit;
	}
}

Like the WooCommerce redirection function, the code above will redirect the users to the archive Downloads page, where they can view all your available products.

If you want to set a different URL, change 'downloads' to your desired address, such as 'downloads/product-name/'.

Related Articles

Conclusion

This article teaches you how to redirect empty checkout pages in WooCommerce and Easy Digital Downloads to another page.

Let us know which code snippet you used to achieve that.

The post How To Redirect WooCommerce and EDD Empty Checkout Pages appeared first on PluginsForWP.

]]>
https://plugins-for-wp.world/blog/how-to-redirect-woocommerce-and-edd-empty-checkout-pages/feed/ 0
How to Get Product Info (Price, ID, Name, Etc) from WooCommerce $product Object https://plugins-for-wp.world/blog/woocommerce-product-object-tutorial/ https://plugins-for-wp.world/blog/woocommerce-product-object-tutorial/#respond Wed, 21 Dec 2022 15:54:58 +0000 https://plugins-for-wp.world/?p=431936 The WooCommerce $product object holds every piece of data that we need to work with products. If you’re a WooCommerce developer, you’ll need to access some or all of that information at some point or another. Whether you’re developing a plugin, theme, or just a code snippet, knowing how to get the relevant product information […]

The post How to Get Product Info (Price, ID, Name, Etc) from WooCommerce $product Object appeared first on PluginsForWP.

]]>
The WooCommerce $product object holds every piece of data that we need to work with products.

If you’re a WooCommerce developer, you’ll need to access some or all of that information at some point or another.

Whether you’re developing a plugin, theme, or just a code snippet, knowing how to get the relevant product information is the key to success.

Once you access the $product variable, you can use any class object method.

Some of the product data you can get from the $product object is the item name, order, ID, SKU, price, stock level, order notes, etc. We can retrieve complete information about the item simply by getting the $product object.

For example, when a customer asks us to create a code snippet to disable a payment method for a specific product, we need to check if the item exists on the cart page and conditionally execute the function based on true or false.

This was just one scenario out of many possibilities, and you can find many more in some of the most useful WooCommerce functions.

Now that we know how essential the product object is let’s discover how to get the data from it.

Get Data from the $product Variable

Some additional arguments are passed through the function when working with WooCommerce hooks (actions and filters).

If you can use the $product object as an argument in the function, you’re golden. However, if the process doesn’t accept it as an argument, you’ll need to define the ‘global $product’ inside it to access all the object’s methods.

Once you’re ‘in,’ here is the product’s data you can get.

Get Product ID:

$product->get_id();

Get Product General Info:

$product->get_type();
$product->get_name();
$product->get_slug();
$product->get_date_created();
$product->get_date_modified();
$product->get_status();
$product->get_featured();
$product->get_catalog_visibility();
$product->get_description();
$product->get_short_description();
$product->get_sku();
$product->get_menu_order();
$product->get_virtual();
get_permalink( $product->get_id() );

Get Product Prices:

$product->get_price();
$product->get_regular_price();
$product->get_sale_price();
$product->get_date_on_sale_from();
$product->get_date_on_sale_to();
$product->get_total_sales();

Get Tax, Shipping, and Stock:

$product->get_tax_status();
$product->get_tax_class();
$product->get_manage_stock();
$product->get_stock_quantity();
$product->get_stock_status();
$product->get_backorders();
$product->get_sold_individually();
$product->get_purchase_note();
$product->get_shipping_class_id();

Get Product Dimensions:

$product->get_weight();
$product->get_length();
$product->get_width();
$product->get_height();
$product->get_dimensions();

Get Similar / Linked Products:

$product->get_upsell_ids();
$product->get_cross_sell_ids();
$product->get_parent_id();

Get Variations and Attributes:

$product->get_children(); // get variations
$product->get_attributes();
$product->get_default_attributes();
$product->get_attribute( 'attributeid' );

Get Product Taxonomies:

$product->get_categories();
$product->get_category_ids();
$product->get_tag_ids();

Get Product Downloads:

$product->get_downloads();
$product->get_download_expiry();
$product->get_downloadable();
$product->get_download_limit();

Get Product Images:

$product->get_image_id();
$product->get_image();
$product->get_gallery_image_ids();

Get Product Reviews:

$product->get_reviews_allowed();
$product->get_rating_counts();
$product->get_average_rating();
$product->get_review_count();

No $product Variable? No Worries!

If the hooks don’t accept the $product variable as is, you can still get to the object by adding another step.

The following sections will show how to retrieve the $product object from different variables and objects.

Access the $product Object from the $product_id

$product = wc_get_product( $product_id );
    
$product->get_type();
$product->get_name();
// etc.

Access the $product Object from the $order_id

$order = wc_get_order( $order_id );
$items = $order->get_items();
  
foreach ( $items as $item ) {
  
    $product = $item->get_product();
    
    $product->get_type();
    $product->get_name();
    // etc.
  
}

Access the $product Object from the Cart Object

$cart = WC()->cart->get_cart();
  
foreach( $cart as $cart_item_key => $cart_item ){
  
    $product = $cart_item['data'];
    
    $product->get_type();
    $product->get_name();
    // etc.
  
}

Access the $product Object from the $post Object

$product = wc_get_product( $post );
  
$product->get_type();
$product->get_name();
// etc.

Once you get into the $product object, you can use any of its methods from the first section.

Related Articles

  1. How to set a default variation in WooCommerce
  2. How to reorder products in WooCommerce
  3. How to hide the WooCommerce additional information tab
  4. WooCommerce: add back in stock notifications
  5. Best name your price plugins for WooCommerce
  6. How to change store currency in WooCommerce
  7. Add additional variation images in WooCommerce
  8. How to change the sale badge in WooCommerce

Conclusion

This article showed you how to access the WooCommerce $product object and use its data to expand the functionality of the WooCommerce plugin.

You can use it to develop a new plugin, theme, extension, or simple function.

Leave a comment and tell us what you use the $product object for or if you need further help.

The post How to Get Product Info (Price, ID, Name, Etc) from WooCommerce $product Object appeared first on PluginsForWP.

]]>
https://plugins-for-wp.world/blog/woocommerce-product-object-tutorial/feed/ 0
How to Easily Check If a User Is Logged In to WordPress https://plugins-for-wp.world/blog/check-if-user-logged-in-wordpress/ https://plugins-for-wp.world/blog/check-if-user-logged-in-wordpress/#respond Fri, 04 Nov 2022 17:05:49 +0000 https://plugins-for-wp.world/?p=429875 If you are a full-stack WordPress developer or just a website owner using code snippets, checking whether a user is logged in is an essential function you will need to use sooner or later. Checking the condition of the user and triggering relevant actions is a common practice and is used by many functions and […]

The post How to Easily Check If a User Is Logged In to WordPress appeared first on PluginsForWP.

]]>
If you are a full-stack WordPress developer or just a website owner using code snippets, checking whether a user is logged in is an essential function you will need to use sooner or later.

Checking the condition of the user and triggering relevant actions is a common practice and is used by many functions and plugins.

This article will show you how to check if a user is logged in to WordPress and how to use it in the functions.php and template files.

Why Check if a User is Logged In to WordPress

As mentioned above, validating if a user is logged in is used in many scenarios. Moreover, it is one of the most used WordPress functions, and for a good reason.

There are thousands of reasons why you’ll need to check the user’s status, and here are only some of them:

  • Conditionally display login/logout links in your website’s header.
  • Use the logged-in users’ names to greet and welcome them.
  • Show WooCommerce prices for logged-in users only.
  • Display certain pages, like my account page, for logged-in users.

You probably got the point and understand by now why it is beneficial to check if a WordPress user is logged in.

Whether you want to use it for one of the reasons above or for a different one, we will show you how to do it.

How to Check if a User is Logged In

WordPress has a built-in function that we can use to determine if a user is logged in or not.

is_user_logged_in()

You can also use PHP logical operator to check if a user is NOT logged in (logged out) by adding an exclamation point (!) to the left of the function, like so:

!is_user_logged_in()

It is a simple function because it doesn’t take any variables and returns true if a user is logged in and false if not logged in.

We can then execute different kinds of actions based on the returned value.

For example, let’s greet all logged-in users with a Hello fan message and just Hello to all logged out users. To do that, we can use the is_user_logged_in() function in our functions.php or template files.

Use the is_user_logged_in() Function in the functions.php File

WordPress uses hooks (like anchors) to simplify adding content to certain page parts.

Therefore, we will need to add the message to the desired hook based on where we want it to appear.

If we want to display it above the header, we can use the init hook.

Using an FTP or from your WordPress dashboard, navigate to Appearance -> Theme File Editor and open the functions.php file of your child’s theme.

Note: please back up your website before editing core files.

Then, scroll to the bottom of the file and paste this function:

add_action('init', 'greet_logged_in_users');
function greet_logged_in_users() {
	if ( is_user_logged_in() ) {
		echo '<p>Hello fan!</p>';
	} else {
		echo '<p>Hello</p>';
  }
}
Check if WordPress user logged in PHP function

Once saved, visit your website and ensure you can see the message for the logged-in users. Then, log out and verify that the alternative message is displayed.

If you prefer to check the user status within a template file, continue to the next section.

Use the is_user_logged_in() Function in a PHP Template File

The second way to use the is_user_logged_in() function to check if the user is logged-in is by executing it in a template file instead of the functions.php file.

The advantage of using a template file is the option to position the function anywhere on the page, regardless if there is a hook to which we can attach it.

Let’s take the example above and display a greeting message to logged in users using one of our theme’s template files.

Because we want to display the message above the header, we will need to modify the header.php file.

Therefore, navigate to Appearance -> Theme File Editor and click on the header.php file from the right sidebar.

Then, place the is_user_logged_in() function in your desired position, and save the file.

<?php
  if ( is_user_logged_in() ) {
    echo '<p>Hello fan!</p>';
  } else {
    echo '<p>Hello</p>';
  }
?>
Check if WordPress user logged in template file

Once saved, visit your website and ensure that the correct message is displayed based on the user status.

Conclusion

The is_user_logged_in() is a default WordPress function to check if a user is logged in.

This article taught you to check if a user is logged in using the is_user_logged_in() function in different scenarios and file types.

Leave us a comment and tell us your preferred method to achieve this task.

The post How to Easily Check If a User Is Logged In to WordPress appeared first on PluginsForWP.

]]>
https://plugins-for-wp.world/blog/check-if-user-logged-in-wordpress/feed/ 0
How to Get Current User Role and Name in WordPress https://plugins-for-wp.world/blog/get-current-user-role-wordpress/ https://plugins-for-wp.world/blog/get-current-user-role-wordpress/#respond Mon, 28 Feb 2022 18:13:22 +0000 https://plugins-for-wp.world/?p=407826 When adding a custom code or creating a plugin in WordPress, you may need to use the information of the specific current user. Getting the current user values, such as role or ID, will enable you to segment and display unique content to different users. In this article, you’ll learn how to get the current […]

The post How to Get Current User Role and Name in WordPress appeared first on PluginsForWP.

]]>
When adding a custom code or creating a plugin in WordPress, you may need to use the information of the specific current user.

Getting the current user values, such as role or ID, will enable you to segment and display unique content to different users.

In this article, you’ll learn how to get the current user role and ID in WordPress.

Why Get Current User Data in WordPress

There are many reasons why to get the current user data in WordPress:

  • Display different content to each user based on his role. For example, an active customer can view content others can’t.
  • Add a login and logout link to the header based on the user’s status.
  • Special discounts for logged-in users.
  • Remove the admin bar for all non-admin users.
  • Congrats to logged-in users by their first names on their account page.

These were just a few examples of the unlimited possibilities you can achieve when working with the WordPress user’s object and getting data such as role, ID, and name.

In one of our blog posts, we used a function to get the current user role to create a registration form with Elementor.

You can see many more examples when getting the current user object comes in handy in some of WordPress’s most common code snippets.

Some of the most often uses of the get current user object in WordPress will require us to retrieve the user role, ID, and name, and in the following few sections, we will learn exactly how to do it.

Get Current User Role in WordPress

Getting the current user role in WordPress is a two steps process. We will first need to check if the visitor is logged in and, if so, what’s its role.

To check if the visitor is logged in and an actual user, use this function:

is_user_logged_in()

Once we verify that the visitor is logged in, we can use the wp_get_current_user() function and get the user’s object along with all the data that comes with it.

WordPress user’s object includes relevant and valuable information regarding the logged-in user. The -> is used in object scope to access methods and properties of an object. Knowing that, wp_get_current_user()->roles will get us the current user role.

The complete code will look like that:

 if( is_user_logged_in() ) {
 $user = wp_get_current_user();
 $roles = ( array ) $user->roles;
 return $roles; // This returns an array
 // Use this to return a single value
 } else {
 return array();
 }

You can test the function by passing it inside the functions.php file of your child theme, hooking it to the header, and print the result:

add_action( 'wp_head', 'pfwp_get_current_user_role'); 

function pfwp_get_current_user_role() {
	
    if( is_user_logged_in() ) {
        $user = wp_get_current_user();
        $roles = ( array ) $user->roles;
        print_r($roles); // This will print the array of the current user roles
    } else {
    return array();
    }

}
Get current user role with a function

Once saved, visit your website and check the returned role values of the current user.

Array current user role

You can choose a specific role from that array if the user has multiple roles, such as admin and subscriber. Replace print_r($roles) with the desired value of the array, for example print_r($roles[0]).

Once you modify the code, please remember to save the changes and verify that it’s working as expected.

Get Current User ID in WordPress

Getting the current user ID in WordPress is a simple process because there is no need to invoke a new WP_User_object as we did above when getting the user role.

The code below is very similar but with less overhead. It will retrieve the current user’s ID, or 0 if no user is logged in.

get_current_user_id()

To display the user ID or congrats to a visitor with no ID, use a function like so:

if ( is_user_logged_in() ) {
echo 'User ID: ' . get_current_user_id();
} else {
echo 'Hello visitor!';
}

You can trigger it to the website header or any other part of your website using WordPress hooks.

Get Current User Name in WordPress

As explained above, the wp_get_current_user() function returns the WP_User object.

The WP user object is full of data regarding the current user.

Some attributes we can scope from that object are username, name, display name, etc.

Here are some examples of how to get that data:

Get current user first name:

$current_user->user_firstname

You can use it in a function to display the username first name like so:

if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
echo 'Hello: ' . $current_user->user_firstname;
} else {
echo 'Hello visitor!';
}

Get current user last name:

$current_user->user_lastname

Get current user display name:

$current_user->display_name

Those were only examples of explicit content you could use with the user object.

Regardless of the function, I recommend using a backup plugin before modifying core files if something goes wrong.

Conclusion

In this article, you learned how to get WordPress’s current user data, such as role and ID.

Leave us a comment and tell us the functions and data you used or if you have any further questions regarding the process.

The post How to Get Current User Role and Name in WordPress appeared first on PluginsForWP.

]]>
https://plugins-for-wp.world/blog/get-current-user-role-wordpress/feed/ 0
How to Change WordPress Login URL in Three Different Ways https://plugins-for-wp.world/blog/how-to-change-wordpress-login-url/ https://plugins-for-wp.world/blog/how-to-change-wordpress-login-url/#respond Mon, 31 Jan 2022 15:58:38 +0000 https://plugins-for-wp.world/?p=400635 All WordPress websites have one common aspect if you don’t know this already. They have the same login URL. So, your login URL can be yourwebsite.com/wp-admin or yourwebsite.com/wp-login. If you think that makes things simpler for you, think again. The fact is, this makes your website vulnerable to attacks from hackers and cybercriminals. And considering […]

The post How to Change WordPress Login URL in Three Different Ways appeared first on PluginsForWP.

]]>
All WordPress websites have one common aspect if you don’t know this already. They have the same login URL.

So, your login URL can be yourwebsite.com/wp-admin or yourwebsite.com/wp-login.

If you think that makes things simpler for you, think again.

The fact is, this makes your website vulnerable to attacks from hackers and cybercriminals. And considering WordPress is the most popular content management system (CMS) used, the risks are higher.

So, changing the login URL is an essential step that you need to take to keep your site secure.

This article will show you how to change your login URL in WordPress in every possible way.

What Makes WordPress Login Important?

Once you install WordPress, you will have access to the admin dashboard of your website. This is where you can make all the necessary changes to your site.

The login page URL or the admin URL is the web address that allows you access to this management zone of your website. That makes it play an essential role in the overall management of the site.

Since this URL is standardized, anyone can visit the admin panel and access the site. Moreover, WordPress creates an “admin” username by default. It can make the hacker’s task even more accessible.

Now all that is needed is to guess the password you have set. As the hacker tries to guess the correct password, your site’s resources are wasted.

One recent study has shown that 75% of Americans set weak passwords. That means your site may not have the necessary defenses against intruders.

By changing the URL, you can reduce the risks and prevent your site from being overwhelmed by unwanted bot traffic.

Changing the WordPress URL will not offer you complete protection against hackers. But it will surely make your site less vulnerable to attacks.

Steps to Change your WordPress Login URL

The first step is to find your WordPress login URL, and it’s simple. You need to add /admin/ or /login/ at the end of your WordPress URL.

For example, you can use www.yourwebsite.com/login/ or www.youwebsite.com/admin/

Generally, these two URLs should take you directly to your login page. If that doesn’t happen, add /wp-login.php at the end of the primary URL (www.yourwebsite.com/wp-login.php).

You can change the WordPress URL manually or use a reliable security plugin. If you do it manually, you will need to change your site’s core files. If done incorrectly, this can affect the overall site functionality.

Also, every time you update WordPress, it will recreate the default login URL. So, you will need to redo the process from scratch.

Using a security plugin is a more straightforward and hassle-free method.

You can also change your URL by updating the functions.php file and inserting specific codes. However, using the wrong codes can result in errors and disable your access to the WordPress dashboard.

If you are not a coder or uncomfortable about using codes that you do not understand, the last method is the simplest one you can choose from.

Changing the WordPress Login URL with a Function

In this first method, we will change the default WordPress login URL with a PHP that we will paste into the functions.php file of our child theme.

If you don’t have one already, please read our how-to create a child theme article before moving forward.

Navigate to Appearance -> Theme File Editor and open the functions.php file of your child theme.

Scroll to the bottom of the file and paste the function below:

// Redirect default URL link to a custom page
add_action('init','custom_login');
function custom_login(){
 global $pagenow;
 if( 'wp-login.php' == $pagenow  && $_GET['action']!="logout" && $_GET['action']!= "lostpassword" ) {
  wp_redirect('link-goes-here');
  exit();
 }
}

Replace the ‘link-goes-here‘ with the actual link of your new login page. You can create a new login or registration form with Elementor or other plugins.

Change WordPress Login URL Function

Once saved the changes, navigate to the /wp-admin link using an incognito window and verify that you’re being redirected to the new custom URL.

Changing the WordPress Login URL Manually

Changing the URL manually is not a difficult task. We have listed down the steps for doing it.

You will need to use a Code Editor like Notepad++ or Sublime Text. Regardless of which code editor you’re using, the steps are the same:

  • Before you make any changes to the wp-login.php file, backup your website. In case of any errors, you can use the backup for restoration. This file is located in the public_html directory.
  • Select a new login URL that you will use. Let’s assume that you have chosen /mynewlogin.php.
  • Rename the wp-login.php file with the new login name you have chosen. That means you will rename it to /mynewlogin.php.
    Rename wp-login php file
  • Now open the mynewlogin.php file. Find all the instances of the term ”wp-login.php” used and replace them with “mynewlogin.php.” Select the replace all option for a quick change.
    Rename instances of wp login file

After saving the changes, you will log into WordPress using the new URL. In this case, it will be localhost/wp/mynewlogin.php. Anyone trying to log in using the old URLs will find a 404-not-found page.

New login URL

If something goes wrong, you can replace the backup file in the root directory, and the site will return to the old format.

Changing the WordPress-admin URL With a Plugin

If you don’t want to make manual changes to the core WordPress files, using a WordPress plugin is a simple way to change the login URL. The good news is these plugins are free.

Multiple reliable plugins are lightweight and easy to use. Also, use an updated plugin that offers the best security.

How does the WordPress plugin work?

The plugin introduces some additional lines in the WordPress .htaccess file. However, it doesn’t modify or relocate the wp-login.php or the wp-config.php file.

Here are the steps you need to take.

  • To start with, install the plugin and activate it. We will also recommend keeping a backup of your site before installing it.
  • Go to the WordPress admin dashboard and access Settings > Easy Hide Login.
    Easy Hide Login WordPress Plugin
  • Once you access it, you will be able to put a Slug text in the window. For example, you can put mynewlogin as the Slug Text. Then hit the submit button. This will be your new login page. Anyone visiting the original URL will get a 404-error message.
    New login page slug text
  • If you want to reverse the process, delete the Slug Text and press the submit or deactivate WPS Hide Login button. The URL will return to its original format.

Once you have finished changing the login page, please optimize your website by redirecting the logout link.

Change Login Page URL with Security Plugins

Another great option to change the default WordPress page is using different security plugins.

Installing a security plugin is a simple way to change the default WordPress login URL, and it will better protect your website from harm.

This section will learn how to change the default URL with some of the best security plugins for WordPress.

Perfmatters

The performance or optimization plugins will allow you to enable or disable WordPress features based on your preferences. Because WordPress is a multi-purpose platform that you can use for various options, it’s a bright idea to only enable the necessary features for you. That way, you can save valuable resources.

Many great optimization plugins increase site speed, improve SEO, etc. In this example, we will focus on Perfmatters.

You can get the Perfmatters plugin from the official website or us for only $4.99.

Once you activate the plugin on your WordPress website, navigate to Settings -> Perfmatters screen and scroll to the Login URL section.

Perfmatters plugin settings page

Please enter your desired slug into the custom login URL field to replace it with the default one.

Create custom login URL with Perfmatters plugin

Once saved, navigate to the default login URL address /wp-admin and verify that an error message is presented instead of the login form.

Disable default login page

Then, navigate to the new login URL you created and test the login form on that page.

New custom login URL

Alternatively, you can also modify the error message of the default URL page to a custom text or even redirect it to the homepage.

iThemes Security

The iThemes Security plugin is one of the most popular security plugins for WordPress, with over one million active installations.

iThemes Security plugin

Once you activate it on your website, navigate to Security -> Settings -> Advanced -> Hide Backend, and enable the hide backend option.

Hide backend iThemes security

Then, enter the new URL login slug that you would like to replace instead of the default one and save the changes.

Change default URL with the iThemes Security plugin

Once saved, the new URL will take effect, and the default URL will no longer work.

You can get the Pro version from the official website or us for only $4.99.

Defender

The Defender plugin by the WPMUDEV team is another great option to mask the default login link.

Defender security plugin WordPress

Once activated on your website, navigate to Defender -> Tools and click on the Activate button.

Mask login area with the Defender plugin

Then, enter your custom slug inside the masking URL slug field and save the changes.

Enter new URL slug with Defender plugin

You can get the pro version of the Defender from the official website or us for only $4.99.

SecuPress

The SecuPress plugin for WordPress is another powerful plugin to move the login and admin page.

SecuPress plugin WordPress

Once you activate SecuPress, navigate to SecuPress -> Modules -> Users and Login and enable the move the login and admin pages option.

Enter your custom new login page slug and save the changes to finish the process.

Enter new login page slug SecuPress plugin

You can get SecuPress Pro from the official website or us for only $4.99.

Conclusion

So, these are the three most straightforward ways of changing the login URL of your WordPress site. All these methods are simple and will add additional layers of security to your site.

Even though WordPress is a secure platform, you can’t neglect this simple step to secure your website.

Leave us a comment and know which method you chose to achieve this task.

The post How to Change WordPress Login URL in Three Different Ways appeared first on PluginsForWP.

]]>
https://plugins-for-wp.world/blog/how-to-change-wordpress-login-url/feed/ 0
How to Change the ‘Read More’ Button Text in WordPress https://plugins-for-wp.world/blog/change-read-more-text-in-wordpress/ https://plugins-for-wp.world/blog/change-read-more-text-in-wordpress/#respond Wed, 17 Nov 2021 00:00:23 +0000 https://plugins-for-wp.world/?p=393585 The read more button will appear below every post excerpt on the blog archive page. While many are satisfied with the default one, others would like to change it to a custom text. In this article, you’ll learn how to change the ‘Read More’ text in WordPress. What is the Read More Button The blog […]

The post How to Change the ‘Read More’ Button Text in WordPress appeared first on PluginsForWP.

]]>
The read more button will appear below every post excerpt on the blog archive page.

While many are satisfied with the default one, others would like to change it to a custom text.

In this article, you’ll learn how to change the ‘Read More’ text in WordPress.

What is the Read More Button

The blog archive page displays the most recent ten blog posts on your website.

Because each blog post has its page, showing the full article on the archive page is not practical.

Instead, the archive page will only display the post’s excerpt linked to the full blog post.

The read more button is positioned below every post excerpt, indicating that the article’s visible content is just a tiny part.

After clicking on the read more text, the user will be redirected to the full blog post to read the entire article.

While the default read more text is used by most, some would like to have the option to customize every part of their website, including that button.

In the next section, I’ll show you how to change the read more button to a custom text.

Changing the Read More Text

Changing the default button’s text is a straightforward task that can be done in a couple of different ways:

  • From the customization screen (theme dependent).
  • With a PHP function.

I recommend you follow the first method before trying the PHP function.

Method #1: Customization screen

This is my preferred way because it’s easy and does not require codes.

Many themes should have the option to change the read more text to your desired text easily.

Click on the customization link from the admin bar and look for it inside the different tabs.

Enter the customize screen

Because we can’t list all the various WordPress themes, we will use the popular Responsive theme as an example.

Once entered the customization screen, click on ‘Blog/Archive‘ twice and scroll down the sidebar.

Blog archive tab

At the bottom of the screen, you’ll have the option to change the default text to your custom desired text.

Change the read more text

Once changed, save the changes and revisit the archive page.

Most popular themes will have the option to change the read more text inside the customization tab.

If you can’t find the option to do so, you’ll need to use a PHP function.

Method #2: use a PHP function

This method should work for most WordPress themes.

As I said earlier, first check if your theme can change the read more text before using a function.

If you couldn’t find a way, your theme is using the default WordPress hook, and therefore you can change it with a function or custom plugin.

  1. Open the functions.php file of your child theme. If you don’t already have one, make sure to create a child theme before moving on.

    Tip: please backup your website before editing core files.

    Navigate to Appearance -> Theme editor and click on the functions.php file from the list on the right.

    Once clicked, scroll to the bottom of the file and move on to the next step.

  2. Now, copy the function below, and paste it at the bottom of the functions file.
    function new_excerpt_more($more) { global $post; return '<div class="moretag"><a href="'. get_permalink($post->ID) . '"> Read the full article...</a></div>'; } add_filter('excerpt_more', 'new_excerpt_more');
    WordPress function to change the read more text

    Make sure to change the Read the full article... to your desired custom text, and click update when finished.

  3. Once updated, the read more text button should change and reflect the new text you entered.

    Refresh the blog archive page and verify that the new text appears instead of the old one.

    Custom read more text

After changing the text, check out how to change the link color article and style the button with CSS to match the rest of your website.

If the function above didn’t work for you, search for a function coded explicitly for your theme. You can usually find it inside the documentation page of the theme you’re using.

Conclusion

In this article, you learned how to easily change the read more text in a few different ways.

Let us know which way you choose or need a specific function for the theme you’re using.

The post How to Change the ‘Read More’ Button Text in WordPress appeared first on PluginsForWP.

]]>
https://plugins-for-wp.world/blog/change-read-more-text-in-wordpress/feed/ 0
How to Add ‘Continue Shopping’ Button to WooCommerce Cart Page https://plugins-for-wp.world/blog/add-continue-shopping-button-to-woocommerce-cart/ https://plugins-for-wp.world/blog/add-continue-shopping-button-to-woocommerce-cart/#respond Thu, 04 Nov 2021 17:13:38 +0000 https://plugins-for-wp.world/?p=392850 The WooCommerce cart page will only display a return to shop button when the cart is empty. When products are in the cart, the button will disappear, and you won’t see it. In this article, you’ll learn how to add a continue shopping button to the WooCommerce cart page. Why Add the Continue Shopping Button […]

The post How to Add ‘Continue Shopping’ Button to WooCommerce Cart Page appeared first on PluginsForWP.

]]>
The WooCommerce cart page will only display a return to shop button when the cart is empty.

When products are in the cart, the button will disappear, and you won’t see it.

In this article, you’ll learn how to add a continue shopping button to the WooCommerce cart page.

Why Add the Continue Shopping Button

A continue shopping button will help you to increase your store revenue.

It’s a good practice to add a call to action button to the cart and encourage your customers to add more products.

In most cases, the cart and checkout pages will act like landing pages without a header or footer.

If a customer would like to navigate back to the store, he’ll initially need to enter the home URL in the address bar and navigate to the store from there.

This extra step will slow down the checkout process and eventually affect your business’ revenue.

We need to add it because the WooCommerce cart page does not have a ‘continue shopping’ button.

Default WooCommerce cart page

The following section will show you how to add the continue shopping / return to shop button using a PHP function.

Adding the Continue Shopping Button

Adding the return to shop button is easy to accomplish by entering a PHP function into our child theme.

If you don’t already have one, read our article on how to create a child theme.

Step #1: Open the functions.php file

The functions.php file can be found inside your theme’s folder.

First, navigate to Appearance -> Theme Editor and click on the functions.php file from the list on the right.

Edit theme functions php file

Once opened, scroll to the bottom of the file.

Step #2: Paste the function

Now, copy the function below and paste it at the bottom of the PHP file.

// Add continue shopping button to WooCommerce cart
add_action('woocommerce_cart_actions', function() {
  ?>
  <a class="button wc-backward" href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>"> <?php _e( 'Return to shop', 'woocommerce' ) ?> </a>
  <?php
});
Add continue shopping button function

Once pasted, save the changes and move on to the next step.

Step #3: Test the button

After you have saved the changes, let’s run a test and verify that we’ve successfully added the button.

First, add any product to the cart and then click on view cart.

Add products to cart

If the code is working correctly, you should see the return to shop button on the left just below the products list.

Return to shop button was added to cart

Click on the button and verify that you’ve been redirected to the shop page.

Conclusion

In this article, you learned to add a continue shopping button to the WooCommerce cart page.

Leave us a comment and let us know if you have any questions about the process.

The post How to Add ‘Continue Shopping’ Button to WooCommerce Cart Page appeared first on PluginsForWP.

]]>
https://plugins-for-wp.world/blog/add-continue-shopping-button-to-woocommerce-cart/feed/ 0
How to Set a Default Variation in WooCommerce https://plugins-for-wp.world/blog/how-to-set-a-default-variation-in-woocommerce/ https://plugins-for-wp.world/blog/how-to-set-a-default-variation-in-woocommerce/#respond Fri, 03 Sep 2021 18:50:29 +0000 https://plugins-for-wp.world/?p=385829 It is a well-known fact that the least amount of choices that a customer needs to make while visiting your website will lead to a quicker checkout. When the default WooCommerce variations are Choose an option, it adds an extra step to the checkout funnel, which will hurt your revenue in the long run. This […]

The post How to Set a Default Variation in WooCommerce appeared first on PluginsForWP.

]]>
It is a well-known fact that the least amount of choices that a customer needs to make while visiting your website will lead to a quicker checkout.

When the default WooCommerce variations are Choose an option, it adds an extra step to the checkout funnel, which will hurt your revenue in the long run.

This article will teach you how to set a default variation in WooCommerce to shorten the customers’ decision process.

Why Set a Default Value

Big companies already know that time equals money. Therefore, the goal is to convert your website’s visitors into customers in the most efficient way.

One of those ways is by setting a default value for the products’ variations. By doing so, the visitor will spend less time debating which option to choose because you chose it for him.

Take a look at this HappySocks website, for example. The size variation is already automatically selected when viewing one of their products, making the shopping process smoother.

Example of a website with default variation value

By default, WooCommerce products’ variation value is set to nothing. That will force the visitor to select a variation that will slow him down and even cause him to abandon your website.

Empty WooCommerce default variation

To fix that, we will need to follow best practices and mimic the site above.

Set a Default Variation in WooCommerce

There are a bunch of WooCommerce plugins that will help you achieve that, but in this article, we will use a PHP function.

The code below needs to be pasted into the functions.php file of your child theme or a site-specific plugin. Read our article and learn how to create a child theme if you don’t already have one.

// Set a defaualt variation value in WooCommerce
add_action('woocommerce_before_variations_form', 'p4wp_default_variation_value');
function p4wp_default_variation_value() {
      global $product;
      if (!count($default_attributes = get_post_meta($product->get_id(), '_default_attributes'))) {
        $new_defaults = array();
        $product_attributes = $product->get_attributes();
        if (count($product_attributes)) {
          foreach ($product_attributes as $key => $attributes) {
            $values = explode(',', $product->get_attribute($key));
            if (isset($values[0]) && !isset($default_attributes[$key])) {
              $new_defaults[$key] = sanitize_key($values[0]);
            }
          }
          update_post_meta($product->get_id(), '_default_attributes', $new_defaults);
        }
      }
} 
Paste code snipped in the functions file

This code is based on a WooCommerce hook instead of a specific theme. Therefore, it will work with any theme.

After saving the file, revisit any product page and verify that the variations are now populating with a value.

Set a default variation value in WooCommerce

Optional: Hide the choose an option field

If you would like to take it one step further and prevent your customers from accidentally not picking an option, you should hide the ‘Choose an option’ field.

Navigate to customize -> additional CSS and paste the code below.

.variations .value select option:first-child {
  display: none;
}

Once saved, you’ll no longer see the ‘choose an option’ field inside the variations drop-down menu.

Hide the choose an option field

Make sure to visit more products pages on your website to verify that it’s all working as expected.

Related Articles

Conclusion

In this article, you learned how to set a default variation value in WooCommerce instead of the ‘choose an option’ one.

Let us know how it worked for you or if you have any questions.

The post How to Set a Default Variation in WooCommerce appeared first on PluginsForWP.

]]>
https://plugins-for-wp.world/blog/how-to-set-a-default-variation-in-woocommerce/feed/ 0
How to Get URL of Current Page in WordPress https://plugins-for-wp.world/blog/how-to-get-url-of-current-page-in-wordpress/ https://plugins-for-wp.world/blog/how-to-get-url-of-current-page-in-wordpress/#respond Mon, 01 Feb 2021 22:41:47 +0000 https://plugins-for-wp.world/?p=272096 When using functions with WordPress, you’ll sometimes need to get the URL of the page you’re currently on. Whether it is for a custom PHP code, snippet, or script, knowing how to get the current page URL is handy. In this article, you’ll learn how to get the URL of the current page in WordPress. […]

The post How to Get URL of Current Page in WordPress appeared first on PluginsForWP.

]]>
When using functions with WordPress, you’ll sometimes need to get the URL of the page you’re currently on.

Whether it is for a custom PHP code, snippet, or script, knowing how to get the current page URL is handy.

In this article, you’ll learn how to get the URL of the current page in WordPress.

Why Getting Page’s URL

There are many reasons to get a page’s link, depending on what the function tries to achieve.

For example, you can display the page’s URL and create a copy to clipboard button.

Other popular functions will need the current URL to display related posts at the bottom of every blog post.

Knowing how to get the current page URL is necessary and will become handy at some point or another.

Whatever the reason, getting the current page URL is a simple task, and the next section will teach you exactly how.

Get the Current Page’s URL in WordPress

The first couple of snippets in this article will work with any WordPress template file. The second part will be template-specific.

The codes will get the URL for any page type like single post, single page, the home page, taxonomies templates, search templates, custom post types, etc.

Using a child theme when working with WordPress core files is highly recommended. Please create a child theme if you don’t already have one before moving forward.

Use the code snippets below inside the functions.php file or within a custom plugin.

Use the code below to get the current page URL:

global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) );

To get the last slug only of the current URL without the base part, use this:

global $wp;
$current_url = add_query_arg( array(), $wp->request );

For example, if your website is https://plugins-for-wp.world/post-name the code above will return just the /post-name.

You can also use the add_query_arg function to add multiple queries to the URL, which can be very useful for search and filter components.

Get current URL in specific templates

When working with WordPress templates, you can use the specific code snippets below to utilize the template and get the current URL of the page.

The snippets are very similar to each other. Make sure to replace the second line of the code based on the template you’re using.

$queried_id = get_queried_object_id();
//SECOND LINE GOES HERE

For single.php or page.php, replace the second line of the code with:

$current_url = get_permalink( $queried_id );

If you would like to get the taxonomy URLs like category or tag, replace the second line of the code with:

$current_url = get_term_link( $queried_id );

Last, for the archive page URL, replace the second line of the code with:

$current_url = get_author_posts_url( $queried_id );

To get the home page URL (it doesn’t matter which page you’re on), use the code snippet below (you don’t need the first line above):

$current_url = home_url( '/' );

Conclusion

In this article, you learn how to get the URL of the current page in WordPress.

Leave us a comment and tell us which one of the codes you used above.

The post How to Get URL of Current Page in WordPress appeared first on PluginsForWP.

]]>
https://plugins-for-wp.world/blog/how-to-get-url-of-current-page-in-wordpress/feed/ 0