Add Custom Size Image to Media Popup in WordPress

PHP, snippet, Wordpress, , Leave a comment

In WordPres 4.5, my old function to add a custom size to media uploader seems not working anymore. But I found that new way to add custom size image is a lot simpler using image_size_names_choose filter.

Basically when you create your own image sizes using the add_image_size function:

function add_news_thumb() {
    add_image_size( 'news', 800, 300, TRUE );
    add_image_size( 'news-detail', 1500, 600, TRUE );
}
add_action( 'after_setup_theme', 'add_news_thumb' );

you can just add the new sizes to image_size_names_choose:

function added_custom_size( $sizes ) {
  return array_merge( $sizes, array(
	'news' => __( 'News Thumbnail' ), 
	'news-detail' => __( 'News Detail ' ),
  ) );
}
add_filter( 'image_size_names_choose', 'added_custom_size' );

Screen Shot 2016-06-06 at 4.36.51 PM

Get Page ID or URL From a Slug in WordPress

PHP, snippet, WordpressLeave a comment

This is interesting, with years experience working with WordPress, I’ve never dealt with this kind of issue. I searched the codex, and found get_page_by_path is the ideal solution instead of directly create query to database.

[php]get_page_by_path( $page_path, $output, $post_type );[/php]

To get a page ID simply add the slug to the function
[php]
// Returns Object page ID
$page_id = grain_get_url_by_slug(‘slug’);
[/php]

I creates a function to simplify the usage

function get_url_by_slug($slug) {
    $page_url_id = get_page_by_path( $slug );
    $page_url_link = get_permalink($page_url_id);
    return $page_url_link;
}

And use it in real life

<p>
	<a href="<?php echo get_url_by_slug('services'); ?> ">
		What can we do for you?
	</a>
</p>