PHP function return. Nested functions - php

I know there's Wordpress StackExchange, but that's more PHP related question.
I'm writing my own shortcode for Wordpress it looks like:
function myShortcode_shortcode() {
return 'something';
}
This shortcode displays simple string "something".
The problem is I want to display an image from template directory:
<img src="<?php bloginfo('template_directory') ?>/images/myImage.jpg" alt="" />
And I don't know how?
When I do:
return '<img src="'. bloginfo('template_directory') .'/images/myImage.jpg" alt="" />';
Script is echoing template directory instead of image.
Any ideas?

The problem is that the bloginfo() function is an output function (intended for templates). You need get_bloginfo() rather.

You probly need to place <img src="<?php bloginfo('template_directory') ?>/images/myImage.jpg" alt="" /> in his own variable like
function shortcode(){
$shortcode = "<img src='". bloginfo('template_directory') ."/images/myImage.jpg' alt="" />"
return $shortcode;
}
Hope this helps

Related

How to change multiple html elements with php (preg_replace?)

I'm using wordpress and I want to change the logo on some pages. Since the theme I'm using (flatsome) doesn't support this I thought using php would be a good idea. I'm not sure how to do it though.
I tried this:
<?php
function change_logo_on_single($html) {
if(is_single( array(1441, 1425, 1501, 1494, 1498, 1503))){
$html = preg_replace('<a(.*?)><img(.*?)><img(.*?)></a>',
'<a href="https://example.com/" title="" rel="home">
<img width="146" height="150" src="https://example.com/wp-content/uploads/2021/02/Logo.png" class="header_logo header-logo" alt="">
<img width="146" height="150" src="https://example.com/wp-content/uploads/2021/02/Logo.png" class="header-logo-dark" alt="">
</a>', $html);
}
return $html;
}
add_filter('get_custom_logo','change_logo_on_single');
?>
I think I used the wrong pattern in preg_replace. Can someone suggest a way to do it?

PHP variable with string

I'm trying to get an image using the following
$html ='<img alt="" src="'.bloginfo('stylesheet_directory').'/images/my-image.png">';
When I return $html the path from bloginfo('stylesheet_directory') shows in the top portion of the page instead in the src attribute of the image.
You should use use get_stylesheet_directory_uri() instead. And you are missing a quote (as CyberJunkie pointed out). Try this:
$img_path = get_stylesheet_directory_uri() . '/images/my-image.png';
$html = '<img alt="" src="' . $img_path . '" />';
There is also a sample in the wordpress codex:
<img src="<?php echo get_stylesheet_directory_uri() ?>/images/aternus.png" alt="" title="" width="" height="" />
bloginfo will automatically echo out the directory you are requesting, and has no return value. The output happens when you are building your string, not whenever you are echoing the $html variable.
I think the function you are looking for is get_stylesheet_directory_uri
Example usage of bloginfo (notice no echo):
<?php bloginfo('name'); ?>
Example usage of get_stylesheet_directory_uri:
<?php echo get_stylesheet_directory_uri(); ?>

Shortcode issues [duplicate]

I have the following text in a page. As you can see my shortcode is right at the bottom but somehow when the code runs, the output of my shortcode is inserted at the top of the page instead of following its preceding content.
<img class="alignnone" title="title_enquires" src="http://localhost/barcelona/wp-content/uploads/2011/08/title_enquires.jpg" alt="" width="589" height="77" />
<img class="alignnone" title="contact_map" src="http://localhost/barcelona/wp-content/uploads/2011/08/contact_map.jpg" alt="" width="555" height="222" />
[barcelona_address]
Here is my short code registration inside the function.php file:
<?php
add_shortcode( 'barcelona_address', 'barcelona_shortcode_handler' );
function barcelona_address_func()
{
print "<p>sdsdsds</p>";
}
function barcelona_shortcode_handler( $atts, $content=null, $code="" )
{
if (function_exists($code . "_func"))
{
call_user_func($code . "_func", $atts);
}
}
?>
And the result is:
<p>sdsdsds</p>
<img class="alignnone" title="title_enquires" src="http://localhost/barcelona/wp-content/uploads/2011/08/title_enquires.jpg" alt="" width="589" height="77" />
<img class="alignnone" title="contact_map" src="http://localhost/barcelona/wp-content/uploads/2011/08/contact_map.jpg" alt="" width="555" height="222" />
Your shortcode handler is supposed to return the output to display in place of the shortcode, not output anything itself.
http://codex.wordpress.org/Shortcode_API
You can use as alternative:
ob_start();
...your code...
return ob_get_clean();
and use without return the html as string.

Wordpress advanced excerpt - use image in 'read more' link, using get bloginfo

I'm trying to output a custom image in my excerpt 'read more' links.
Like so...
I am using the advanced exceprt plugin because it so much customizable for my needs.
This is how I've tried to output the excerpt with the image in the readmore link...
<?php the_advanced_excerpt('length=120&use_words=0&no_custom=0&ellipsis=%26hellip;&read_more=read more <img src"' . get_bloginfo('template_url') . '"/images/readmore-arrow.png" alt="" />&add_link=1'); ?>
But weirdly it outputs this readmore link...
I seems to break up the URL of the image location and output it like this...
<img alt="" readmore-arrow.png"="" images="" my-theme-name"="" themes="" wp-content="" wp="" mywebsitename.co.uk="" src"http:="">
Can any one help me fix this?
Thanks
Working code...
<?php the_advanced_excerpt('length=120&use_words=0&no_custom=0&ellipsis=%26hellip;&rea‌​d_more=read more <img src="' . get_bloginfo('template_url') . '/images/readmore-arrow.png" alt="" />&add_link=1'); ?>
I'm not sure if this will fix it for you as i've not used that plugin, but you're missing an = for the src attribute. I've added it in here:
<?php the_advanced_excerpt('length=120&use_words=0&no_custom=0&ellipsis=%26hellip;&read_more=read more <img src="' . get_bloginfo('template_url') . '"/images/readmore-arrow.png" alt="" />&add_link=1'); ?>

Why does my shortcode get executed before other content?

I have the following text in a page. As you can see my shortcode is right at the bottom but somehow when the code runs, the output of my shortcode is inserted at the top of the page instead of following its preceding content.
<img class="alignnone" title="title_enquires" src="http://localhost/barcelona/wp-content/uploads/2011/08/title_enquires.jpg" alt="" width="589" height="77" />
<img class="alignnone" title="contact_map" src="http://localhost/barcelona/wp-content/uploads/2011/08/contact_map.jpg" alt="" width="555" height="222" />
[barcelona_address]
Here is my short code registration inside the function.php file:
<?php
add_shortcode( 'barcelona_address', 'barcelona_shortcode_handler' );
function barcelona_address_func()
{
print "<p>sdsdsds</p>";
}
function barcelona_shortcode_handler( $atts, $content=null, $code="" )
{
if (function_exists($code . "_func"))
{
call_user_func($code . "_func", $atts);
}
}
?>
And the result is:
<p>sdsdsds</p>
<img class="alignnone" title="title_enquires" src="http://localhost/barcelona/wp-content/uploads/2011/08/title_enquires.jpg" alt="" width="589" height="77" />
<img class="alignnone" title="contact_map" src="http://localhost/barcelona/wp-content/uploads/2011/08/contact_map.jpg" alt="" width="555" height="222" />
Your shortcode handler is supposed to return the output to display in place of the shortcode, not output anything itself.
http://codex.wordpress.org/Shortcode_API
You can use as alternative:
ob_start();
...your code...
return ob_get_clean();
and use without return the html as string.

Categories