if (function_exists('add_image_size')) {
   add_image_size( 'dummy-1', 940, 450, true );
   add_image_size( 'dummy-2', 480, 0 );
}

global $_wp_additional_image_sizes;
foreach ( $_wp_additional_image_sizes as $name => $image_size ){
    update_option( $name."_size_w", $image_size['width'] );
    update_option( $name."_size_h", $image_size['height'] );
    update_option( $name."_crop", $image_size['crop'] );
}

add_filter( 'intermediate_image_sizes', 'regenerate_custom_image_sizes' );
function regenerate_custom_image_sizes( $sizes ){
    global $_wp_additional_image_sizes;
    foreach ( $_wp_additional_image_sizes as $name => $size ){
        $sizes[] = $name;
    }
    return $sizes;
}

WordPress custom image size crop: The snippets Story

If you are using custom image sizes in your WordPress theme you likely know the problem. Cropping, rotating or flipping an image via the WordPress image editor, works just on the original. After editing the image is not available in your custom sizes anymore, all you get in your theme is the edited version in original size.

I thought that there has to be a simple solution to this problem, because it is pretty basic after all. But a quick google search just got me not working code. Finally I found some code on pastebin that pointed me in the right direction.

Copy and paste the above code into your functions.php, adjust the image sizes and even your cropped images should be available in your custom sizes.

$menuParameters = array(
    'echo' => false,
    'items_wrap' => '<nav>%3$s</nav>',
);
echo strip_tags(wp_nav_menu( $menuParameters ), '<a><nav>' );

Result:

<nav>
    <a href="...">...</a>
    <a href="...">...</a>
    <a href="...">...</a>
</nav>

Source: Remove LI Elements From Output of wp_nav_menu

HTML5 wp_nav_menu: The Snippets Story

This is how you build a HTML5-navigation without lists in WordPress. I slightly changed the code I found on CSS-Tricks. Had to post this after todays reading of “Navigation in Lists: To Be or Not To Be” by Chris Coyier.

function walk_up_hierarchy($object,$method) {
    $values = array();
    $class = get_class($object);
    do {
        array_unshift($values,$class::$method());
    } while (($class = get_parent_class($class)) !== false);
    return $values;
}

Get class hierarchy in PHP: The snippets story

Yesterday I tried to call a function for every parent class of an object and get the results in an array. I didn’t found the solution instantly, so I thought that’s something for the snippets section.

The function above takes an instance of a class and a class-methods name. Then it calls the function on the object, storing the result as an array-item. Next it calls the method of the parent class of the object, storing the result in the beginning of the result-array and so on.

Apparently the method has to be defined in all parent classes, if that is not sure you have to take further precautions.

$menu = wp_get_nav_menu_items($menu_id,array(
   'posts_per_page' => -1,
   'meta_key' => '_menu_item_object_id',
   'meta_value' => $post->ID // the currently displayed post
));
var_dump($menu[0]->ID);

Current wp_nav_menu item: The snippets story

I needed to retrieve the current wp_nav_menu item connected to the currently requested page/post in WordPress. I found some solutions going through the hole menu-tree, comparing each items object-id with the current post-id. But, since menu items are post-types, you are able to use all the WP-Query-params with wp_get_nav_menu_items, even a meta-query. The code above selects all menu_items which are connected to the current post, from the menu you specify via $menu_id.

The only drawback is that two menu-items, linked to the same post, would both be returned without you knowing which was actually clicked. However thats the same with all the other solutions I found.

$assoc_arr = array_reduce($arr, function ($result, $item) {
    $result[$item['text']] = $item['id'];
    return $result;
}, array());

 

Recommendation: If you are interested in fast PHP for your WordPress site, check out this great PHP/WP benchmark-post from Kinsta, where you can get great managed WordPress hosting with the best support and performance optimizing technical addons like Redis and ElasticSearch.

The snippets story

I just ran into a problem using array_map on associative arrays, like the following one:

$arr = array(
   array(
      'id' => 22,
      'text' => 'Lorem'
   ),
   array(
      'id' => 25,
      'text' => 'ipsum'
   ),
);

From this array, I wanted to create another, associative array, with the following structure:

$assoc_arr = array(
   'Lorem' => 22,
   'ipsum' => 25
);

My first guess was the array_map function, but I had to realize that there is no way to manipulate the keys of the resulting array. After some googling, I discovered, that it is array_reduce you have to use to simulate array_map on associative arrays.

ps: I know, a simple foreach would have done the trick, but once on the path of functional programming, I didn’t want to leave it that fast 🙂