Author: adminword

  • Check if The Current User is The Post Author in WordPress

    How to Check if The Current User is The Post Author in WordPress?

    While working on a WooCommerce project recently, my client asked me if there is a way to show a message to seller on their own product items. A simple message like “you are the seller of this item” if the product page is previewed by the seller itself. Typically WooCommerce is a plugin which essentially creates a custom post type for listing products and each product item is like a single WordPress post. So typically I need to find a way to show message if current user and the post author. That makes this task as easy as eating a piece of cake, although finding the right file and exact spot in WooCommerce plugin to insert the message for seller, is bit of a challenge.

    In order to insert message for seller (post author) only, I had to create an if statement that checks if the current user is the author of the post or not. So first I had to get the user id of current user by WordPress function get_currentuserinfo() and then I need to match it with the id of current post author. If this condition returns true and both id matches, WordPress prints the message “you are the seller of this item!” for the seller.

    So here is the code to achieve this. You can simply use this code in any theme PHP file and to leave personalized messages for post authors.

    https://web.archive.org/web/20220701045135if_/https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-7443478584383873&output=html&h=280&slotname=3463695748&adk=1514384836&adf=1185081263&pi=t.ma~as.3463695748&w=336&lmt=1775835579&psa=0&format=336×280&url=https%3A%2F%2Fwpcodesnippet.com%2Fcheck-if-current-user-is-post-author%2F&wgl=1&dt=1656651098137&bpp=1&bdt=3155&idt=1323&shv=r20220613&mjsv=m202206090101&ptt=9&saldr=aa&abxe=1&prev_fmts=336×280&correlator=6874851047926&frm=20&pv=1&ga_vid=1427030432.1656651100&ga_sid=1656651100&ga_hid=1300761260&ga_fc=0&u_tz=0&u_his=8&u_h=926&u_w=428&u_ah=926&u_aw=428&u_cd=24&u_sd=1&adx=30&ady=1689&biw=428&bih=781&scr_x=0&scr_y=0&eid=44759875%2C44759926%2C44759837%2C31067934%2C31065742%2C31067628%2C31065721&oid=2&pvsid=4317841587184629&tmod=4619812&uas=0&nvt=1&eae=0&fc=896&brdim=0%2C0%2C0%2C0%2C428%2C0%2C428%2C926%2C428%2C781&vis=1&rsz=%7C%7CoeEbr%7C&abl=CS&pfx=0&fu=0&bc=31&ifi=2&uci=a!2&btvi=1&fsb=1&xpc=ugN4Cd1vXM&p=https%3A//web.archive.org&dtd=2336

    <?php
        // check if current user is the post author
        global $current_user;
        get_currentuserinfo();
        
        if (is_user_logged_in() && $current_user->ID == $post->post_author)  {
            echo 'You are the seller of this item!';
        }
    ?>

    That’s it!

  • Prevent User Registration in WordPress From Specific Domain

    How to Prevent User Registration in WordPress From Specific Domain

    Usually it’s a good idea to disable user registration in WordPress, if you do not use membership feature, since spammers can attack your website with spam user registrations. One of my clients had enabled the option “anyone can register” in WordPress settings and allowed visitors to register. Within as week, he received couple of user signups but soon after he started to receive user registrations, hundreds in number from one specific domain. That made him realize, all user were essentially spam registrations.

    After couple of attempts, I was able to block spam user registrations from this specific domain. I used registration_errors hook in WordPress which filters the errors encountered when a new user is being registered. If any errors are present, WordPress will abort the user’s registration. This filter can also be used to create custom validation rules on user registration. This hook fires when the form is submitted but before user information is saved to the database. So I created custom rule to check user’s email domain and provide error if user’s email match blacklisted domain.

    Here is the function you can dump in your theme’s functions.php file. The following function will prevent user registration if WordPress find “domain.Com” in user’s email address.

    https://web.archive.org/web/20220701035414if_/https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-7443478584383873&output=html&h=280&slotname=3463695748&adk=3644181001&adf=3895680182&pi=t.ma~as.3463695748&w=336&lmt=1775835405&psa=0&format=336×280&url=https%3A%2F%2Fwpcodesnippet.com%2Fprevent-user-registration-from-specific-domain%2F&wgl=1&dt=1656647656858&bpp=1&bdt=2874&idt=1607&shv=r20220613&mjsv=m202206090101&ptt=9&saldr=aa&abxe=1&prev_fmts=336×280&correlator=737306254100&frm=20&pv=1&ga_vid=1720460838.1656647659&ga_sid=1656647659&ga_hid=185658651&ga_fc=0&u_tz=0&u_his=8&u_h=926&u_w=428&u_ah=926&u_aw=428&u_cd=24&u_sd=1&adx=30&ady=1892&biw=428&bih=781&scr_x=0&scr_y=0&eid=44759875%2C44759926%2C44759837%2C42531556%2C44761043%2C31067528%2C31067629%2C31060049%2C42531605%2C31061690&oid=2&pvsid=1442993630606171&tmod=4619812&uas=0&nvt=1&eae=0&fc=896&brdim=0%2C0%2C0%2C0%2C428%2C0%2C428%2C926%2C428%2C781&vis=1&rsz=%7C%7CoeEbr%7C&abl=CS&pfx=0&fu=0&bc=31&ifi=2&uci=a!2&btvi=1&fsb=1&xpc=4k2rVlPlXF&p=https%3A//web.archive.org&dtd=2629

    // prevent user registration in wordpress from specific domain
    function wpcs_disable_email_domain ( $errors, $sanitized_user_login, $user_email ) {
        list( $email_user, $email_domain ) = explode( '@', $user_email );
        if ( $email_domain == 'domain.com' ) {
            $errors->add( 'email_error', __( '<strong>ERROR</strong>: Domain not allowed.', 'my_domain' ) );
        }
        return $errors;
    }
    add_filter( 'registration_errors', 'wpcs_disable_email_domain', 10, 3 );

    Simple. Right!

  • Allow Users to Login With Email Address in WordPress

    How to Allow Users to Login With Email Address in WordPress

    WordPress by default assign a unique username to each user on registration. As usernames are unique on a WordPress website, there is a fine possibility that the username a user wants to register has already been taken by another user, in that case, he have to choose a different username that user might not rememberlater. And if they don’t remember their username then it will be very hard for them to login to your website. I am sure, you must have faced similar situation in past and know how frustrating this is.

    The following snippet will help you fix this issue. Now, your users will be able to login with email addressassigned to their account. We will use wp_authenticate action for this purpose since it runs before WordPress tries to authenticate the user details. We will change the default authentication method used by WordPress and force it to check for email address against user submitted username.

    To allow users to login with email address, simply paste this code snippet in your theme’s functions.php file.

    https://web.archive.org/web/20220521004225if_/https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-7443478584383873&output=html&h=280&slotname=3463695748&adk=2899339934&adf=2615313419&pi=t.ma~as.3463695748&w=336&lmt=1775835312&psa=0&format=336×280&url=https%3A%2F%2Fwpcodesnippet.com%2Flogin-with-email-address-in-wordpress%2F&wgl=1&dt=1653093751444&bpp=1&bdt=6481&idt=2752&shv=r20220613&mjsv=m202206140101&ptt=9&saldr=aa&abxe=1&prev_fmts=336×280%2C300x250%2C0x0&nras=1&correlator=8483018775109&frm=20&pv=1&ga_vid=490464046.1653093755&ga_sid=1653093755&ga_hid=325795584&ga_fc=0&u_tz=0&u_his=7&u_h=926&u_w=428&u_ah=926&u_aw=428&u_cd=24&u_sd=1&adx=30&ady=1767&biw=428&bih=781&scr_x=0&scr_y=0&eid=44759875%2C44759926%2C44759842%2C31067933%2C31067628%2C31068039%2C42531605%2C31064019&oid=2&pvsid=1214473788402877&tmod=1118653441&uas=0&nvt=1&eae=0&fc=896&brdim=0%2C0%2C0%2C0%2C428%2C0%2C428%2C926%2C428%2C781&vis=1&rsz=%7C%7CoeEbr%7C&abl=CS&pfx=0&fu=0&bc=31&ifi=2&uci=a!2&btvi=1&fsb=1&xpc=eS4GjdNTDZ&p=https%3A//web.archive.org&dtd=6934

    // Allow users to login with email address
    function wpcs_login_email_address( &$username, &$password ) {
        $user = get_user_by( 'email', $username );
        if( !empty( $user->user_login ) ) {
            $username = $user->user_login;
        }
    }
    add_action( 'wp_authenticate', 'wpcs_login_email_address' );
  • Change WordPress Email Content Type to HTML Instead of Text

    How to Change WordPress Email Content Type to HTML Instead of Text

    WordPress uses wp_mail function to send all emails and the default content type for these emails is text/plain. It simply means that all emails, goes through WordPress, will be sent in text format only and WordPress will not allow any HTML to be placed in your emails. But on a project, you may require to send emails in HTML format and for that you must change WordPress email content type, to allow you to have HTML in them. Unfortunately WordPress does not have any built in option to change email content types. But luckily, there is a easy way to change WordPress email content type to text/html from default text/plain.

    To change WordPress email content type, we will use wp_mail_content_type filter. With the help of this filter, we will set the email content type to HTML allowing us to style our emails in anyway we want. This will also allow us to brand our emails with our company logo and insert images in email.

    You can dump this code snippet in your functions.php file. It will automatically change WordPress email content type to HTML. Now start sending HTML emails. 🙂

    // change WordPress email content type to HTML
    function wpcs_set_email_content_type() {
        return 'text/html';
    }
    add_filter( 'wp_mail_content_type', 
  • Stop Loading Contact Form 7 JavaScript And CSS in WordPress

    How to Stop Loading Contact Form 7 JavaScript And CSS in WordPress

    Contact form 7 is undoubtedly one of the most popular plugins in WordPress plugins directory. It makes our life a lot easier by allowing us to create contact forms without any PHP knowledge and add them in our website with the help of shortcode. The plugin is super easy to set up and use. But the only downside I could see is that it automatically inserts couple of JavaScript and CSS files on pages. Although it’s essential to have those files on page for proper functioning of Contact form 7 but it’s useless to keep plugin JavaScript and CSS files on pages that do not have any contact form.

    In this topic, we will see how to remove or stop loading Contact form 7 JavaScript and CSS files. Contact form 7 use standard wp_enqueue_scripts function to load these static plugin files which makes it easy for us to stop them from loading on all WordPress pages and allow them manually, to load on pages that actually contains a contact form.

    So following code snippet will disable the loading of plugin JavaScript and CSS files. You must add this code snippet into your theme’s functions.php file.

    https://web.archive.org/web/20220523115517if_/https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-7443478584383873&output=html&h=280&slotname=3463695748&adk=2438680785&adf=246442902&pi=t.ma~as.3463695748&w=336&lmt=1775835035&psa=0&format=336×280&url=https%3A%2F%2Fwpcodesnippet.com%2Fstop-loading-contact-form-7-javascript-and-css%2F&wgl=1&dt=1653306921116&bpp=1&bdt=4153&idt=7200&shv=r20220613&mjsv=m202206140101&ptt=9&saldr=aa&abxe=1&prev_fmts=336×280%2C300x250%2C0x0&nras=1&correlator=7679915271680&frm=20&pv=1&ga_vid=1932052441.1653306929&ga_sid=1653306929&ga_hid=241297331&ga_fc=0&u_tz=0&u_his=7&u_h=926&u_w=428&u_ah=926&u_aw=428&u_cd=24&u_sd=1&adx=30&ady=1792&biw=428&bih=781&scr_x=0&scr_y=0&eid=44759875%2C44759926%2C44759842%2C42531556%2C31068039%2C31064019&oid=2&pvsid=1341909750081000&tmod=1118653441&uas=0&nvt=1&eae=0&fc=896&brdim=0%2C0%2C0%2C0%2C428%2C0%2C428%2C926%2C428%2C781&vis=1&rsz=%7C%7CoeEbr%7C&abl=CS&pfx=0&fu=0&bc=31&ifi=2&uci=a!2&btvi=1&fsb=1&xpc=KqCj4au9CH&p=https%3A//web.archive.org&dtd=9295

    // stop loading Contact form 7 JavaScript and CSS files
    add_filter( 'wpcf7_load_js', '__return_false' );
    add_filter( 'wpcf7_load_css', '__return_false' );

    Super easy. Right?

    Now if we want to include Contact form 7 JavaScript and CSS files on a page, then we will add this code snippet.

    // selectively load Contact form 7 JavaScript and CSS files
    function rapidx_deregister_javascript() {
        if ( is_page( array( 'contribute', 'contact-us' ) ) ) {
            if ( function_exists( 'wpcf7_enqueue_scripts' ) ) {
                wpcf7_enqueue_scripts();
            }
            if ( function_exists( 'wpcf7_enqueue_styles' ) ) {
                wpcf7_enqueue_styles();
            }
        }
    }
    add_action( 'wp_enqueue_scripts',
  • Change the Search Results Page URL Slug to ‘/search/term’

    How to Change the Search Results Page URL Slug to ‘/search/term’

    Default WordPress URL Structure is not the most Search Engine friendly, and it certainly is not very user friendly too. But WordPress has built in option to enable SEO friendlyURLs to make URLs more user friendly. All you need to do is to login to WordPress admin panel and pick a permalink structure of your choice on Permalink Option page. Enabling SEO friendly URLs on your website will force WordPress to follow permalink structure for most part of the website but it wouldn’t change search results page URLwhich by default is “/?s=term“. So in this topic we will see how we can change it to more user friendly and rewrite the search results page URL slug to “/search/term“.

    For this purpose we will use template_redirect action hook. We will first check if a user is accessing search results page and if search does not return empty results. If both of the conditions are true then WordPress will redirect user to “/search/term” link instead of default “/?s=term”. Following is the function we wrote to change the search results page URL slug to ‘/search/term’. You can paste this code in your theme’s functions.php file without any modification.

    https://web.archive.org/web/20220712154914if_/https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-7443478584383873&output=html&h=280&slotname=3463695748&adk=2591867861&adf=1664325628&pi=t.ma~as.3463695748&w=336&lmt=1775834902&psa=0&format=336×280&url=http%3A%2F%2Fwpcodesnippet.com%2Fchange-search-results-page-url-slug%2F&wgl=1&dt=1657640959196&bpp=1&bdt=5228&idt=2352&shv=r20220613&mjsv=m202206090101&ptt=9&saldr=aa&abxe=1&prev_fmts=336×280&correlator=3839844517360&frm=20&pv=1&ga_vid=865867330.1657640963&ga_sid=1657640963&ga_hid=865787196&ga_fc=0&u_tz=0&u_his=7&u_h=926&u_w=428&u_ah=926&u_aw=428&u_cd=24&u_sd=1&adx=30&ady=1774&biw=428&bih=781&scr_x=0&scr_y=0&eid=44759875%2C44759926%2C44759837%2C31068022&oid=2&pvsid=161143458889152&tmod=4619812&uas=0&nvt=1&eae=0&fc=896&brdim=0%2C0%2C0%2C0%2C428%2C0%2C428%2C926%2C428%2C781&vis=1&rsz=%7C%7CoeEbr%7C&abl=CS&pfx=0&fu=0&bc=31&ifi=2&uci=a!2&btvi=1&fsb=1&xpc=uEpbOpXw6e&p=https%3A//web.archive.org&dtd=3447

    // change the search results page URL slug
    function wpcs_search_url_rewrite_rule() {
      if ( is_search() && !empty( $_GET['s'] ) ) {
        wp_redirect( home_url( '/search/' ) . urlencode( get_query_var( 's' ) ) );
        exit();
      }  
    }
    add_action( 'template_redirect', 'wpcs_search_url_rewrite_rule' );

    That’s it. Now your users are redirecting to “/search/term” search results page URL.

  • Strip HTML Tags From the_terms Function Output

    How to Strip HTML Tags From the_terms Function Output

    If you need to display the terms list of the current post on your website then you must use the_termsfunction. WordPress function the_term displays the linked terms of a taxonomy separated by user defined characters. It’s very powerful function to get list of categoriestags or any other custom defined taxonomies in WordPress. The only issue I can see is that it returns anchor HTML links of the terms which is fine in most general cases since most bloggers just want a way to list terms on their WordPress website. But on a project you may want to display the list of terms without anchor links, in just plain text. So in this article I will show you how we can strip HTML tags from the_termsoutput.

    It is relatively easy since we can hook the WordPress filter to alter the output of the function. We will use strip_tags PHP function to simply strip any HTML tags in terms list. So here is the code snippet to do just that. Adding this snippet to the functions.php file of your WordPress theme will strip HTML tags from the_terms. Now you can display all your terms in just plain text and without any anchor links.

    https://web.archive.org/web/20220523110311if_/https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-7443478584383873&output=html&h=280&slotname=3463695748&adk=489613739&adf=3607016135&pi=t.ma~as.3463695748&w=336&lmt=1775834778&psa=0&format=336×280&url=https%3A%2F%2Fwpcodesnippet.com%2Fstrip-html-tags-from-the_terms%2F&wgl=1&dt=1653303793490&bpp=1&bdt=2507&idt=2152&shv=r20220613&mjsv=m202206140101&ptt=9&saldr=aa&abxe=1&prev_fmts=336×280&correlator=8784253287945&frm=20&pv=1&ga_vid=1357845291.1653303797&ga_sid=1653303797&ga_hid=502073723&ga_fc=0&u_tz=0&u_his=7&u_h=926&u_w=428&u_ah=926&u_aw=428&u_cd=24&u_sd=1&adx=30&ady=1749&biw=428&bih=781&scr_x=0&scr_y=0&eid=44759875%2C44759926%2C44759842%2C31065544%2C31067768%2C31068039%2C31067488&oid=2&pvsid=3380730692499310&tmod=1118653441&uas=0&nvt=1&eae=0&fc=896&brdim=0%2C0%2C0%2C0%2C428%2C0%2C428%2C926%2C428%2C781&vis=1&rsz=%7C%7CoeEbr%7C&abl=CS&pfx=0&fu=0&bc=31&ifi=2&uci=a!2&btvi=1&fsb=1&xpc=DzoP3xAATl&p=https%3A//web.archive.org&dtd=3172

    // strip HTML tags from the_terms
    function wpcs_strip_html_from_terms( $term_list, $taxonomy, $before, $sep, $after ) {
        return strip_tags( $them_list );
    }
    add_filter( 'the_terms', 'wpcs_strip_html_from_terms', 10, 5 );

    Simple. Right!

  • Disable Content Editor for Specific Page Template

    How to Disable Content Editor for Specific Page Template

    While working on a project I needed a way to disable content editor on a specific page. Why you asked? On this project, I created a custom page which has lots of meta-boxes on editor screen and we were not using visual editor for adding any content on that page so we wanted to disable content editor to avoid confusion for end user. As a solution, the first thing that came to my mind is to dynamically hide visual editor with JavaScript and CSS. Although it would work just find but it’s not a proper solution so I tried to follow a different approach to accomplish this task.

    Finally, I came across the WordPress function remove_post_type_support which is used to remove support of certain features for a given post type. So I tried to utilize this function to disable content editor on edit screen for a specific page template. I put together a code snippet that you can add in functions.php of your WordPress theme. It will disable visual content editor when a page is using a specific template.

    https://web.archive.org/web/20220521004451if_/https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-7443478584383873&output=html&h=280&slotname=3463695748&adk=1936710829&adf=2914854856&pi=t.ma~as.3463695748&w=336&lmt=1775834667&psa=0&format=336×280&url=http%3A%2F%2Fwpcodesnippet.com%2Fdisable-content-editor-for-specific-page-template%2F&wgl=1&dt=1653093900827&bpp=1&bdt=9860&idt=10120&shv=r20220613&mjsv=m202206140101&ptt=9&saldr=aa&abxe=1&prev_fmts=336×280&correlator=886962003436&frm=20&pv=1&ga_vid=58262051.1653093912&ga_sid=1653093912&ga_hid=1743329949&ga_fc=0&u_tz=0&u_his=7&u_h=926&u_w=428&u_ah=926&u_aw=428&u_cd=24&u_sd=1&adx=30&ady=1724&biw=428&bih=781&scr_x=0&scr_y=752&eid=44759875%2C44759926%2C44759842%2C42531556%2C31067629%2C31068031%2C31068039%2C42531605&oid=2&pvsid=3489942211240886&tmod=1118653441&uas=0&nvt=1&eae=0&fc=896&brdim=0%2C0%2C0%2C0%2C428%2C0%2C428%2C926%2C428%2C821&vis=1&rsz=%7C%7CoeEbr%7C&abl=CS&pfx=0&fu=0&bc=31&ifi=2&uci=a!2&btvi=1&fsb=1&xpc=Kfry1GDTtq&p=https%3A//web.archive.org&dtd=11139

    // disable content editor for page template
    function wpcs_disable_content_editor() {
    
        $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
    
        if( !isset( $post_id ) ) return;
    
        $template_file = get_post_meta($post_id, '_wp_page_template', true);
    
        if ( $template_file == 'page-custom.php' ) {
            remove_post_type_support( 'page', 'editor' );
        }
    
    }
    add_action( 'admin_init', 'wpcs_disable_content_editor' );

    Don’t forget to change the name of the template file on above code. I have used page-custom.php in this example but it can be changed to anything.

  • Add Featured Post Thumbnails to WordPress Admin Post Columns

    How to Add Featured Post Thumbnails to WordPress Admin Post Columns

    Have you ever wanted to see the featured post thumbnails associated with your posts, without going through each post individually? Wouldn’t it be nice if you can see all post thumbnails while viewing the list of posts in the WordPress admin? If you want this feature to add in your website then you are in luck because we just have a perfect solution for you. We have a code snippet that does just that by adding an additional column to the list of posts and pages in WordPress admin and displays the featured post thumbnails in it.

    WordPress filters mange_posts_columns and manage_posts_custom_column allow us to add custom columns to the list of post & pages in the WordPress admin. These two filters control which columns should be displayed when a user views the list of posts or pages. So we used these functions to add an additional column to the list of posts and pages for displaying featured post thumbnails. You can add this code snippet in your functions.php file of your current theme.

    https://web.archive.org/web/20220521000428if_/https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-7443478584383873&output=html&h=280&slotname=3463695748&adk=383361705&adf=3150202812&pi=t.ma~as.3463695748&w=336&lmt=1775834539&psa=0&format=336×280&url=http%3A%2F%2Fwpcodesnippet.com%2Fadd-featured-post-thumbnails-to-post-columns%2F&wgl=1&dt=1653091490865&bpp=1&bdt=22903&idt=1882&shv=r20220613&mjsv=m202206140101&ptt=9&saldr=aa&abxe=1&prev_fmts=336×280%2C300x250%2C0x0&nras=1&correlator=3431599922232&frm=20&pv=1&ga_vid=1836101822.1653091494&ga_sid=1653091494&ga_hid=20464061&ga_fc=0&u_tz=0&u_his=7&u_h=926&u_w=428&u_ah=926&u_aw=428&u_cd=24&u_sd=1&adx=30&ady=1724&biw=428&bih=781&scr_x=0&scr_y=496&eid=44759875%2C44759926%2C44759842%2C31068039%2C31062930&oid=2&pvsid=3798946907895945&tmod=1118653441&uas=0&nvt=1&eae=0&fc=896&brdim=0%2C0%2C0%2C0%2C428%2C0%2C428%2C926%2C428%2C821&vis=1&rsz=%7C%7CoeEbr%7C&abl=CS&pfx=0&fu=0&bc=31&ifi=2&uci=a!2&btvi=1&fsb=1&xpc=4YzxBWuQJk&p=https%3A//web.archive.org&dtd=15838

    // add featured thumbnail to admin post columns
    function wpcs_add_thumbnail_columns( $columns ) {
        $columns = array(
            'cb' => '<input type="checkbox" />',
            'featured_thumb' => 'Thumbnail',
            'title' => 'Title',
            'author' => 'Author',
            'categories' => 'Categories',
            'tags' => 'Tags',
            'comments' => '<span class="vers"><div title="Comments" class="comment-grey-bubble"></div></span>',
            'date' => 'Date'
        );
        return $columns;
    }
    
    function wpcs_add_thumbnail_columns_data( $column, $post_id ) {
        switch ( $column ) {
        case 'featured_thumb':
            echo '<a href="' . get_edit_post_link() . '">';
            echo the_post_thumbnail( 'thumbnail' );
            echo '</a>';
            break;
        }
    }
    
    if ( function_exists( 'add_theme_support' ) ) {
        add_filter( 'manage_posts_columns' , 'wpcs_add_thumbnail_columns' );
        add_action( 'manage_posts_custom_column' , 'wpcs_add_thumbnail_columns_data', 10, 2 );
        add_filter( 'manage_pages_columns' , 'wpcs_add_thumbnail_columns' );
        add_action( 'manage_pages_custom_column' , 'wpcs_add_thumbnail_columns_data', 10, 2 );
    }

    This is a very handy code snippet if you use post thumbnail feature very heavily and don’t want to have to edit each post in order to see the thumbnail being used.

  • Redirect WooCommerce User to Checkout Page After Add to Cart

    How to Redirect WooCommerce User to Checkout Page After Add to Cart

    WooCommerce is one of the best e-commerce plugin for WordPress. I have used it on couple of website that I built for my clients and setting up e-commerce platform with WooCommerce is super-duper easy. Although the plugin works flawlessly out of the box but there is always room for improvement. Right! So today I am going to share a code snippet, that I used on a project, to redirect WooCommerce user to checkout page after adding a product to cart. This is especially useful snippet for small e-commerce stores having couple of items to sell or if you’re building a landing page with an Add to Cart feature.

    WooCommerce provides many useful filter and action hooks that we can use to ‘hook into’ the code for changing the default behaviors of system. Here I used woocommerce_add_to_cart_redirect hook that fires just after adding a product in cart. I used this hook to force WordPress to redirect WooCommerce user to Checkout Page after add to cart. So here’s a very simple piece of code that you can add to the file functions.php.

    https://web.archive.org/web/20220520234056if_/https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-7443478584383873&output=html&h=280&slotname=3463695748&adk=262512233&adf=168099604&pi=t.ma~as.3463695748&w=336&lmt=1775834328&psa=0&format=336×280&url=http%3A%2F%2Fwpcodesnippet.com%2Fredirect-woocommerce-user-to-checkout-page-after-add-to-cart%2F&wgl=1&dt=1653090058898&bpp=1&bdt=2915&idt=1573&shv=r20220613&mjsv=m202206140101&ptt=9&saldr=aa&abxe=1&prev_fmts=336×280&correlator=5680206778282&frm=20&pv=1&ga_vid=879887859.1653090061&ga_sid=1653090061&ga_hid=1032298188&ga_fc=0&u_tz=0&u_his=7&u_h=926&u_w=428&u_ah=926&u_aw=428&u_cd=24&u_sd=1&adx=30&ady=1724&biw=428&bih=781&scr_x=0&scr_y=0&eid=44759875%2C44759926%2C44759842%2C31067528%2C31068039%2C31062931&oid=2&pvsid=964408947982223&tmod=1118653441&uas=0&nvt=1&eae=0&fc=896&brdim=0%2C0%2C0%2C0%2C428%2C0%2C428%2C926%2C428%2C781&vis=1&rsz=%7C%7CoeEbr%7C&abl=CS&pfx=0&fu=0&bc=31&ifi=2&uci=a!2&btvi=1&fsb=1&xpc=2xL6V49qhA&p=https%3A//web.archive.org&dtd=2603

    // redirect woocommerce user to checkout page after add to cart
    function wpcs_add_to_cart_redirect( $url ) {
        global $woocommerce;
        $checkout_url = $woocommerce->cart->get_checkout_url();
        return $checkout_url;
    }
    add_filter( 'woocommerce_add_to_cart_redirect', 'wpcs_add_to_cart_redirect' );

    Super simple, but it could boost your conversion rate.