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.
Related
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?
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(); ?>
So Wordpress is outputting this
<img src="http://site.com/path/to/image.png"; alt="Text" width="600" height="100" class="alignnone size-full wp-image-168" />
However, I would like it to output this
<img src="http://site.com/path/to/image.png"; alt="Text" width="600" height="100" class="full wp-image-168" />
Removing the align class and removing the size- part.
How can I go about this via a filter in the functions.php file?
So here are a couple filters that may work for what you need.
add_filter( 'post_thumbnail_html', 'remove_image_classes', 10 );
add_filter( 'image_send_to_editor', 'remove_image_classes', 10 );
function remove_image_classes( $html ) {
$html = preg_replace( '/(size-|alignnone)/', "", $html );
return $html;
}
I don't know if I got that preg_replace right. Try it out and see if it modifies your code.
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.
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