WordPress simple Image Shortcode - php

I am a newbie, please help me with creating a wordpress image shortcode, as simple as:
[img src=""]
Which shows its thumbnail (thumbnail width=100%), links to OR opens the same source image, when clicked.
I tried searching but could not find in existing plugins, please guide me if any.
Please guide me thoroughly for every copy paste in function.php or anywhere else.

// Add Shortcode
function img_shortcode($atts)
{
// Attributes
$atts = shortcode_atts(
[
'src' => '',
'link_to_img' => '',
], $atts, 'img'
);
$return = '';
if ($atts['link_to_img'] == 'yes')
{
$return = '<a href="' . $atts['src'] . '">
<img src="' . $atts['src'] . '"/>
</a>';
}
else{
$return = '<img src="' . $atts['src'] . '"/>';
}
// Return HTML code
return $return;
}
add_shortcode('img', 'img_shortcode');
Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
USAGE
Without link:: In PHP
echo do_shortcode('[img src="http://example.com/wp-content/uploads/2017/02/hello.jpg" link_to_img="no"]');
Without link:: In Editor
[img src="http://example.com/wp-content/uploads/2017/02/hello.jpg" link_to_img="no"]
With link:: In PHP
echo do_shortcode('[img src="http://example.com/wp-content/uploads/2017/02/hello.jpg" link_to_img="yes"]');
With link:: In Editor
[img src="http://example.com/wp-content/uploads/2017/02/hello.jpg" link_to_img="yes"]
Hope this helps!

The Gallery feature allows wordpress to add one or more image galleries to your posts and pages using a simple Shortcode
[gallery ids="729,732,731,720"]
enter link description here

Related

Woocommerce - Change quick view button text and Eye icon

Im working with Wordpress.
For the last hour Im trying to find a code in php. files considering the "Quick view" button, but with no results.
Does anyone know where I can find it?
Best regards.
Filip
It depends on the eCommerce plugin you are using and also on the theme. If you are using woocommerce with a theme built for woocommerce then you can find these kind of things in "woocomerce" folder located in theme directory. If there is no such directory in your theme then take a look at woocommerce plugin directory.
If you are using this plugin for the quick view
You can try this code:
add_filter('woocommerce_loop_quick_view_button','custom_quick_view');
function custom_quick_view($output)
{
$output = 'My Cutom Output';
return $output;
}
For OceanWP theme please try this code:
add_filter('ocean_woo_quick_view_button_html','custom_quick_view');
function custom_quick_view($output)
{
global $product;
$output = '<a href="#" id="product_id_' . $product->get_id() . '"
class="owp-quick-view" data-product_id="' . $product->get_id() . '"><i
class="icon-eye"></i>' . esc_html__( 'Quick View', 'oceanwp' ) . '</a>'; //Edit the $output as you want
return $output;
}
Place this code in your functions.php
Ok If anyone looking to change the text this how I did it and it works with any Theme:
Just add this a snippet in WordPress so you don't need to play around with function file or even when you update the theme or the plugin it will not change or break the text.
add_filter('woocommerce_loop_quick_view_button','custom_quick_view');
function custom_quick_view($output)
{
global $product;
$output = '<a href="#" id="product_id_' . $product->get_id() . '"
class="quick-view-button button" data-product_id="' . $product->get_id() . '"><i
class="icon-eye"></i>' . esc_html__( 'Buy Now', 'wc_quick_view' ) . '</a>'; //Edit the $output as you want
return $output;
}

oembed_result not running on my Wordpress theme

I implemented the following Wordpress filter in function.php file of my Wordpress theme:
function change_oembed($html, $url, $args){
$video_pattern = "#(\W|^)(youtube)(\W|$)#";
$notification = '<div class="cookieconsent-optout-marketing">Please
accept marketing-cookies
to watch this video.
</div>';
if(preg_match($video_pattern, $url)){
$matches = null;
preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $url, $matches);
$gdpr_youtube = '<iframe data-src="https://www.youtube.com/embed/' .
$matches[1] . '" data-cookieconsent="marketing" frameborder="0"
allowfullscreen></iframe>';
$new_html = '<div class="clearfix"></div>' . $notification . $gdpr_youtube;
} else {
$new_html = $html;
}
return $new_html;
}
add_filter( 'oembed_result', array($this, 'change_oembed'), 999, 3 );
I can see the result of this filter in the post. I only see the youtube.com video URL text. I tried clearing the cache withint wordpress and the browser (using FastCache plugin), I've tried putting the code outside the if-else statement but nothing works.
update: I also tried video_embed_html instead of oembed_result but it still doesn't work.
Update 2: The function is not being called. I just put some question marks just to see if I see them in code, but I don't. So the change_oembed function isn't called at all.
I checked that the regex works, and it does works for that YouTube URL, so it's not the regex issue.

Adding Bootstrap img-responsive class to Wordpress images fails at gallery thumbs

I'm working on a Bootstrap 3 Wordpress theme. To make my images responsive I added Bootstrap's img-responsive class to them with the following code in my functions.php
<?php
function bootstrap_responsive_images( $html ){
$classes = 'img-responsive'; // separated by spaces, e.g. 'img image-link'
// check if there are already classes assigned to the anchor
if ( preg_match('/<img.*? class="/', $html) ) {
$html = preg_replace('/(<img.*? class=".*?)(".*?\/>)/', '$1 ' . $classes . ' $2', $html);
} else {
$html = preg_replace('/(<img.*?)(\/>)/', '$1 class="' . $classes . '" $2', $html);
}
// remove dimensions from images,, does not need it!
$html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
return $html;
}
add_filter( 'the_content','bootstrap_responsive_images',10 );
add_filter( 'post_thumbnail_html', 'bootstrap_responsive_images', 10 );
This works pretty well, all get their class="img-responsive" except the thumbs in galleries. They only hav class="attachment-thumbnail or attachment-medium" depending on te choosen image size.
Any ideas?
Just use the same CSS bootstrap uses for .img-responsive and create a selector for your image galleries and apply that CSS to your new selector

Thumbnail to external link AND Thumbnail to post

Here is what I'd like to accomplish
Goal A: I want to hyperlink each thumbnail in index.php to their post.
Goal B: I want to define a hyperlink for each thumbnail in single.php to an external website.
You may ask why am I using thumbnails for single.php? The reason is because I want this layout:
And so far I understand that there are 3 methods to display images:
Insert image into the editor area along with the text, but the problem is I cannot float the image and text differently because all items within a post are assigned a p tag - am I wrong?
Custom fields should get the job done but it doesn't seem the most efficient way - or am I wrong?
Post Thumbnails should be the easiest way but see my problem below
I have the code to accomplish Goal A and B but they only work separately.
In other words, "Code 1" does not work if "Code 2" is present.
How can I resolve this issue? Or is there a better method accomplish my goal?
Code 1: Link thumbnails to external websites using custom field (single.php)
<?php $name = get_post_meta($post->ID, 'externalurl', true);
if( $name ) { ?>
<?php the_post_thumbnail(); ?>
<?php } else {
the_post_thumbnail();
} ?>
Code 2: Link thumbnails to the post (functions.php)
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
function my_post_image_html( $html, $post_id, $post_image_id ) {
$html = '' . $html . '';
return $html;
}
is_single() function will help you achieve what you need. Try below code in functions.php and remove the additional code from single.php
function my_post_image_html( $html, $post_id, $post_image_id ) {
if ( is_single()) {
$name = get_post_meta($post_id, 'externalurl', true);
if( $name ) {
$html = '' . $html . '';
}
return $html;
}
else
{
$html = '' . $html . '';
return $html;
}
}
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );

How to return a certain html tag in php

Sorry if this is a really stupid question. I am just starting to learn PHP and am probably jumping the gun a bit.
I am writing a very 'simple' wordpress plugin which has a custom post type and takes the content from it and returns it on the homepage with a shortcode. Below is the part of the code that handles the shortcode.
add_shortcode("new-tub", "new_tub_short");
function new_tub_short() {
$post_id = 87;
return '<a class="new-tub" href="' . home_url( '/test' , __FILE__ ) . '">' . get_post_field('post_content', $post_id) . '</a>';
}
So currently it wraps a link around the content of the post. All that is in the post will be an image, however, I would like to make it fool proof so it doesnt include another link and paragraph tag.
My question is, is it possible to search for the img tag within that post and return that only?
Thanks in advance,
Alex
You can do this by using strip_tags. Try this,
add_shortcode("new-tub", "new_tub_short");
function new_tub_short() {
$post_id = 87;
return '<a class="new-tub" href="' . home_url( '/test' , __FILE__ ) . '">' . strip_tags(get_post_field('post_content', $post_id), '<img>') . '</a>';
}
http://php.net/manual/en/function.strip-tags.php

Categories