Monday, November 25, 2013

Solution to order pages/posts specific by id in array with WP Query

Have you had problem with not getting the specific posts displaying in the order you have wanted it to be. Normally the Wordpress WP Query is sorted by the post title but what if you don't want to order the query and just have it "un-ordered", the way you write the id's to be.

The solution to this problem is to order your result with the pages to get.

$you_query = new WP_Query(array('post_type' => 'page', 'post__in' => 'array(12, 10, 18, 22)', 'orderby' => 'post__in'));

So that's how you order your result with your array or string of posts. No manual need of ordering or any other hassling.

Wednesday, November 13, 2013

Bootstrap navbar as your Wordpress menu

Do you want to use the bootstrap navbar as a menu in Wordpress? No problems. It takes just a few seconds to config.

What we want to achieve is to use the bootstrap navbar. No plugin is required but we need to download the helper file to achieve this. That one is called wp-bootstrap-navwalker - "A custom Wordpress walker to implement the Twitter Bootstrap dropdown navigation using the Wordpress built in menu manager." That can be found here and the direct download location of the navbar here. Just note that this is for bootstrap 3.0+ and if you have the legacy version (2.3.2) then you need another version of the helper.

Include the file from your functions.php-file. The file should be placed somewhere in your theme-folder.

require_once('wp_bootstrap_navwalker.php');

Then you need to update your wp_nav_menu() in your theme.
    wp_nav_menu( array(
        'menu'              => 'primary',
        'theme_location'    => 'primary',
        'depth'             => 2,
        'container'         => 'div',
        'container_class'   => 'collapse navbar-collapse navbar-ex1-collapse',
        'menu_class'        => 'nav navbar-nav',
        'fallback_cb'       => 'wp_bootstrap_navwalker::fallback',
        'walker'            => new wp_bootstrap_navwalker())
    );

"primary" is the name of your menu that you have probably already defined somewhere in your functions.php-file but if you don't have done that this is how you could do it:

register_nav_menus( array(    'primary' => __( 'Primary menu', 'yourthemename' ),) );

That's it. But you may need to put some surrounding code to get the appearance you look for. It could be something like this in the source of your theme-file:

<div class="navbar navbar-default navbar-fixed-top" role="navigation"><nav>    <div class="container">        <div class="navbar-header">            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">                <span class="sr-only">Toggle navigation</span>                <span class="icon-bar"></span>                <span class="icon-bar"></span>                <span class="icon-bar"></span>            </button>            <a class="navbar-brand" href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?></a>        </div>
        <?php            wp_nav_menu( array(                'menu'              => 'primary',                'theme_location'    => 'primary',                'depth'             => 2,                'container'         => 'div',                'container_class'   => 'collapse navbar-collapse navbar-ex1-collapse',                'menu_class'        => 'nav navbar-nav',                'fallback_cb'       => 'wp_bootstrap_navwalker::fallback',                'walker'            => new wp_bootstrap_navwalker())            );        ?>    </div></nav></div>

Heads up! This menu does only support 2-level of depths. Check out the links above to learn more about the navbar.

Monday, November 11, 2013

Custom image sizes missing in the media up-loader in Wordpress administration

Does your Wordpress media uploader in the administration miss the custom images sizes that you have configured? Don't worry.

In Wordpress 3.5 something seemed to have changed and any custom image sizes weren't available to select when inserting an image to a post or page. I really don't know what have changed but I know the solution and it's quite simple.

The thing you need to do is to create an filter in your functions.php that modify the Wordpress built-in function "image_size_names_choose()" and add all your custom image size to the "list".

Add this following code to your functions.php-file:

function my_custom_sizes( $sizes ) {    return array_merge( $sizes, array(        'custom-300' => __('Custom 300')    ) );}add_filter( 'image_size_names_choose', 'my_custom_sizes' );
That's it!

And if your don't remember how to add a custom image size in Wordpress. It's simple. Add this code to your functions.php-file:

add_image_size( 'custom-300', 300, 300 );

Wednesday, November 6, 2013

Change or modify elements in the content in Wordpress

Have you ever wanted to change the output of a Wordpress page or a post? Maybe modify the H1-elements in the content or something else. The solution is quite simple to achieve this.

Lets say we wanted to add something, in this case a header-element before the H1-element and a closing one right after. This is how you do it:

Put this code below in your functions.php-file:
function filter_h1_pages( $content ) {
$content = str_ireplace( '<h1>', '<header><h1>', $content ); return str_ireplace( '</h1>', '</header></h1>', $content );}add_filter( 'the_content', 'filter_h1_pages' );

What the code does is replacing a string with something else, twice and then return it. So we have filtered the main function the_content in Wordpress.

Friday, October 4, 2013

Woocommerce - Removing or changing fields in edit shipping or billing addresses.

Have you had problems with alter/remove/change or add fields for the edit shipping or edit billing addresses? Doing a search on this topic seems like I wasn't the only one who had the problem. But I've the solution for how to do this.

The first problem I had was to to know what function that controlled this fields. But pretty soon I found out that they where in fact described in the woocommerce docs.
The shipping fields exists in this function : woocommerce_shipping_fields and the billing fields exists in this function: woocommerce_billing_fields.

So the method to change these fields are then fairly easy to do. This is an example code in the functions.php-file on how to remove an unnecessary field "shipping state" and to set an label to the "shipping address 2":

add_filter( 'woocommerce_shipping_fields' , 'custom_override_shipping_fields' );
function custom_override_shipping_fields( $fields ) {

unset($fields['shipping_state']);
$fields['shipping_address_2']['label'] = 'Address Field 2';

     return $fields;
}

The same solution goes for billing fields. 
If you want to alter any fields on the checkout page you could use this method also. The function for those fields lays in this function "woocommerce_checkout_fields". An heads up: the array $fields is one step deeper and therefore if you want to remove the shipping_state like in the example above then you would need to to it like this: "unset($fields['shipping']['shipping_state']);" - notice the extra bracket. 

If you would like to know which fields exist for e.g. the shipping fields function, then use the php function var_dump(). IE: Put "var_dump($fields)" inside the custom override function and it will show you how the array looks like and all the settings for all fields.


Tuesday, January 15, 2013

Config logrotate for apache log-files in "www"-directory or in other non-standard

If you have configured Apache to split error and access logs for each virtual server or configured website. Then logrotate wont rotate those log-files in their custom locations with standard configs. Logrotate comes normally installed with your distribution and is configured to rotate the apache log files that is located in /var/logs/apache2.

But if you have several websites running on your VPS or server, it's linkley you have a seperate log file for each website that you have configured and the logging wont be in the standard location.

As an example. I've several websites running under a structure of "/var/www/$NAMEOFTHEWEBSITE". Now each website has separate logging, in a structure like this  "/var/www/$NAMEOFTHEWEBSITE/logs". In each logs-directory I've a access.log, error.log and a php-error.log. If I want to rotate these with logrotate I need to configure logrotate for that.

This is done by creating another config file for my custom directories.

vim /etc/logrotate.d/www

Then with this configuration:
/var/www/*/logs/*.log { 
weekly
missingok
rotate 26
compress
delaycompress
dateext
notifempty
create 640 root adm
sharedscripts
postrotate
if [ -f "`. /etc/apache2/envvars ; echo ${APACHE_PID_FILE:-/var/run/apache2.pid}`" ];

then /etc/init.d/apache2 reload > /dev/null fi 
endscript
}

My configuration does this:
  • For each and any .log-file in /var/www/*/logs/
  • Do weekly rotation. (weekly)
  • If  log-file is missing, ignore without error message (missingok)
  • Rotate for 26 weeks (rotate 26)
  • Compress all log-files (compress)
  • Delay compression with one time (delaycompress)
  • Name the rotated files with a date-based name so it's easier for me to know where to look (dateext)
  • Do not rotate if log-file is empty (notifempty)
  • Owner and permission settings for rotated file (create 640 root adm)
  • Normally, prescript and postscript scripts are run for each log which is rotated, meaning that a single script may be run multiple times for log file entries which match multiple files (such as the /var/log/news/* example). If sharedscript is specified, the scripts are only run once, no matter how many logs match the wildcarded pattern. However, if none of the logs in the pattern require rotating, the scripts will not be run at all. This option overrides the nosharedscripts option and implies create option. (sharedscripts)
  • The lines between postrotate and endscript (both of which must appear on lines by themselves) are executed after the log file is rotated. These directives may only appear inside of a log file definition. (postrotate & endscript)

That's it. I hope it helps you out configuring your vps or server with the very useful logrotate.