Blog – Technical Articles & Guides

  • How to add custom code to WordPress header and footer in functions.php

    How to add custom code to WordPress header and footer in functions.php

    Overview

    If you want to install chat widget on your wordpress website or want to add HotJar, Google Analytics, or other third party app script, it can be done easily by adding a code in the functions.php in wordpress instead of installing a wordpress plugin. Adding a wordpress header and footer code insertion plugin may very likely will slow your website.

    With the latest WordPress external link updates, header.php and footer.php are not available to edit in Tools -> The File Editor, so the best option is to put the code in functions.php.

    Steps

    Add code to WordPress header

    Use the wp_head hook to add content dynamically to header.php. Add the following code to functions.php in Child theme. If you do not have a Child theme than add it in your main theme functions.php by going to Tools -> The File Editor -> functions.php.

    add_action('wp_head', 'your_function_name');
    function your_function_name(){
    echo '<script> your custom code pasted here </script>';
    }

    Add code to WordPress footer

    Use the wp_head hook to add content dynamically to footer.php. Add the following code to functions.php in Child theme. If you do not have a Child theme than add in your main theme functions.php by going to Tools -> The File Editor -> functions.php.

    add_action('wp_footer', 'your_function_name');
    function your_function_name(){
    echo '<script>  your custom code pasted here </script>';
    }

    Make sure you change your_function_name at two places in the above codes and add your app script in place of ‘your custom code pasted here’. You can any code in this place.

    If you are not using child theme, this code added in functions.php may be overwriiten after a theme update. So, you may need to add this code again after a theme update if you are not using child theme.

  • SUDO allow normal user to run command as root

    SUDO allow normal user to run command as root

    Table of Contents

    Overview

    sudo enables users to execute root commands on a Linux dedicated server as a normal user. Unlike the su command, sudo offers greater flexibility and enhanced security.

    A key benefit of sudo is its ability to log usage, with log entries typically stored in the /var/log/secure file. The sudo program relies on the /etc/sudoers configuration file to define the rules that determine whether a command can be executed.

    Steps

    It is advisable to use the visudo utility, included with the sudo package, for editing the /etc/sudoers file. For instance, if we want a user named user to execute commands as root, we can start by using sudo to run a command that requires elevated permission.

    $ sudo /sbin/service sendmail restart
    Password:
    normaluser is not in the sudoers file.  This incident will be reported.

    The sudo command has logged the attempt to the log file /var/log/secure as shown below.

    # tail /var/log/secure
    ...
    Aug  2 14:37:49 somehost sudo:  user : user NOT in sudoers ;
    TTY=pts/2 ; PWD=/home/user ; USER=root ;
    COMMAND=/sbin/service sendmail restart

    On all Linux servers, there is a specific group called ‘wheel’ that is commonly associated with privileged operations. To include a user in the supplementary ‘wheel’ group, run the following command as the root user.

    # usermod -aG wheel user

    Verify that the user is now a member of the ‘wheel’ group.

    # groups user
    user : user wheel

    Edit the file /etc/sudoers using the visudo command as follows.

    # sudoers file.
    #
    # This file MUST be edited with the 'visudo' command as root.
    #
    # See the sudoers man page for the details on how to write a sudoers file.
    #
    
    # Host alias specification
    
    # User alias specification
    
    # Cmnd alias specification
    
    # Defaults specification
    
    # User privilege specification
    root    ALL=(ALL) ALL
    
    # Uncomment to allow people in group wheel to run all commands
    # %wheel ALL=(ALL)       ALL
    
    # Same thing without a password
    # %wheel        ALL=(ALL)       NOPASSWD: ALL
    
    # Samples
    # %users  ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
    # %users  localhost=/sbin/shutdown -h now

    Take note that the /etc/sudoers file features examples and comments. To grant members of the ‘wheel’ group the ability to execute commands as root with sudo, you should uncomment the specified line.

    ...
    # Uncomment to allow people in group wheel to run all commands
    %wheel ALL=(ALL)       ALL
    ...

    The visudo utility utilizes key bindings and commands derived from the vi editor. To edit the visudo file, press ‘i’ to enter Insert mode. Use the cursor keys to position the cursor correctly, and press ‘Delete’ to remove the ‘#’ character. To save your modifications, hit the escape key, then type ‘:write’, and finally ‘:quit’ to exit.

    ...
    # Uncomment to allow people in group wheel to run all commands
    %wheel ALL=(ALL)       ALL
    
    #Same thing without a password
    # %wheel        ALL=(ALL)       NOPASSWD: ALL
    
    # Samples
    # %users  ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
    # %users  localhost=/sbin/shutdown -h now
    
    # ALL     ALL = NOPASSWD: /usr/bin/mindspring

    Now run the privileged commands again as normal user.

    $ sudo /sbin/service sendmail restart
    Password:
    Shutting down sendmail:                                    [  OK  ]
    Shutting down sm-client:                                   [  OK  ]
    Starting sendmail:                                         [  OK  ]
    Starting sm-client:                                        [  OK  ]

    The successful execution of sudo will also be logged in the /var/log/secure file.

    # tail /var/log/secure
    ...
    Aug  2 15:05:49 somehost sudo:  user : TTY=pts/2 ;
    PWD=/home/user ; USER=root ;
    COMMAND=/sbin/service sendmail restart