Get the value from multiple checkboxes


<html>
<body>
<h3>Get Value from multiple checkbox in PHP</h3>
<form action="test.php" method="post">
<input type="checkbox" name="fruit_list[]" value="1">Apple 
<input type="checkbox" name="fruit_list[]" value="2">Banana 
<input type="checkbox" name="fruit_list[]" value="3">Mango
<input type="checkbox" name="fruit_list[]" value="4">Guava</br></br>
<input type="checkbox" name="fruit_list[]" value="5">Orange</br></br>
<input type="submit" value="Save" />
</form>
<body>
</html>
<?php
if(isset($_POST['fruit_list']))
{
$fruit_list = array();
foreach($_POST['fruit_list'] as $val)
{
$fruit_list[] = (int) $val;
}
$fruit_list = implode(',', $fruit_list);
echo "Fruits Value are :- ".''.$fruit_list;
exit;
}
?>

Redirect users/customers away from wp-admin

Use this code in the functions.php file for the active theme. Currently it will redirect users to /my-account – change as you like

add_action( 'init', 'blockusers_init' );
function blockusers_init() {
if ( is_admin() && ! current_user_can( 'administrator' ) &&
! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
wp_redirect( home_url() . "/my-account" );
exit;
}
}

Chroot sftp user/group directory.

This is a simple reference to chroot a sftp user or group to a folder – usually the web server folder. It is not covered “full” chrooting.

Edit /etc/ssh/sshd_config (/etc/sshd_config on some distributions) and set the following options:

#Subsystem sftp internal-sftp
#In some cases you might need to uncomment the line above and comment existing #Susbsytem option 
#Match user sftp-user
Match group sftp
ChrootDirectory /var/www
ForceCommand internal-sftp
AllowTcpForwarding no

Be sure to place the “Match” directive at the end of the file. This tells OpenSSH that all users in the sftp group are to be chrooted to their home directory (which %h represents in the ChrootDirectory command – you can use it instead of “/var/www” in this case), or any other you specify.

Don’t leave two or more Subsytem sftp directives at the same time – use only one. Otherwise you wont be able to access the server from ssh!

For any users that you wish to chroot, add them to the sftp group by using:

# usermod -G sftp paul
# usermod -s /bin/false paul
# chown root:root /home/paul
# chmod 0755 /home/paul

If you still have problems it is most probably because of directory permissions or/and ownership – you can try this:


sudo chown root /var/www
sudo chmod go-w /var/www
sudo mkdir /var/www/writeable
sudo chown bob:sftponly /var/www/writeable
sudo chmod ug+rwX /var/www/writeable

Be very careful as changes to sshd_config might leave you without ssh and sftp access to the server!!!

PHP GeoIP redirection.

This script uses the Maxmind GeoIP redirection which in this case is hosted by https://freegeoip.net which will return a XML with the details of the visitors IP.


<?php 

//Start session to avoid infinity loops
session_start();
// store session data
$_SESSION['geo']=1;


//Get the client country and set ip that not be redirected
$countriesrow = array('US','CA','AP','AR','BR','CN','CL','CO','CR','GT','HK','JP','KR','MX','SG','TH','TW'); //These are the countries that would be redirected to the normal site
$countrieseuro = array('AT','BE','BG','CH','CZ','DE','DK','EE','EG','ES','EU','FI','FR','GB','GR','HR','HU', 'IE','IL','IN','IS','IT','LU','LV','MA','MC','NL','NO','PL','PT','RO','RS','RU','SE','SI','SK','UA','ZA'); //This will be redirected to the eu version
$admin = '31.53.62.141'; //Put here your IP

//Get the client IP
$ipc =  $_SERVER['REMOTE_ADDR'];
$surl = "https://freegeoip.net/xml/".$ipc;

//Check if freegeoip is available
$response = get_headers($surl);
$response = $response[0];
if ($response == "HTTP/1.1 200 OK") {
	$avl = 1;
} else {
	$avl = 0;
}
if ($avl == 1){
$getxml = trim(file_get_contents($surl)); // get raw post data + trim out the spaces
$xmlsimp = new SimpleXMLElement($getxml);  // convert to object array
$ccode = $xmlsimp->CountryCode;  // returns the country code of the visitor example "US"
}
$country = $ccode;

//$xmlsimp->Ip; // returns the Ip address of the visitor
$gurl = $_SERVER['REQUEST_URI']; //returns the current URL
$parts = explode('/',$gurl);
$cdir = $_SERVER['SERVER_NAME'];
for ($i = 0; $i < count($parts) - 1; $i++) {
 $cdir .= $parts[$i] . "/";
}

$urlParts = explode('.', $_SERVER['HTTP_HOST']);
$subdomain = $urlParts[0]; //or 0 if no www


if ($subdomain !== "eu" && in_array($country, $countrieseuro) && $_SESSION['geo'] == 1 &&  $avl == 1  && ($admin !== $ipc)) 
	{ header('Location: https://eu.rockandpebble.com' . $gurl . '');
	  exit;
	}
elseif ($subdomain !== "eu" && in_array($country, $countrieseuro) && $_SESSION['geo'] == 1 &&  $avl == 1 && ($admin !== $ipc))  
	{ 
		header('Location: https://rockandpebble.com' . $gurl . '');
	  exit;
	}
?>


Check System Load and restart services


#!/bin/sh
# script to check server for extremely high load and restart Apache/Nginx/Varnish/MySQL if the cond$
check=`cat /proc/loadavg | sed 's/\./ /' | awk '{print $1}'`
# define max load avarage when script is triggered
max_load='9'
# log file
high_load_log='/var/log/server_high_load_restart.log';
#
if [ $check -gt "$max_load" ]; then
service nginx restart
sleep 5;
service php5-fpm restart
sleep 5;
service varnish restart
echo "$(date) : Server Restart due to excessive load | $check |" >> $high_load_$
fi



Shell VPS Backup Script

This script uses rsync and ssh keys over ssh to backup remote server with anacron, it should work with cron too, but it haven’t been tested.


#!/bin/sh
#Script for VPS back ups

#This is for the desktop notification on Ubuntu
notify-send -i drive-multidisk Rsync "VPS Backup Started"

#This is for the reallusion VPS
rsync -e "ssh -i /home/user/.ssh/id_rsa.2 -p 104" -rtv --delete root@domain.com:/home /mnt/DATA-L/backups
rsync -e "ssh -i /home/user/.ssh/id_rsa.2 -p 104" -rtv --delete root@domain.com:/etc /mnt/DATA-L/backups
rsync -e "ssh -i /home/user/.ssh/id_rsa.2 -p 104" -rtv --delete root@domain.com:/var/log /mnt/DATA-L/backups
rsync -e "ssh -i /home/user/.ssh/id_rsa.2 -p 104" -rtv --delete root@domain.com:/var/mail /mnt/DATA-L/backups
rsync -e "ssh -i /home/user/.ssh/id_rsa.2 -p 104" -rtv --delete root@domain.com:/var/backups /mnt/DATA-L/backups

#This is for the desktop notification on Ubuntu
notify-send -i drive-multidisk Rsync "VPS Backup Finished"

Simple Text Sliding

This will slide the according div id.


function ani(){
    $('#text').animate(
        {'margin-left': '1000px'}, 
        10000, // duration
        function(){
            $('#text').css('margin-left','-100px');
            ani();
        });
     $("#text").hover(function(){
    $("#text").stop();
  });    
    $("#text").mouseout(function(){
    ani();
  });
    }
ani();

Theme Options Page

Sample code for theme options page. It can be added to the functions.php file of the theme, or extend it and put in on different file and then include the file in functions.php
Note that the fields are not sanitized, refer to WordPress site for more info.


 
<div id="theme-options-wrap">
<div class="icon32" id="icon-tools"></div>
<h2>Theme Settings</h2>
Update various settings throughout your website.

<form action="options.php" enctype="multipart/form-data" method="post"><!--?php settings_fields('theme_options'); ?--> <!--?php do_settings_sections(__FILE__); ?-->
<p class="submit"><input class="button-primary" type="submit" name="Submit" value="<?php esc_attr_e('Save Changes'); ?>" /></p>

</form></div>
<!--?php <br ?-->
} add_action('admin_init', 'register_and_build_fields');

function register_and_build_fields() {

register_setting('theme_options', 'theme_options', 'validate_setting');

add_settings_section('homepage_settings', 'Homepage Settings', 'section_homepage', __FILE__);

add_settings_section('footer_settings', 'Footer Settings', 'section_footer', __FILE__);

function section_homepage() {

}

function section_footer() {

}

add_settings_field('button1text', 'Button 1 Text', 'button1text_setting', __FILE__, 'homepage_settings');

add_settings_field('button1link', 'Button 1 URL', 'button1link_setting', __FILE__, 'homepage_settings');

add_settings_field('button2text', 'Button 2 Text', 'button2text_setting', __FILE__, 'homepage_settings');

add_settings_field('button2link', 'Button 2 URL', 'button2link_setting', __FILE__, 'homepage_settings');

add_settings_field('button3text', 'Button 3 Text', 'button3text_setting', __FILE__, 'homepage_settings');

add_settings_field('button3link', 'Button 3 URL', 'button3link_setting', __FILE__, 'homepage_settings');

add_settings_field('phonenumber', 'Phone Number', 'phonenumber', __FILE__, 'footer_settings');

add_settings_field('facebookurl', 'Facebook URL', 'facebookurl', __FILE__, 'footer_settings');

add_settings_field('googleurl', 'Google+ URL', 'googleurl', __FILE__, 'footer_settings');

add_settings_field('twitterurl', 'Twitter URL', 'twitterurl', __FILE__, 'footer_settings');

}

function validate_setting($theme_options) {

return $theme_options;

}

function button1text_setting() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[button1text_setting]" value="{$options[" />";

}

function button1link_setting() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[button1link_setting]" value="{$options[" />";

}

function button2text_setting() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[button2text_setting]" value="{$options[" />";

}

function button2link_setting() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[button2link_setting]" value="{$options[" />";

}

function button3text_setting() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[button3text_setting]" value="{$options[" />";

}

function button3link_setting() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[button3link_setting]" value="{$options[" />";

}

function phonenumber() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[phonenumber]" value="{$options[" />";

}

function facebookurl() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[facebookurl]" value="{$options[" />";

}

function googleurl() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[googleurl]" value="{$options[" />";

}

function twitterurl() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[twitterurl]" value="{$options[" />";

}

add_action('admin_menu', 'theme_options_page');

function theme_options_page() {

add_menu_page('Theme Settings', 'Theme Settings', 'administrator', __FILE__, 'build_options_page', '', '62.2');

}

Then we create a variable with all our settings so we can use it in the template.

<?php $options = get_option('theme_options'); ?>

Then we can use some of the options like this:

<a href="<?php echo $options['button1link_setting']; ?>"><?php echo $options['button1text_setting']; ?></a>

Remove comments

This code snippet will remove the comments from everywhere, just put in the functions.php file in the theme. Here is the code:

<?php
// Removes from admin menu
add_action( 'admin_menu', 'my_remove_admin_menus' );
function my_remove_admin_menus() {
    remove_menu_page( 'edit-comments.php' );
}
// Removes from post and pages
add_action('init', 'remove_comment_support', 100);

function remove_comment_support() {
    remove_post_type_support( 'post', 'comments' );
    remove_post_type_support( 'page', 'comments' );
}
// Removes from admin bar
function mytheme_admin_bar_render() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('comments');
}
add_action( 'wp_before_admin_bar_render', 'mytheme_admin_bar_render' );
?>