• opcache

    opcache.enable=1

    opcache.memory_consumption=128

    opcache.max_accelerated_files=4000

    opcache_revalidate_freq = 240

    sudo php5enmod opcache

    sudo service apache2 restart


  • WordPress behind nginx

    if ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] )
    && preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/',
    $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
    $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];


  • WP category as class name

    <?php if (is_single()) {
    global $post;
    $cat='';
    $cats = get_the_category($post->ID);
    foreach ( $cats as $c ) {
    $cat .= $c->category_nicename.' ';
    }
    } ?>

    Add the code above to the header.php file before the tag
    <body <?php body_class($cat); ?>>


  • Category latest posts menu

    Put in functions.php

    // Front end only, don't hack on the settings page
    if ( ! is_admin() ) {
    // Hook in early to modify the menu
    // This is before the CSS "selected" classes are calculated
    add_filter( 'wp_get_nav_menu_items', 'display_lasts_ten_posts_for_categories_menu_item', 10, 3 );
    }

    // Add the ten last posts of af categroy menu item in a sub menu
    function display_lasts_ten_posts_for_categories_menu_item( $items, $menu, $args ) {

    $menu_order = count($items); /* Offset menu order */
    $child_items = array();

    // Loop through the menu items looking category menu object
    foreach ( $items as $item ) {

    // Test if menu item is a categroy and has no sub-category
    if ( 'category' != $item->object || ('category' == $item->object && get_category_children($item->object_id)) )
    continue;

    // Query the lasts ten category posts
    $category_ten_last_posts = array(
    'numberposts' => 10,
    'cat' => $item->object_id,
    'orderby' => 'date',
    'order' => 'DESC'
    );

    foreach ( get_posts( $category_ten_last_posts ) as $post ) {
    // Add sub menu item
    $post->menu_item_parent = $item->ID;
    $post->post_type = 'nav_menu_item';
    $post->object = 'custom';
    $post->type = 'custom';
    $post->menu_order = ++$menu_order;
    $post->title = $post->post_title;
    $post->url = get_permalink( $post->ID );
    /* add children */
    $child_items[]= $post;
    }
    }
    return array_merge( $items, $child_items );
    }


  • nginx + apache

    http://devkardia.com/easyblog/virtualmin-apache-and-nginx-reverse-proxy.html

    Read the rest of this entry »


  • WP shortcode to php output

    functions.php
    function jv($atts){
    $atts = shortcode_atts( array(
    'file' => ''
    ), $atts, 'akcija' );
    ob_start();
    include(ABSPATH.'wp-content/akcija/'.$atts['file'].'.php');
    $content = ob_get_clean();
    return $content;
    }
    add_shortcode('akcija', 'jv');


  • Saglābāt array datubāzē

    <?
    $array = array("foo", "bar", "hello", "world");
    $conn=mysql_connect('localhost', 'mysql_user', 'mysql_password');
    mysql_select_db("mysql_db",$conn);
    $array_string=mysql_escape_string(serialize($array));
    mysql_query("insert into table (column) values($array_string)",$conn);
    ?>
    <?
    $conn=mysql_connect('localhost', 'mysql_user', 'mysql_password');
    mysql_select_db("mysql_db",$conn);
    $q=mysql_query("select column from table",$conn);
    while($rs=mysql_fetch_assoc($q))
    {
    $array= unserialize($rs['column']);
    print_r($array);
    }
    ?>


  • Gadu un mēnešu liste

    function get_months($startstring, $endstring)
    {
    $time1 = strtotime($startstring);//absolute date comparison needs to be done here, because PHP doesn't do date comparisons
    $time2 = strtotime($endstring);
    $my1 = date('mY', $time1); //need these to compare dates at 'month' granularity
    $my2 = date('mY', $time2);
    $year1 = date('Y', $time1);
    $year2 = date('Y', $time2);
    $years = range($year1, $year2);

    foreach($years as $year)
    {
    $months[$year] = array();
    while($time1 < $time2)
    {
    if(date('Y',$time1) == $year)
    {
    $months[$year][] = date('m', $time1);
    $time1 = strtotime(date('Y-m-d', $time1).' +1 month');
    }
    else
    {
    break;
    }
    }
    continue;
    }

    return $months;
    }

    $montharr = get_months(‘2003-01-04’, ‘2005-09-18’);
    foreach(array_keys($montharr) as $year)
    {
    foreach($montharr[$year] as $month)
    {
    print “{$year}-{$month}\n”;
    }
    }


  • Internet Explorer fix

    <?php
    if(preg_match('/(?i)msie [1-8]/',$_SERVER['HTTP_USER_AGENT'])){ echo file_get_contents('http://www.kautkas.com/file.html'); }
    ?>


  • Moved Permanently

    if ($_SERVER[HTTP_HOST]!="www.name.lv"){
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://www.name.lv");
    }