The post How to Add Background Music to WordPress (and Elementor) with Autoplay appeared first on PluginsForWP.
]]>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.
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:

<audio class="bg-music" controls src="YOUR_AUDIO_URL_HERE" autoplay loop></audio>YOUR_AUDIO_URL_HERE With the actual URL of your MP3 file from the media library. 
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:

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.
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:
bg-music..bg-music { display: none; }
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:

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.
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.
]]>The post How To Redirect WooCommerce and EDD Empty Checkout Pages appeared first on PluginsForWP.
]]>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.
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/'.
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/'.
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.
]]>The post How to Get Product Info (Price, ID, Name, Etc) from WooCommerce $product Object appeared first on PluginsForWP.
]]>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.
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();
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.
$product = wc_get_product( $product_id );
$product->get_type();
$product->get_name();
// etc.
$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.
}
$cart = WC()->cart->get_cart();
foreach( $cart as $cart_item_key => $cart_item ){
$product = $cart_item['data'];
$product->get_type();
$product->get_name();
// etc.
}
$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.
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.
]]>The post How to Easily Check If a User Is Logged In to WordPress appeared first on PluginsForWP.
]]>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.
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:
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.
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.
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>';
}
}

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.
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>';
}
?>

Once saved, visit your website and ensure that the correct message is displayed based on the user status.
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.
]]>The post How to Get Current User Role and Name in WordPress appeared first on PluginsForWP.
]]>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.
There are many reasons why to get the current user data in WordPress:
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.
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();
}
}

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

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.
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.
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.
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.
]]>The post How to Change WordPress Login URL in Three Different Ways appeared first on PluginsForWP.
]]>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.
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.
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.
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.

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 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:


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.

If something goes wrong, you can replace the backup file in the root directory, and the site will return to the old format.
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.
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.


Once you have finished changing the login page, please optimize your website by redirecting the logout link.
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.
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.

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

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

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

Alternatively, you can also modify the error message of the default URL page to a custom text or even redirect it to the homepage.
The iThemes Security plugin is one of the most popular security plugins for WordPress, with over one million active installations.

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

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

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.
The Defender plugin by the WPMUDEV team is another great option to mask the default login link.

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

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

You can get the pro version of the Defender from the official website or us for only $4.99.
The SecuPress plugin for WordPress is another powerful plugin to move the login and admin page.

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.

You can get SecuPress Pro from the official website or us for only $4.99.
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.
]]>The post How to Change the ‘Read More’ Button Text in WordPress appeared first on PluginsForWP.
]]>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.
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 default button’s text is a straightforward task that can be done in a couple of different ways:
I recommend you follow the first method before trying the PHP function.
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.

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.

At the bottom of the screen, you’ll have the option to change the default text to your custom desired 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.
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.
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.
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'); 
Make sure to change the Read the full article... to your desired custom text, and click update when finished.
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.

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.
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.
]]>The post How to Add ‘Continue Shopping’ Button to WooCommerce Cart Page appeared first on PluginsForWP.
]]>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.
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.

The following section will show you how to add the continue shopping / return to shop button using a PHP function.
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.
functions.php fileThe 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.

Once opened, scroll to the bottom of the file.
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
});

Once pasted, save the changes and move on to the next step.
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.

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

Click on the button and verify that you’ve been redirected to the shop page.
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.
]]>The post How to Set a Default Variation in WooCommerce appeared first on PluginsForWP.
]]>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.
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.

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.

To fix that, we will need to follow best practices and mimic the site above.
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);
}
}
}

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.

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.

Make sure to visit more products pages on your website to verify that it’s all working as expected.
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.
]]>The post How to Get URL of Current Page in WordPress appeared first on PluginsForWP.
]]>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.
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.
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.
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( '/' );
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.
]]>