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 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 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.
]]>The post How to Insert Ads and Content in Any Part Within Your WordPress Post appeared first on PluginsForWP.
]]>We will focus on three breakpoints, the beginning, middle, and the end of the post.
I will provide you with three separate codes, one code for each of the post parts, and also one big code at the end to use it all together at the same time.
Also, at the end of this article, I’ll provide you with the template and common code snippets to various locations to insert your content.
The codes below need to be pasted into the functions.php file of your child theme or a site-specific plugin.
Let’s start with the first location and insert the ads or other content at the beginning of the post.
To insert ads or any other content to the beginning of the post (before the first paragraph) use the code below:
function pfwp_insert_content_into_post($content){
if (is_singular('post')) {
$beforePostContent = '<p>Content at the beginning of the post</p>';
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $index == 0 ) {
$paragraphs[$index] = $beforePostContent . $paragraph;
}
}
$content = implode( '', $paragraphs );
}
return $content;
}
add_filter( "the_content", "pfwp_insert_content_into_post" );
To insert ads or any other content to the end of the post (after the last paragraph) use the code below:
function pfwp_insert_content_into_post($content){
if (is_singular('post')) {
$afterPostContent = '<p>Content at the end of the post</p>';
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( count($paragraphs) == $index + 1 ) {
$paragraphs[$index] .= $afterPostContent;
}
}
$content = implode( '', $paragraphs );
}
return $content;
}
add_filter( "the_content", "pfwp_insert_content_into_post" );
To insert ads or any other content exactly in the middle of the post use the code below:
function pfwp_insert_content_into_post($content){
if (is_singular('post')) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
// contet to add number 1
$insertContent_1 = '<p>Content in the middle of the post</p>';
$position_1 = floor(count($paragraphs) / 2);
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $position_1 == $index + 1 ) {
$paragraphs[$index] .= $insertContent_1;
}
}
$content = implode( '', $paragraphs );
}
return $content;
}
add_filter( "the_content", "pfwp_insert_content_into_post" );
To insert ads or any other content to the below positions, use the mid-post template (the template above) and just replace line 7 ($position_1 = floor(count($paragraphs) / 2)) with the relevant position from the list below.
$position_1 = floor(count($paragraphs) / 3);
$position_1 = floor(count($paragraphs) / 1.5);
$position_1 = 1;
$position_1 = 2;
$position_1 = floor(count($paragraphs) - 2);
$position_1 = floor(count($paragraphs) - 3);
Many of you would like to combine some of the positions above and insert ads or other content in multiple positions of the posts.
For example, ads at the beginning of the post and images in the middle of the post.
For that, I’ve created the template below and tried to keep it as simple and easy as possible for non-developers.
The template below will insert content to the beginning, middle, and end of the post. You can leave $beforePostContent or $afterPostContent empty (like so: $beforePostContent = '';) if you don’t want any content to be added there.
Feel free to play with the mid-post position based on your needs. You can use the code snippets I mentioned above. Also, you can add more positions to the code if you desire. For example, content to the first third and the second third of the post (you can also watch the video for clarification).
function pfwp_insert_content_into_post($content){
if (is_singular('post')) {
$beforePostContent = '<p>Content at the beginning of the post</p>';
$afterPostContent = '<p>Content at the end of the post</p>';
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
// Contet number 1
$insertContent_1 = '<p>Mid post content goes here</p>';
$position_1 = floor(count($paragraphs) / 2);
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $index == 0 ) {
$paragraphs[$index] = $beforePostContent . $paragraph;
}
// Add content number 1 to anywhere in the post
if ( $position_1 == $index + 1 ) {
$paragraphs[$index] .= $insertContent_1;
}
if ( count($paragraphs) == $index + 1 ) {
$paragraphs[$index] .= $afterPostContent;
}
}
$content = implode( '', $paragraphs );
}
return $content;
}
add_filter( "the_content", "pfwp_insert_content_into_post" );
Feel free to watch the video to better understand how it’s working. If you need another position that I did not mention in this post please leave a comment down below and I’ll add it.
In this article, you learned how to insert content in various places in your WordPress blog posts. Now, it’s a good time for you to copy some of the codes above and make good use of them.
Leave a comment below and let us know which code you’re using.
The post How to Insert Ads and Content in Any Part Within Your WordPress Post appeared first on PluginsForWP.
]]>