Wordpress - Image upload custom field? - wordpress

Does anybody know if there is a way of having an image upload field on a post so I can upload a specific image for that post.
Thanks

It's not currently possible (have been looking after it for months with no luck).
Are you trying to display that image as a thumbnail (while keeping it easy for the authors)?
The nicest approach is auto-detecting the first image in the post. Users don't need to deal with custom fields nor URLs:
// Get URL of first image in a post
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
// no image found display default image instead
if(empty($first_img)){
$first_img = "/images/default.jpg";
}
return $first_img;
}
// With TimThumb
<img src="/thumb.php?src=<?php echo catch_that_image() ?>&w=200&zc=1&q=200" alt="<?php the_title(); ?>"/>
Found it here: http://snipplr.com/view/23968/wordpress--get-first-image-of-post-without-custom-fields/
If that's not what you were looking for please give more details and we'll try to find a solution.
:)

The solution I used was the Flutter module, which let me create custom content types.

It's not an ideal solution but I'm currently just using a custom field called "thumbnail" and putting the location of an uploaded image in there. Then, when I display the post details somewhere I use the value from the custom field as the <img src=""> value.
To upload the image in the first place I just use the normal image upload tool in the editor and copy the uploaded name into the custom field as I'm writing the post.
Not very elegant but it works.

Related

Set default image for woocommerce products

In my project I have many products whose image is not available till now. So I want to show custom image for those products who don't have image.
Is there a way to set default image for products (in case if product don't have image).
I solved it by adding following code in functions.php file
add_action( 'init', 'custom_fix_thumbnail' );
function custom_fix_thumbnail() {
add_filter('woocommerce_placeholder_img_src', 'custom_woocommerce_placeholder_img_src');
function custom_woocommerce_placeholder_img_src( $src ) {
$upload_dir = wp_upload_dir();
$uploads = untrailingslashit( $upload_dir['baseurl'] );
$src = $uploads . '/2012/07/thumb1.jpg';
return $src;
}
}
NOTE : Do not type complete url of image. Instead of complete url, use only short url as shown in code.
Original Post
TLDR;
You need to upload an image via default Wordpress Media uploader, then copy its URL (or just ID) and paste it to "WooCommerce->Settings->Products->Placeholder image"
Found an answer here: https://www.iqcomputing.com/support/articles/changing-the-woocommerce-default-image/

Force specific thumbnail size using wp_get_attachment_image

I've seen all kind of solutions to this issue but none of them resolved my simple problem.
As you can see on the website I'm working on the thumbnails to the right are not equal.
I need to set them to be 214x121px as the thumbnails.
I've tried using plugins such as Regenerate thumbnails (after adding new image size to functions.php) and Custom image sizes, with no success.
I think it's obvious but <?php echo wp_get_attachment_image( $attachment->ID, array(214, 121), false );?> since as I understood Wordpress will bring the closest, considering proportions, image to the given size.
My Wordpress version is 3.6.
Any ideas/assistance will be appreciated.
I generally use wp_get_attachment_image_src for doing this, and here's how I do it:
If you've added your custom image size like this:
add_image_size( 'blog_featured_image', 214, 121, false); //Featured Image for Blog
Then in your template, you could call the custom image size like this:
<?php $imageID = get_field('featured_resource_image');
$imageURL = wp_get_attachment_image_src( $imageID, 'blog_featured_image' ); ?>
<img src="<?php echo $imageURL[0]; ?>" />
'featured_resource_image' is a custom field I create using Advanced Custom Fields plugin, but the get_field for it just returns the ID of the image selected. So I assign the image's ID to $imageID. I then get the image's object using wp_get_attachment_image_src, where I used the custom image size of 'blog_featured_image'. This returns an array with all the image's data. The first record in the array is the URL of the image, so I echo out $imageURL[0] to return the URL.
There's probably other ways to do it, possibly much better ways, but this way has always worked for me.
EDIT:
From looking at the function you are referencing, this should work:
Define your custom image size as outlined above. Then use this:
<?php wp_get_attachment_image( $attachment_id, 'blog_featured_image' ); ?>
Just set $attachment_id to equal your image's ID however you are currently grabbing the ID.

How to use WP oembed script outside the_content

I've a custom post type "video" and wanted to show videos like youtube, dailymotion in a specified area using WP default oembed script.
So i'm using a custom field"video url", but the problem is that oembed work in the_content not with custom field. so how can i do this. or any other solution
If the custom field only contains the video URL like http://www.youtube.com/watch?v=dQw4w9WgXcQ, then you can get the oEmbed HTML code with wp_oembed_get:
$videourl = 'http://www.youtube.com/watch?v=dQw4w9WgXcQ';
$htmlcode = wp_oembed_get($videourl);
echo $htmlcode;
If your custom field contains more than just the URL, you could use the the_content filter to do the same thing the the_content-function does:
$content = "<h2>this video is great</h2>\n<p>check it out</p>\n"
. "[embed]http://www.youtube.com/watch?v=dQw4w9WgXcQ[/embed]";
$htmlcode = apply_filters('the_content', $content);
echo $htmlcode;
Here is a complete answer to your question. It is also a cleaner and quicker method, using wp_oembed_get, rather than shortcode. Of course, change video_url to the name of your custom field.
This code checks that the video_url field is not empty, then oEmbeds the video.
<?php if (!((get('video_url', TRUE))=='')) {
echo wp_oembed_get( get('video_url', true) );
}?>

Drupal 7 Get image field path

I would like to get the image path of a field. I'm in a node and I need the Url of image in order to put it as a background-image in inline css.
I can't find the solution. Can you help me?
To get just the path of an image from it's URI:
file_create_url($node->field_image['und'][0]['uri']);
More information on file_create_url() can be found here: http://api.drupal.org/api/drupal/includes%21file.inc/function/file_create_url/7
To get the path of an image created using an image style from it's URI use:
image_style_url($style, $node->field_image['und'][0]['uri']);
More information on image_style_url() can be found here: http://api.drupal.org/api/drupal/modules!image!image.module/function/image_style_url/7
I am afraid, none of the solutions above are correct. They don't follow Drupal standards.
// field_video_image is the name of the image field
// using field_get_items() you can get the field values (respecting multilingual setup)
$field_video_image = field_get_items('node', $node, 'field_video_image');
// after you have the values, you can get the image URL (you can use foreach here)
$image_url = file_create_url($field_video_image[0]['uri']);
More details here:
http://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way
I sometime use this:
$node = node_load($nid);
$img_url = $node->field_userimage['und'][0]['uri'];
<img src="<?php print image_style_url($style, $img_url) ?>" />
You also can use the Image URL Formatter module. It allows to change the field format in order to get only the URL:
Bonus: it works with Views as well:
to get image style url:
$field = field_get_items('node', $node, 'field_image');
$image_url = image_style_url('landingpage_slider', $field[0]['uri']);

Wordpress : Add a custom field directly above the title input field (w/o modifiying core files)?

Does anyone know of a way to add an input field (or any type of html for the matter) directly above (or below) the title input field on the post edit page ?
I'm looking of a way to do this without modifying core files (I'm doing this as part of a plug-in which creates a custom post-type).
I'm not aware of any available wp hooks in that area of the edit-form-advanced.php file which could help out. I really hope some has come up with a genius workaround !
Since version 3.5 wordpress introduced new hooks for the add/edit post screen called edit_form_after_title and edit_form_after_editor. So now i think we can easily add new html element after wordpress input title and input content.
just use filter like this on your functions.php
add_action( 'edit_form_after_title', 'my_new_elem_after_title' );
function my_new_elem_after_title() {
echo '<h2>Your new element after title</h2>';
}
add_action( 'edit_form_after_editor', 'my_new_elem_after_editor' );
function my_new_elem_after_editor() {
echo '<h2>Your new element after content</h2>';
}
You're on the right track; pursue the add_action('admin_head') point of entry. What you want can specifically be done with a bit of JavaScript + jQuery (which is built into WP). To display the input field above the title input field, do something like this:
add_action('admin_head', 'my_admin_head_in_posts');
function my_admin_head_in_posts() {
?>
jQuery('#post').before(
'<div id="id_my_field" class="updated below-h2">' +
'<input type="text" name="my_field" value="lol" />' +
'</div>'
);
<?php
}
And you should be seeing something like this:

Resources