WordPress – How to use shortcode in PHP files - php

I set up WordPress with the WooCommerce Plugin and the products get looped out...all good till there.
I wanted to add the possibility that visitors are able to upvote a product. So I was looking for a plugin and found one.
But that's the actual problem! The plugin called "Like-Photo" offers me the WordPress shortcode function. If I insert the shortcode in the WordPress editor (before and after the image) all is working fine.
But I need to put that code in the PHP file (the one that loops out the products) itself.
So I tried using the PHP echo function for the shortcode as you can see below. It's not working at all. When I open up the inspector tools in my browser I only see the second shortcode part rendered out in text and it's supposed to create a div (what it does, when I paste in the shortcodes inside the WordPress post editor).
How can I fix this?
<?php echo do_shortcode('[add_voting]'); ?> <!-- shortcode beginning-->
<?php do_action( 'woocommerce_before_shop_loop_item' ); ?> <!-- The loop for the items-->
<div class="image-box">
<div class="voteup_layer">
<div class="voteup_layer_triangle">
<div class="voteup_layer_triangle-inner"></div>
</div>
<p>CLICK 2 UPVOTE</p>
</div>
<div class="sb_title"><?php the_title(); ?></div>
<div class="arrow-up"></div>
<div id="num-id" class="count-num">20.453</div>
<a href="<?php the_permalink(); ?>">
<?php
/**
* woocommerce_before_shop_loop_item_title hook
*
* #hooked woocommerce_show_product_loop_sale_flash - 10
* #hooked woocommerce_template_loop_product_thumbnail - 10
*/
do_action( 'woocommerce_before_shop_loop_item_title' );
if ( has_post_thumbnail() ) {
//echo get_the_post_thumbnail( get_the_ID(), array(370,370) );
echo get_the_post_thumbnail( get_the_ID(), 'home-small-box' );
}
?>
</a>
</div>
<?php echo do_shortcode('[/add_voting]'); ?> <!-- shortcode end-->
I'm getting this HTML output:
<div class="home_small_box ">
<div class="image-box">
<div class="voteup_layer">
<div class="voteup_layer_triangle">
<div class="voteup_layer_triangle-inner"></div>
</div>
<p>CLICK 2 UPVOTE</p>
</div>
<div class="sb_title">FOSSIL <br> <span class="thin">Moon Explorer</span></div>
<div class="arrow-up"></div>
<div id="num-id" class="count-num">20.453</div>
<a href="http://online.com/product/fossil-moon-explorer/">
<img width="360" height="360" src="http://online.com/wp-content/uploads/2015/08/shopping-1-360x360.jpg" class="attachment-home-small-box wp-post-image" alt="shopping-1">
</a>
</div>
"[/add_voting]"
And want (this is how the HTML gets rendered once I add the shortcode inside the WordPress editor – it creates a div called "like-photo-wrapper" around the image where I placed the shortcode and adds the ability to vote):
<div class="like-photo-wrapper">
<a href="http://online.com/wp...2.jpg">
<img src="http://online.com/wp...300.jpg" alt="shopping-2" >
</a>
<div class="votes"><span class="currentVotes">Votes: 0</span>
vote on this image
</div>
</div>
The shortcode isn't working properly in my PHP code.

Check out the documentation for do_shortcode.
The gist of it is that the call to do_shortcode for a shortcode that wraps content should be like this
// In case there is opening and closing shortcode.
echo do_shortcode( '[iscorrect]' . $text_to_be_wrapped_in_shortcode . '[/iscorrect]' );
You can try something like this using output buffering to capture the output and pass it into your shortcode.
ob_start();
<?php do_action( 'woocommerce_before_shop_loop_item' ); ?> <!-- The loop for the items-->
<div class="image-box">
<div class="voteup_layer">
<div class="voteup_layer_triangle">
<div class="voteup_layer_triangle-inner"></div>
</div>
<p>CLICK 2 UPVOTE</p>
</div>
<div class="sb_title"><?php the_title(); ?></div>
<div class="arrow-up"></div>
<div id="num-id" class="count-num">20.453</div>
<a href="<?php the_permalink(); ?>">
<?php
/**
* woocommerce_before_shop_loop_item_title hook
*
* #hooked woocommerce_show_product_loop_sale_flash - 10
* #hooked woocommerce_template_loop_product_thumbnail - 10
*/
do_action( 'woocommerce_before_shop_loop_item_title' );
if ( has_post_thumbnail() ) {
//echo get_the_post_thumbnail( get_the_ID(), array(370,370) );
echo get_the_post_thumbnail( get_the_ID(), 'home-small-box' );
}
?>
</a>
</div>
$out = ob_get_clean();
<?php echo do_shortcode('[add_voting] ' . $out . '[/add_voting]'); ?> <!-- shortcode end-->

Related

Product Information on Hover - WooCommerce

Have a fairly default install of WordPress and WooCommerce on a client's site. I am looking to move the default product information on top of the product image and have it show on hover. I have made a fairly basic outline of how it should work below.
A current link to the page in question is: https://captaincutz.com/store/
The basic code base from the backend looks something like this, before CSS
<li <?php function_exists('wc_product_class') ? wc_product_class( $class, $product ) : post_class($class); ?>>
<?php do_action( 'woocommerce_before_shop_loop_item' ); ?>
<div class="item-inner">
<div class="product--thumbnail item--image">
<div class="item--image-holder">
<?php do_action( 'woocommerce_before_shop_loop_item_title' ); ?><div class="item--overlay"></div>
</div>
<div class="product--action">
<?php
do_action('oasis_shop_loop_item_action');
?>
</div>
</div>
<div class="product--info">
<?php
do_action( 'woocommerce_shop_loop_item_title' );
do_action( 'woocommerce_after_shop_loop_item_title' );
?>
<div class="product">
<?php
do_action('oasis_shop_loop_item_action');
?>
</div>
</div>
<?php
do_action( 'woocommerce_after_shop_loop_item' );
?>
</div>
</li>
I've tried some basic code edits, as well as absolute positioning the items, which works; but it does not achieve the desired look

Header section missing on WooCommerce products page

So, in my page.php (and other template pages) I have a hero section with a background image and the title of the page. Then, following that, a section where the content goes (with ) all that is inside the loop.
To add WooCommerce functionality I added the below code, as per the official documentation:
// WooCommerce Integration
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10);
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);
add_action('woocommerce_before_main_content', 'my_theme_wrapper_start', 10);
add_action('woocommerce_after_main_content', 'my_theme_wrapper_end', 10);
function my_theme_wrapper_start() {
echo '<section class="page-content large-padding shop-container">';
echo '<div class="grid-container">';
echo '<div class="grid-x">';
echo '<div class="cell entry-content" id="woocom">';
}
function my_theme_wrapper_end() {
echo '</div>';
echo '</div>';
echo '</div>';
echo '</section>';
}
The problem is that, yes, the products appear on the products page and the header and the footer appear, but the previous section (the hero image and title) don't.
What am I doing wrong?
Edit: Here's my page.php code:
<?php get_header(); ?>
<?php while (have_posts()): the_post(); ?>
<section class="page-hero text-center background" style="background-image:url(<?php $src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), '1600×1000', false ); echo $src[0]; ?>);">
<div class="grid-container vertically-aligned">
<div class="grid-x justify-center">
<div class="cell large-9">
<h1 class="m-0"><?php the_title(); ?></h1>
</div>
</div>
</div>
</section>
<section class="page-content large-padding">
<div class="grid-container">
<div class="grid-x">
<div class="cell entry-content">
<?php the_content(); ?>
</div>
</div>
</div>
</section>
<?php endwhile; ?>
<?php get_footer(); ?>
In any case, I've been able to add that section modifying the archive-product.php template from woocommerce. The issue here is that because it's an archive page it doesn't get the attached image to add on the header but the image of the first listed product... is there a way to get the attached image of the Products page?

apply_filters (how to remove this filter already added) - woocommerce

I need to remove a filter that sets the template of my results, in this case, it uses UL to organize the content. I don't need this.
Case 1) Remove this and my code takes care os the layout
Case 2) Apply another filter to set my own template.
woocommerce/includes/widgets/class-wc-widgets-products.php
line 192
echo apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' );
line 199
echo apply_filters( 'woocommerce_after_widget_product_list', '</ul>');
There is another element that comes from the hook, but this one I couldn't found the place it comes from, my rendered html shows it:
<div class="listaprodutos"><h2 class="widgettitle">Produtos</h2>
<ul class="product_list_widget">
My goal is remove all these two settings (widgettitle and product_list_widget).
I add this lines in my functions.php page, but it didn't work:
// Remove UL format for products_widget
remove_filter( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' );
remove_filter( 'woocommerce_after_widget_product_list', '</ul>' );
front-page.php
<p class="display-4" style="text-align:center">Produtos mais vendidos</p>
<div class="container">
<?php if ( is_active_sidebar( 'listaprodutos' ) ) : ?>
<?php dynamic_sidebar( 'listaprodutos' ); ?>
<?php endif; ?>
</div>
content-widget-product.php
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="card" style="width: 19rem; margin-bottom:3rem; margin-top:3rem;">
<img class="card-img-top img-fluid" src=<?php echo $product->get_image(); ?>
<div class="card-block">
<h4 class="card-title"><?php echo $product->get_name(); ?></h4>
<p class="card-text">
<?php echo $product->get_price_html(); ?>
</p>
<a class="btn btn-quick" href="<?php echo esc_url( $product->get_permalink() ); ?>">Ler o post</a>
</div>
</div>
</div>
</div>

How do I get my entire footer to show up on all page templates? It is currently only showing up on the home page. Using wordpress

I am using a Wordpress template made with Cherry Framework. The complete footer only appears on the home page. I would like it to appear on all page types, which are mostly 'fullwidth page templates'.
When I look at the PHP files for each template, the difference I can see is this:
<?php do_action( 'cherry_after_home_page_content' ); ?>
I can't seem to find where cherry_after_home_page_content is defined. Could this be what I'm looking for? I tried copying that into the fullwidth page template, but it doesn't work. If I'm totally off base, what would be the correct way to accomplish this?
Footer that appears on every page regardless of template type. Here is the screenshot:
Code from fullwidth page template:
<?php
/**
* Template Name: Fullwidth Page
*/
get_header(); ?>
<div class="motopress-wrapper content-holder clearfix">
<div class="container">
<div class="row">
<div class="<?php echo cherry_get_layout_class( 'full_width_content' ); ?>" data-motopress-wrapper-file="page-fullwidth.php" data-motopress-wrapper-type="content">
<div class="row">
<div class="<?php echo cherry_get_layout_class( 'full_width_content' ); ?>" data-motopress-type="static" data-motopress-static-file="static/static-title.php">
<?php get_template_part("static/static-title"); ?>
</div>
</div>
<div id="content" class="row">
<div class="<?php echo cherry_get_layout_class( 'full_width_content' ); ?>" data-motopress-type="loop" data-motopress-loop-file="loop/loop-page.php">
<?php get_template_part("loop/loop-page"); ?>
</div>
</div>
</div>
<?php do_action( 'cherry_after_home_page_content' ); ?>
</div>
</div>
</div>
<?php get_footer(); ?>
Complete footer that only appears on the home page. Below is a screenshot:
Code from homepage template:
<?php
/**
* Template Name: Home Page
*/
get_header(); ?>
<div class="motopress-wrapper content-holder clearfix">
<div class="container">
<div class="row">
<?php do_action( 'cherry_before_home_page_content' ); ?>
<div class="<?php echo apply_filters( 'cherry_home_layout', 'span12' ); ?>" data-motopress-wrapper-file="page-home.php" data-motopress-wrapper-type="content">
<div class="row">
<div class="<?php echo apply_filters( 'cherry_home_layout', 'span12' ); ?>" data-motopress-type="static" data-motopress-static-file="static/static-slider.php">
<?php get_template_part("static/static-slider"); ?>
</div>
</div>
<div class="row">
<div class="<?php echo apply_filters( 'cherry_home_layout', 'span12' ); ?>" data-motopress-type="loop" data-motopress-loop-file="loop/loop-page.php">
<?php get_template_part("loop/loop-page"); ?>
</div>
</div>
</div>
<?php do_action( 'cherry_after_home_page_content' ); ?>
</div>
</div>
</div>
<?php get_footer(); ?>
I figured this out, so just for anyone who comes across this with the same problem.
The "cherry_after_home_page_content" had nothing to do with it. (And actually I noticed afterward that in my pasted code above I accidentally put in the code where I had pasted it in to the fullwidth page template where it does not belong. Ooops!)
Anyway, it is just in the CSS. If you are seeing that the footer widgets only appear on your homepage, you probably have this in your CSS:
.home .footer .footer-widgets { display:block; }
In your child theme, paste in the above code but change .home to the pages where you want it to show up. For me it was .page for all regular pages, .blog and .single-post for the blog.
Hope that helps someone!

Editing Theme to apply Co-Authors Plus

I'm trying to implement the Co-Authors Plus Plugin. It says that you will need to change some PHP in the theme. It outlines the changes on this Link.
I believe I have found where I would need to change it in my theme. In a file called "post-author-info.php".
This is what it looks like:
<?php
/**
* Post Author Info
*
* #package WP Journal
* #subpackage Include
*/
?>
<div id="authorinfo" class="columns alpha omega marT30 marB20">
<?php echo get_avatar( get_the_author_meta('email'), '80' ); ?>
<h5 class="marB10"><?php _e('By', 'contempo'); ?>: <?php the_author(); ?></h5>
<p><?php the_author_meta('description'); ?></p>
<div class="clear"></div>
</div>
Just adding this line <?php if ( function_exists( 'coauthors' ) ) { coauthors(); } else { the_author(); } ?> seems to give me something that I want. Both "Admin" And "Andrew Gable" are displayed.
Here is the screen shot when changes are applied.
I'm unsure on how to get it to link correctly, and how to handle photos and multiple Bio's.
Thanks
You might wanna post this question at https://wordpress.stackexchange.com/
I had a similar issue and resolved it this way:
global $post;
$author_id=$post->post_author;
foreach( get_coauthors() as $coauthor ): ?>
<div class="entry-author co-author">
<?php echo get_avatar( $coauthor->user_email, '96' ); ?>
<h3 class="author vcard"><span class="fn"><?php echo $coauthor->display_name; ?></span></h3>
<p class="author-bio"><?php echo $coauthor->description; ?></p>
<div class="clear"></div>
</div><!-- .entry-author co-author -->
<?php endforeach;
Be sure to use that global $post;and the foreach-loop.
This function will display an author box containing author image/avatar, author name with link to its archive, and the author's bio. You might want to use your own theme's CSS classes though.

Categories