Category: Post & Pages

  • 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!

  • 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.