I would like to let Wordpress calculate the aspect ratio & orientation when the media is uploaded instead of doing this in the templates. Then I can call images and ask for image.orientation which will give me 'portrait' or 'landscape' as a value.
Example, how it would look like after uploading an image:
Is this possible or do I have to do this in the frontend?
You can write some code for it. Use http://codex.wordpress.org/Function_Reference/wp_get_attachment_metadata to get the width and the height, and then if $width/$height > 1 => landscape, otherwise its portrait. Store this value in the attachment using: http://codex.wordpress.org/Function_Reference/wp_update_attachment_metadata.
You can use an action for this, such as:
add_action('add_attachment', 'process_images');
Adding the solution as well, following from your code attempt (I HAVENT TESTED THIS):
function attachment_orientation_meta($id){
$attachmentInfo = wp_get_attachment_metadata($id);
$attachmentOrientation = ( $attachmentInfo['height'] > $attachmentInfo['width'] ) ? 'portrait' : 'landscape';
update_post_meta($id, 'orientation', $attachmentOrientation);
}
add_action( 'add_attachment', 'attachment_orientation_meta' );
Related
I am using https://wordpress.org/plugins/if-menu/ to show/hide certain menu items in menu bar list only if device is mobile.
The problem is it recognizes even tablets as mobiles.
To tackle this situation, I have to create a new rule which I am unable to do.
An example of creating new custom visibility rule:
add_filter( 'if_menu_conditions', 'wpb_new_menu_conditions' );
function wpb_new_menu_conditions( $conditions ) {
$conditions[] = array(
'name' => 'If it is Custom Post Type archive', // name of the condition
'condition' => function($item) { // callback - must return TRUE or FALSE
return is_post_type_archive();
}
);
return $conditions;
}
This code needs to be added in function.php of child theme.
Result of the above code: here
I want to edit the above code for this condition:
window width <= 480 or window height <= 480
(i.e. if device's window width or height is less or equal to 480)
Can someone help me with the code? It would be very helpful.
[NOTE: Using #media is the easiest solution, still I want to add the above mentioned condition in the If Menu Plugin.]
Thank You.
How can I hide content in a post or page after it has received a certain amount of views I have set using a shortcode in WordPress?
Let's say I make a post. I enclose some content in the shortcode. I set the content to be shown for just 500 views. Then once the post reaches 500 views, the content should disappear from the post or page.
I have tried so many other plugins but couldn't find any solutions to this. wp-limit-post-views plugin also didn't solve my problem. I need help on this.
You could try something like that:
function hide_contents_function($atts, $content) {
$attributes = shortcode_atts(
array(
'count' => 500
),
$atts
);
// Get the max counts for the current post from the DB.
// You could use either an options if the counter is global, or the post meta.
// For this example I am using options, but it's up to you the implementation
$total_count = get_option('total_count', 0);
// Alternative way using post meta to get the counter per page/post
$total_count = get_post_meta(get_the_ID(), 'post_view_count', true);
if ( ! $total_count ) {
$total_count = 0;
}
// If the count loaded from the DB is bigger than the count
// property value then return nothing.
if ( $total_count > (int)$attributes['count'] ) {
return '';
}
return do_shortcode($content);
}
add_shortcode('hide_contents', 'hide_contents_function');
The above code, will register a short code that accepts an attribute allowing you to control how many views you want to have before you hide the contents.
In this example I used a single value from options table, but you are free to use any method you like to count the total views of a single post.
To use this short code you can do something like that:
[hide_contents count="345"]I will be hidden after 345 views.[/hide_contents]
Note that if you have installed any cache system, your content will not be hidden if the page is cached! That's not a problem of the short code, but the problem will occur because of the cache.
Finally, remember to update the counter of the views on each post refresh :)
I'm coding a shortcode that accept one media ID as an attribute. This shortcode should then display the image at an existing size registered with add_image_size. Problem : my image is not displayed at the correct size.
Explanations :
My media image has been uploaded in the WP library. The original file size is huge (1227x924). The media has the ID 294.
I registered an image size in functions.php :
function my_after_setup_theme(){
add_image_size('my-image-size', 210, 136, true);
}
add_action('after_setup_theme', 'my_after_setup_theme');
I insert my shortcode in one of my pages : [my_shortcode imageid="294"]
My shortcode code is :
function my_shortcode_func($atts)
{
$a = shortcode_atts(array(
'imageid' => 0,
), $atts);
if ($a['imageid'] == 0) {
// default placeholder image
$img = 'img src="http://placehold.it/210x136"/>';
} else {
// get resized (NOT WORKING !)
$img = wp_get_attachment_image($a['imageid'], 'my-image-size');
}
return $img;
}
add_shortcode('my_shortcode', 'my_shortcode_func');
I expect the original image to be resized at the correct size (ie : 210x136) in a new thumbnail file. Instead, the $img displays the original image (1227x924) , displayed at an intermediate size via the width and height attributes of the HTML <img> tag :
What am I doing wrong ?
thanks for help.
Did you register the thumbnail size after uploading the media? If so, you will need to use a plugin to regenerate the thumbnails.
Additionally, I don't believe you need to wrap thumbnail size declarations as you have done, I have the following;
if (function_exists('add_theme_support'))
{
add_theme_support('post-thumbnails'); // Featured image support for posts - you may not need this bit.
add_image_size('large', 700, '', true); // Large Thumbnail - This is the bit you would need
}
So it would be worth checking if your thumbnails are actually be generated to start with, as I have never done it the way you have defined in your question.
In some Wordpress Posts I want to show the featured thumbnail image shown:
fearless_post_thumbnail(); //located in content.php of template
But in some posts I want to hide that thumbnail, In that case I would like to create a custom
field (hide_thumb = 1 )
in posts where I want to hide it:
Question: Which condition should I wrap around:
fearless_post_thumbnail(); // it shows thumbnail.
Thank you
not sure I understand your question, but it sounds like you want to do this:
if ( $hide_thumb != 1 ) {
fearless_post_thumbnail();
}
If your custom field, $hide_thumb, is set to TRUE or 1, then your thumbnail function will not execute.
The other answer here assumed you had already retrieved the value of of your custom field. As per http://codex.wordpress.org/Function_Reference/get_post_custom,
$custom_fields = get_post_custom();
$hide_thumb = $custom_fields['hide_thumb'];
if ( $hide_thumb[0] != 1) {
fearless_post_thumbnail();
}
I'm hoping to get a little insite for using filters in Woocommerce. My main question is, what am I looking for in the template files? or what are the variables that can be targeted using filters? If we look at the filters list I see filter name and files. Using this filter
single_product_small_thumbnail_size
Files - product-thumbnail.php and woocommerce-template.php
What am I looking for in those files that can be targeted and changed? Would you give me a simple example? Maybe something like change thumbnail size?
add_filter('filter_name', 'your_function_name');
function your_function_name( $variable ) {
// Your code
return $variable;
}
I understand what each part of the function and filter are, but I'm not sure what code to write for "Your code." What variable am I grabbing from the file? How do I apply the change? I can't completely wrap my mind around this. Any help would be greatly appreciated.
Thanks,
~MK
As you can see in e.g. woocommerce-template.php you can filter the shop_catalog string:
$small_thumbnail_size = apply_filters( 'single_product_small_thumbnail_size', 'shop_catalog' );
That string is used in the subsequent code to determine the image size to be used:
$image = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );
So, if you would like to use another image size, you can filter the string like e.g.:
add_filter( 'single_product_small_thumbnail_size', 'my_single_product_small_thumbnail_size', 25, 1 );
function my_single_product_small_thumbnail_size( $size ) {
$size = 'large';
return $size;
}