modify walker_comment to show/hide comment_text - wordpress

I'm trying to show / hide the comment_text under the comment_meta for a custom post type, editing the walker_class of the twenty twenty theme (child).
I succeeded in doing it with javascript but it works only on the first comment!
What's wrong?
Either trying to pass the comment_id like the div_id, it doesn't work at all.
It's about 2days I've spent round here!
<?php
/**
* Custom comment walker for this theme.
*
* #package WordPress
* #subpackage Twenty_Twenty
* #since 1.0.0
*/
if ( ! class_exists( 'TwentyTwenty_Walker_Comment' ) ) {
/**
* CUSTOM COMMENT WALKER
* A custom walker for comments, based on the walker in Twenty Nineteen.
*/
class TwentyTwenty_Walker_Comment extends Walker_Comment {
/**
* Outputs a comment in the HTML5 format.
*
* #see wp_list_comments()
* #see https://developer.wordpress.org/reference/functions/get_comment_author_url/
* #see https://developer.wordpress.org/reference/functions/get_comment_author/
* #see https://developer.wordpress.org/reference/functions/get_avatar/
* #see https://developer.wordpress.org/reference/functions/get_comment_reply_link/
* #see https://developer.wordpress.org/reference/functions/get_edit_comment_link/
*
* #param WP_Comment $comment Comment to display.
* #param int $depth Depth of the current comment.
* #param array $args An array of arguments.
*/
protected function html5_comment( $comment, $depth, $args ) {
$tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
?>
<<?php echo $tag; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static output ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>>
<article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
<footer class="comment-meta">
<div class="comment-author vcard" onclick="showDiv()" >
<?php
$comment_author_url = get_comment_author_url( $comment );
$comment_author = get_comment_author( $comment );
$avatar = get_avatar( $comment, $args['avatar_size'] );
$comme = get_comment_text( $comment );
$id_com = get_comment_id( $comment );
if ( 0 !== $args['avatar_size'] ) {
if ( empty( $comment_author_url ) ) {
echo wp_kses_post( $avatar );
} else {
printf( '<a href="%s" rel="external nofollow" class="url">', $comment_author_url ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped --Escaped in https://developer.wordpress.org/reference/functions/get_comment_author_url/
echo wp_kses_post( $avatar );
}
}
printf(
'<span class="fn">%1$s</span><span class="screen-reader-text says">%2$s</span>',
esc_html( $comment_author ),
__( 'says:', 'twentytwenty' )
);
if ( ! empty( $comment_author_url ) ) {
echo '</a>';
}
?>
</div><!-- .comment-author -->
<div class="comment-metadata">
<a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
<?php
/* Translators: 1 = comment date, 2 = comment time */
$comment_timestamp = sprintf( __( '%1$s at %2$s', 'twentytwenty' ), get_comment_date( '', $comment ), get_comment_time() );
?>
<time datetime="<?php comment_time( 'c' ); ?>" title="<?php echo esc_attr( $comment_timestamp ); ?>">
<?php echo esc_html( $comment_timestamp ); ?>
</time>
</a>
<?php
if ( get_edit_comment_link() ) {
echo ' <span aria-hidden="true">•</span> <a class="comment-edit-link" href="' . esc_url( get_edit_comment_link() ) . '">' . __( 'Edit', 'twentytwenty' ) . '</a>';
}
?>
</div><!-- .comment-metadata -->
</footer><!-- .comment-meta -->
<!--- <div class="comment-content entry-content"> -->
<div class="comment-content entry-content" id="<?php echo $id_com ;?>" >
<?php
echo $comme;
if ( '0' === $comment->comment_approved ) {
?>
<p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'twentytwenty' ); ?></p>
<?php
}
?></div>
<script type='text/javascript'>
function showDiv() {
var id_com = <?php echo $id_com ;?>; //retrieving the id
if (document.getElementById(id_com).style.display == 'none') {
document.getElementById(id_com).style.display = 'block';
} else {
document.getElementById(id_com).style.display = 'none';
}
}
</script>
<!-- </div><!-- .comment-content -->
<?php
$comment_reply_link = get_comment_reply_link(
array_merge(
$args,
array(
'add_below' => 'div-comment',
'depth' => $depth,
'max_depth' => $args['max_depth'],
'before' => '<span class="comment-reply">',
'after' => '</span>',
)
)
);
$by_post_author = twentytwenty_is_comment_by_post_author( $comment );
if ( $comment_reply_link || $by_post_author ) {
?>
<footer class="comment-footer-meta">
<?php
if ( $comment_reply_link ) {
echo $comment_reply_link; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped --Link is escaped in https://developer.wordpress.org/reference/functions/get_comment_reply_link/
}
if ( $by_post_author ) {
echo '<span class="by-post-author">' . __( 'By Post Author', 'twentytwenty' ) . '</span>';
}
?>
</footer>
<?php
;}
?>
</article><!-- .comment-body -->
<?php
}
}
}

I succeed in solving the problem:
the javascript function needed to have a different name for each comment, same for the container to hide/show
That's the code:
<?php
/**
* Custom comment walker for this theme.
*
* #package WordPress
* #subpackage Twenty_Twenty
* #since 1.0.0
*/
if ( ! class_exists( 'TwentyTwenty_Walker_Comment' ) ) {
/**
* CUSTOM COMMENT WALKER
* A custom walker for comments, based on the walker in Twenty Nineteen.
*/
class TwentyTwenty_Walker_Comment extends Walker_Comment {
/**
* Outputs a comment in the HTML5 format.
*
* #see wp_list_comments()
* #see https://developer.wordpress.org/reference/functions/get_comment_author_url/
* #see https://developer.wordpress.org/reference/functions/get_comment_author/
* #see https://developer.wordpress.org/reference/functions/get_avatar/
* #see https://developer.wordpress.org/reference/functions/get_comment_reply_link/
* #see https://developer.wordpress.org/reference/functions/get_edit_comment_link/
*
* #param WP_Comment $comment Comment to display.
* #param int $depth Depth of the current comment.
* #param array $args An array of arguments.
*/
protected function html5_comment( $comment, $depth, $args ) {
$tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
?>
<<?php echo $tag; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static output ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>>
<article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
<footer class="comment-meta">
<div class="comment-author vcard" onclick="showDiv<?php echo get_comment_id(); ?>()" >
<?php
$comment_author_url = get_comment_author_url( $comment );
$comment_author = get_comment_author( $comment );
$avatar = get_avatar( $comment, $args['avatar_size'] );
$comme = get_comment_text( $comment );
$id_com = get_comment_id( $comment );
if ( 0 !== $args['avatar_size'] ) {
if ( empty( $comment_author_url ) ) {
echo wp_kses_post( $avatar );
} else {
printf( '<a href="%s" rel="external nofollow" class="url">', $comment_author_url ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped --Escaped in https://developer.wordpress.org/reference/functions/get_comment_author_url/
echo wp_kses_post( $avatar );
}
}
printf(
'<span class="fn">%1$s</span><span class="screen-reader-text says">%2$s</span>',
esc_html( $comment_author ),
__( 'says:', 'twentytwenty' )
);
if ( ! empty( $comment_author_url ) ) {
echo '</a>';
}
?>
</div><!-- .comment-author -->
<div class="comment-metadata">
<a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
<?php
/* Translators: 1 = comment date, 2 = comment time */
$comment_timestamp = sprintf( __( '%1$s at %2$s', 'twentytwenty' ), get_comment_date( '', $comment ), get_comment_time() );
?>
<time datetime="<?php comment_time( 'c' ); ?>" title="<?php echo esc_attr( $comment_timestamp ); ?>">
<?php echo esc_html( $comment_timestamp ); ?>
</time>
</a>
<?php
if ( get_edit_comment_link() ) {
echo ' <span aria-hidden="true">•</span> <a class="comment-edit-link" href="' . esc_url( get_edit_comment_link() ) . '">' . __( 'Edit', 'twentytwenty' ) . '</a>';
}
?>
</div><!-- .comment-metadata -->
</footer><!-- .comment-meta -->
<div id="<?php echo get_comment_id(); ?>" style=display:none>
<div class="comment-content entry-content">
<?php
echo $comme;
if ( '0' === $comment->comment_approved ) {
?>
<p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'twentytwenty' ); ?></p>
<?php
}
?></div>
<?php
$comment_reply_link = get_comment_reply_link(
array_merge(
$args,
array(
'add_below' => 'div-comment',
'depth' => $depth,
'max_depth' => $args['max_depth'],
'before' => '<span class="comment-reply">',
'after' => '</span>',
)
)
);
$by_post_author = twentytwenty_is_comment_by_post_author( $comment );
if ( $comment_reply_link || $by_post_author ) {
?>
<footer class="comment-footer-meta">
<?php
if ( $comment_reply_link ) {
echo $comment_reply_link; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped --Link is escaped in https://developer.wordpress.org/reference/functions/get_comment_reply_link/
}
if ( $by_post_author ) {
echo '<span class="by-post-author">' . __( 'By Post Author', 'twentytwenty' ) . '</span>';
}
?>
</footer>
<?php
;}
?>
<script type='text/javascript'>
function showDiv<?php echo get_comment_id(); ?>() {
var id_com = <?php echo $id_com ;?>; //retrieving the id
if (document.getElementById(id_com).style.display == 'none') {
document.getElementById(id_com).style.display = 'block';
} else {
document.getElementById(id_com).style.display = 'none';
}
}
</script>
</div><!-- il mio div -->
</article><!-- .comment-body -->
<?php
}
}
}

Related

How to add woocommerce order table to dokan vendor menu?

I want to integrate Woocommerce my-account dashboard into Dokan vendor dashboard navigation.
for this work I add below code to functions.php in child theme.
//add menu to Dokan vendor dashboard
add_filter( 'dokan_query_var_filter', 'dokan_load_document_menu' );
function dokan_load_document_menu( $query_vars ) {
$query_vars['myOrder'] = 'myOrder';
return $query_vars;
}
add_filter( 'dokan_get_dashboard_nav', 'dokan_add_help_menu' );
function dokan_add_help_menu( $urls ) {
$urls['myOrder'] = array(
'title' => __( 'Help', 'dokan'),
'icon' => '<i class="fa fa-user"></i>',
'url' => dokan_get_navigation_url( 'myOrder' ),
'pos' => 51
);
return $urls;
}
add_action( 'dokan_load_custom_template', 'dokan_load_template' );
function dokan_load_template( $query_vars ) {
if ( isset( $query_vars['myOrder'] ) ) {
require_once dirname( __FILE__ ). '/test.php';
}
}
I add orders.php file in child theme and in include this file to test.php file.
test.php code is :
<?php
/**
* Dokan Dashboard Template
*
* Dokan Main Dahsboard template for Fron-end
*
* #since 2.4
*
* #package dokan
*/
?>
<div class="dokan-dashboard-wrap">
<?php
/**
* dokan_dashboard_content_before hook
*
* #hooked get_dashboard_side_navigation
*
* #since 2.4
*/
do_action( 'dokan_dashboard_content_before' );
?>
<div class="dokan-dashboard-content">
<?php
/**
* dokan_dashboard_content_before hook
*
* #hooked show_seller_dashboard_notice
*
* #since 2.4
*/
do_action( 'dokan_help_content_inside_before' );
?>
<article class="help-content-area">
<h1>Add Your Content</h1>
<?php //echo do_shortcode("[woocommerce_orders]")
?>
<?php require_once "orders.php" ?>
</article><!-- .dashboard-content-area -->
<?php
/**
* dokan_dashboard_content_inside_after hook
*
* #since 2.4
*/
do_action( 'dokan_dashboard_content_inside_after' );
?>
</div><!-- .dokan-dashboard-content -->
<?php
/**
* dokan_dashboard_content_after hook
*
* #since 2.4
*/
do_action( 'dokan_dashboard_content_after' );
?>
</div><!-- .dokan-dashboard-wrap -->
and orders.php code is :
<?php
/**
* Orders
*
* Shows orders on the account page.
*
* This template can be overridden by copying it to yourtheme/woocommerce/myaccount/orders.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* #see https://docs.woocommerce.com/document/template-structure/
* #package WooCommerce\Templates
* #version 3.7.0
*/
defined( 'ABSPATH' ) || exit;
do_action( 'woocommerce_before_account_orders', $has_orders ); ?>
<?php if ( $has_orders ) : ?>
<table class="woocommerce-orders-table woocommerce-MyAccount-orders shop_table shop_table_responsive my_account_orders account-orders-table">
<thead>
<tr>
<?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
<th class="woocommerce-orders-table__header woocommerce-orders-table__header-<?php echo esc_attr( $column_id ); ?>"><span class="nobr"><?php echo esc_html( $column_name ); ?></span></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php
foreach ( $customer_orders->orders as $customer_order ) {
$order = wc_get_order( $customer_order ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$item_count = $order->get_item_count() - $order->get_item_count_refunded();
?>
<tr class="woocommerce-orders-table__row woocommerce-orders-table__row--status-<?php echo esc_attr( $order->get_status() ); ?> order">
<?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-<?php echo esc_attr( $column_id ); ?>" data-title="<?php echo esc_attr( $column_name ); ?>">
<?php if ( has_action( 'woocommerce_my_account_my_orders_column_' . $column_id ) ) : ?>
<?php do_action( 'woocommerce_my_account_my_orders_column_' . $column_id, $order ); ?>
<?php elseif ( 'order-number' === $column_id ) : ?>
<a href="<?php echo esc_url( $order->get_view_order_url() ); ?>">
<?php echo esc_html( _x( '#', 'hash before order number', 'woocommerce' ) . $order->get_order_number() ); ?>
</a>
<?php elseif ( 'order-date' === $column_id ) : ?>
<time datetime="<?php echo esc_attr( $order->get_date_created()->date( 'c' ) ); ?>"><?php echo esc_html( wc_format_datetime( $order->get_date_created() ) ); ?></time>
<?php elseif ( 'order-status' === $column_id ) : ?>
<?php echo esc_html( wc_get_order_status_name( $order->get_status() ) ); ?>
<?php elseif ( 'order-total' === $column_id ) : ?>
<?php
/* translators: 1: formatted order total 2: total order items */
echo wp_kses_post( sprintf( _n( '%1$s for %2$s item', '%1$s for %2$s items', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count ) );
?>
<?php elseif ( 'order-actions' === $column_id ) : ?>
<?php
$actions = wc_get_account_orders_actions( $order );
if ( ! empty( $actions ) ) {
foreach ( $actions as $key => $action ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
echo '' . esc_html( $action['name'] ) . '';
}
}
?>
<?php endif; ?>
</td>
<?php endforeach; ?>
</tr>
<?php
}
?>
</tbody>
</table>
<?php do_action( 'woocommerce_before_account_orders_pagination' ); ?>
<?php if ( 1 < $customer_orders->max_num_pages ) : ?>
<div class="woocommerce-pagination woocommerce-pagination--without-numbers woocommerce-Pagination">
<?php if ( 1 !== $current_page ) : ?>
<a class="woocommerce-button woocommerce-button--previous woocommerce-Button woocommerce-Button--previous button" href="<?php echo esc_url( wc_get_endpoint_url( 'orders', $current_page - 1 ) ); ?>"><?php esc_html_e( 'Previous', 'woocommerce' ); ?></a>
<?php endif; ?>
<?php if ( intval( $customer_orders->max_num_pages ) !== $current_page ) : ?>
<a class="woocommerce-button woocommerce-button--next woocommerce-Button woocommerce-Button--next button" href="<?php echo esc_url( wc_get_endpoint_url( 'orders', $current_page + 1 ) ); ?>"><?php esc_html_e( 'Next', 'woocommerce' ); ?></a>
<?php endif; ?>
</div>
<?php endif; ?>
<?php else : ?>
<div class="woocommerce-message woocommerce-message--info woocommerce-Message woocommerce-Message--info woocommerce-info">
<a class="woocommerce-Button button" href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>"><?php esc_html_e( 'Browse products', 'woocommerce' ); ?></a>
<?php esc_html_e( 'No order has been made yet.', 'woocommerce' ); ?>
</div>
<?php endif; ?>
<?php do_action( 'woocommerce_after_account_orders', $has_orders ); ?>
but don't when I watch page just work else in orders.php file.
(I don't change navigation title and the title is 'HELP')
enter image description here
how I can fix this problem?

Woocommerce variable products

I am using woocommerce in my website. I need radio buttons instead of dropdown for variations on product page. For this, I have replaced the variable.php file with the following code :--
<?php
/**
* Variable product add to cart
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.5.0
*
* Modified to use radio buttons instead of dropdowns
* #author 8manos
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! function_exists( 'print_attribute_radio' ) ) {
function print_attribute_radio( $checked_value, $value, $label, $name ) {
global $product;
// This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
$checked = sanitize_title( $checked_value ) === $checked_value ? checked( $checked_value, sanitize_title( $value ), false ) : checked( $checked_value, $value, false );
$input_name = 'attribute_' . esc_attr( $name ) ;
$esc_value = esc_attr( $value );
$id = esc_attr( $name . '_v_' . $value . $product->get_id() ); //added product ID at the end of the name to target single products
$filtered_label = apply_filters( 'woocommerce_variation_option_name', $label );
printf( '<div><input type="radio" name="%1$s" value="%2$s" id="%3$s" %4$s><label for="%3$s">%5$s</label></div>', $input_name, $esc_value, $id, $checked, $filtered_label );
}
}
global $product;
$attribute_keys = array_keys( $attributes );
do_action( 'woocommerce_before_add_to_cart_form' ); ?>
<form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->get_id() ); ?>" data-product_variations="<?php echo htmlspecialchars( wp_json_encode( $available_variations ) ) ?>">
<?php do_action( 'woocommerce_before_variations_form' ); ?>
<?php if ( empty( $available_variations ) && false !== $available_variations ) : ?>
<p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
<?php else : ?>
<table class="variations" cellspacing="0">
<tbody>
<?php foreach ( $attributes as $name => $options ) : ?>
<tr>
<td class="label"><label for="<?php echo sanitize_title( $name ); ?>"><?php echo wc_attribute_label( $name ); ?></label></td>
<?php
$sanitized_name = sanitize_title( $name );
if ( isset( $_REQUEST[ 'attribute_' . $sanitized_name ] ) ) {
$checked_value = $_REQUEST[ 'attribute_' . $sanitized_name ];
} elseif ( isset( $selected_attributes[ $sanitized_name ] ) ) {
$checked_value = $selected_attributes[ $sanitized_name ];
} else {
$checked_value = '';
}
?>
<td class="value">
<?php
if ( ! empty( $options ) ) {
if ( taxonomy_exists( $name ) ) {
// Get terms if this is a taxonomy - ordered. We need the names too.
$terms = wc_get_product_terms( $product->get_id(), $name, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( ! in_array( $term->slug, $options ) ) {
continue;
}
print_attribute_radio( $checked_value, $term->slug, $term->name, $sanitized_name );
}
} else {
foreach ( $options as $option ) {
print_attribute_radio( $checked_value, $option, $option, $sanitized_name );
}
}
}
echo end( $attribute_keys ) === $name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations cstm-reset" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' ) : '';
?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<div class="single_variation_wrap">
<?php
do_action( 'woocommerce_before_single_variation' );
do_action( 'woocommerce_single_variation' );
do_action( 'woocommerce_after_single_variation' );
?>
</div>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
<?php endif; ?>
<?php do_action( 'woocommerce_after_variations_form' ); ?>
</form>
<?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>
It shows the radio buttons as I needed but the problem is that, the price is not displaying on variation products.You can check my website http://www.itsjal.com/store/product/led-lamp-par38/
Price should be displayed at the end when I select variations options. Can anybody help me to solve this problem ?
Thanks
You can try this plugin. https://wordpress.org/plugins/woocommerce-radio-buttons/
This plugin also has its own template files if you want to customize it.

In wordpress, how to create a widget and add into menu bar

I am green in wordpress.
I have create a widget call woocommerce-dropdown-cart.php and placed in the same folder with the theme I am using. But why I cant find this widget in my Appereance-->widget?
On the other hands, my menu bar have not any widget, how can I add this widget into or just before the menu bar?
Many thanks!
Here is my php code:
<?php class Woocommerce_Dropdown_Cart extends WP_Widget {
public function __construct() {
parent::__construct(
'woocommerce-dropdown-cart', // Base ID
'Woocommerce Dropdown Cart', // Name
array( 'description' => __( 'Woocommerce Dropdown Cart', 'jeffho' ), ) // Args
);
}
public function widget( $args, $instance ) {
global $post;
extract( $args );
echo $before_widget;
global $woocommerce;
global $qode_options_proya;
$cart_holder_class = 'header_cart';
if (isset($qode_options_proya['woo_cart_type'])){
$cart_type = $qode_options_proya['woo_cart_type'];
switch ($cart_type) {
case 'font-elegant':
$cart_holder_class = "header_cart cart_icon";
break;
default:
$cart_holder_class = "header_cart";
break;
}
}
?>
<div class="shopping_cart_outer">
<div class="shopping_cart_inner">
<div class="shopping_cart_header">
<a class="<?php echo esc_attr($cart_holder_class);?>" href="<?php echo $woocommerce->cart->get_cart_url(); ?>"><span class="header_cart_span"><?php echo $woocommerce->cart->cart_contents_count; ?></span></a>
<div class="shopping_cart_dropdown">
<div class="shopping_cart_dropdown_inner">
<?php
$cart_is_empty = sizeof( $woocommerce->cart->get_cart() ) <= 0;
$list_class = array( 'cart_list', 'product_list_widget' );
?>
<ul class="<?php echo implode(' ', $list_class); ?>">
<?php if ( !$cart_is_empty ) : ?>
<?php foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) :
$_product = $cart_item['data'];
// Only display if allowed
if ( ! $_product->exists() || $cart_item['quantity'] == 0 ) {
continue;
}
// Get price
$product_price = get_option( 'woocommerce_tax_display_cart' ) == 'excl' ? $_product->get_price_excluding_tax() : $_product->get_price_including_tax();
$product_price = apply_filters( 'woocommerce_cart_item_price_html', woocommerce_price( $product_price ), $cart_item, $cart_item_key );
?>
<li>
<a itemprop="url" href="<?php echo get_permalink( $cart_item['product_id'] ); ?>">
<?php echo $_product->get_image(); ?>
<?php echo apply_filters('woocommerce_widget_cart_product_title', $_product->get_title(), $_product ); ?>
</a>
<?php echo $woocommerce->cart->get_item_data( $cart_item ); ?>
<?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s × %s', $cart_item['quantity'], $product_price ) . '</span>', $cart_item, $cart_item_key ); ?>
</li>
<?php endforeach; ?>
<?php else : ?>
<li><?php _e( 'No products in the cart.', 'woocommerce' ); ?></li>
<?php endif; ?>
</ul>
</div>
<?php if ( sizeof( $woocommerce->cart->get_cart() ) <= 0 ) : ?>
<?php endif; ?>
<a itemprop="url" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" class="qbutton white view-cart"><?php _e( 'Cart', 'woocommerce' ); ?> <i class="fa fa-shopping-cart"></i></a>
<span class="total"><?php _e( 'Total', 'woocommerce' ); ?>:<span><?php echo $woocommerce->cart->get_cart_subtotal(); ?></span></span>
<?php if ( sizeof( $woocommerce->cart->get_cart() ) <= 0 ) : ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php
echo $after_widget;
}
public function update( $new_instance, $old_instance ) {
$instance = array();
return $instance;
}
}
add_action( 'widgets_init', create_function( '', 'register_widget( "Woocommerce_Dropdown_Cart" );' ) );
?>
<?php
add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
?>
<span class="header_cart_span"><?php echo $woocommerce->cart->cart_contents_count; ?></span>
<?php
$fragments['span.header_cart_span'] = ob_get_clean();
return $fragments;
}
?>
I am using theme called "Creativo 5.0" and I have added:
register_sidebar( array(
'name' => __( 'Header Sidebar', 'Creativo' ),
'id' => 'header-sidebar',
'before_widget' => '<div class="header _widget_content">',
'after_widget' => '</div>',
'before_title' => '<h2 class="header-widget-title">',
'after_title' => '</h2>',
) );
into function creativo_widgets_init()
what I want is to add the php widget into the menu bar.

Where to Fork Recent Posts Widget?

I am trying to create a custom recent posts widget by forking the default WordPress widget. I understand you shouldn;t hack core code, but rather fork the original widget and then register it in the functions file.
The only thing is, I don't know where to fork. I've found the default widget and I know that I'd register the widget in my theme's functions.php file, I just don't know where to save the code I'm forking. For example, do I save it as a plugin in the plugins folder, etc.?
Here's the default WordPress code:
/**
* Recent_Posts widget class
*
* #since 2.8.0
*/
class WP_Widget_Recent_Posts extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "Your site’s most recent Posts.") );
parent::__construct('recent-posts', __('Recent Posts'), $widget_ops);
$this->alt_option_name = 'widget_recent_entries';
add_action( 'save_post', array($this, 'flush_widget_cache') );
add_action( 'deleted_post', array($this, 'flush_widget_cache') );
add_action( 'switch_theme', array($this, 'flush_widget_cache') );
}
function widget($args, $instance) {
$cache = array();
if ( ! $this->is_preview() ) {
$cache = wp_cache_get( 'widget_recent_posts', 'widget' );
}
if ( ! is_array( $cache ) ) {
$cache = array();
}
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
if ( isset( $cache[ $args['widget_id'] ] ) ) {
echo $cache[ $args['widget_id'] ];
return;
}
ob_start();
extract($args);
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts' );
/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
if ( ! $number )
$number = 5;
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
/**
* Filter the arguments for the Recent Posts widget.
*
* #since 3.4.0
*
* #see WP_Query::get_posts()
*
* #param array $args An array of arguments used to retrieve the recent posts.
*/
$r = new WP_Query( apply_filters( 'widget_posts_args', array(
'posts_per_page' => $number,
'no_found_rows' => true,
'post_status' => 'publish',
'ignore_sticky_posts' => true
) ) );
if ($r->have_posts()) :
?>
<?php echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<ul>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<li>
<?php get_the_title() ? the_title() : the_ID(); ?>
<?php if ( $show_date ) : ?>
<span class="post-date"><?php echo get_the_date(); ?></span>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php echo $after_widget; ?>
<?php
// Reset the global $the_post as this query will have stomped on it
wp_reset_postdata();
endif;
if ( ! $this->is_preview() ) {
$cache[ $args['widget_id'] ] = ob_get_flush();
wp_cache_set( 'widget_recent_posts', $cache, 'widget' );
} else {
ob_end_flush();
}
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['number'] = (int) $new_instance['number'];
$instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['widget_recent_entries']) )
delete_option('widget_recent_entries');
return $instance;
}
function flush_widget_cache() {
wp_cache_delete('widget_recent_posts', 'widget');
}
function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
$show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<p><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label></p>
<?php
}
}
Create a folder in Plugins and save it in there.
You don't have to create a new folder, but I would advise it just to keep the project clean.
More info here : http://codex.wordpress.org/Writing_a_Plugin

wordpress commenting system

I am trying to use wordpress's commenting system. Their documentation is very confusing and it is kind of a pain to work with. I am trying to use the same commenting system they have on the default twentytwelve theme. It seems to be the way I want it. But even when using the same code they have I do not get a list of previous comment.
This is my comments.php
if ( post_password_required() )
return;
?>
<div id="comments" class="comments-area">
<?php // You can start editing here -- including this comment! ?>
<?php if ( have_comments() ) : ?>
<h2 class="comments-title">
<?php
printf( _n( 'One thought on “%2$s”', '%1$s thoughts on “%2$s”', get_comments_number(), 'dogcompany' ),
number_format_i18n( get_comments_number() ), '<span>' . get_the_title() . '</span>' );
?>
</h2>
<ol class="commentlist">
<?php wp_list_comments( array( 'callback' => 'dogcompany_comment', 'style' => 'ol' ) ); ?>
</ol><!-- .commentlist -->
<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // are there comments to navigate through ?>
<div id="comment-nav-below" class="navigation" role="navigation">
<h1 class="assistive-text section-heading"><?php _e( 'Comment navigation', 'dogcompany' ); ?></h1>
<div class="nav-previous"><?php previous_comments_link( __( '← Older Comments', 'dogcompany' ) ); ?></div>
<div class="nav-next"><?php next_comments_link( __( 'Newer Comments →', 'dogcompany' ) ); ?></div>
</div>
<?php endif; // check for comment navigation ?>
<?php
/* If there are no comments and comments are closed, let's leave a note.
* But we only want the note on posts and pages that had comments in the first place.
*/
if ( ! comments_open() && get_comments_number() ) : ?>
<p class="nocomments"><?php _e( 'Comments are closed.' , 'dogcompany' ); ?></p>
<?php endif; ?>
<?php endif; // have_comments() ?>
<?php comment_form(); ?>
</div><!-- #comments .comments-area -->
This is function.php
<?php
function dogcompany_setup(){
// Adds RSS feed links to <head> for posts and comments.
add_theme_support( 'automatic-feed-links' );
}
add_action( 'after_setup_theme', 'dogcompany_setup' );
function dogcompany_scripts_styles() {
global $wp_styles;
/*
* Adds JavaScript to pages with the comment form to support
* sites with threaded comments (when in use).
*/
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) )
wp_enqueue_script( 'comment-reply' );
$protocol = is_ssl() ? 'https' : 'http';
}
add_action( 'wp_enqueue_scripts', 'dogcompany_scripts_styles' );
/*remove admin bar */
show_admin_bar(false);
if ( ! function_exists( 'dogcompany_comment' ) ) :
/**
* Template for comments and pingbacks.
*
* To override this walker in a child theme without modifying the comments template
* simply create your own dogcompany_comment(), and that function will be used instead.
*
* Used as a callback by wp_list_comments() for displaying the comments.
*
* #since Twenty Twelve 1.0
*/
function dogcompany_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
switch ( $comment->comment_type ) :
case 'pingback' :
case 'trackback' :
// Display trackbacks differently than normal comments.
?>
<li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
<p><?php _e( 'Pingback:', 'dogcompany' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( '(Edit)', 'dogcompany' ), '<span class="edit-link">', '</span>' ); ?></p>
<?php
break;
default :
// Proceed with normal comments.
global $post;
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
<article id="comment-<?php comment_ID(); ?>" class="comment">
<header class="comment-meta comment-author vcard">
<?php
echo get_avatar( $comment, 44 );
printf( '<cite class="fn">%1$s %2$s</cite>',
get_comment_author_link(),
// If current post author is also comment author, make it known visually.
( $comment->user_id === $post->post_author ) ? '<span> ' . __( 'Post author', 'dogcompany' ) . '</span>' : ''
);
printf( '<time datetime="%2$s">%3$s</time>',
esc_url( get_comment_link( $comment->comment_ID ) ),
get_comment_time( 'c' ),
/* translators: 1: date, 2: time */
sprintf( __( '%1$s at %2$s', 'dogcompany' ), get_comment_date(), get_comment_time() )
);
?>
</header><!-- .comment-meta -->
<?php if ( '0' == $comment->comment_approved ) : ?>
<p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'dogcompany' ); ?></p>
<?php endif; ?>
<section class="comment-content comment">
<?php comment_text(); ?>
<?php edit_comment_link( __( 'Edit', 'dogcompany' ), '<p class="edit-link">', '</p>' ); ?>
</section><!-- .comment-content -->
<div class="reply">
<?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( 'Reply', 'dogcompany' ), 'after' => ' <span>↓</span>', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
</div><!-- .reply -->
</article><!-- #comment-## -->
<?php
break;
endswitch; // end comment_type check
}
endif;
if ( ! function_exists( 'dogcompany_entry_meta' ) ) :
/**
* Prints HTML with meta information for current post: categories, tags, permalink, author, and date.
*
* Create your own dogcompany_entry_meta() to override in a child theme.
*
* #since Twenty Twelve 1.0
*/
function dogcompany_entry_meta() {
// Translators: used between list items, there is a space after the comma.
$categories_list = get_the_category_list( __( ', ', 'dogcompany' ) );
// Translators: used between list items, there is a space after the comma.
$tag_list = get_the_tag_list( '', __( ', ', 'dogcompany' ) );
$date = sprintf( '<time class="entry-date" datetime="%3$s">%4$s</time>',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() )
);
$author = sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>',
esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
esc_attr( sprintf( __( 'View all posts by %s', 'dogcompany' ), get_the_author() ) ),
get_the_author()
);
// Translators: 1 is category, 2 is tag, 3 is the date and 4 is the author's name.
if ( $tag_list ) {
$utility_text = __( 'This entry was posted in %1$s and tagged %2$s on %3$s<span class="by-author"> by %4$s</span>.', 'dogcompany' );
} elseif ( $categories_list ) {
$utility_text = __( 'This entry was posted in %1$s on %3$s<span class="by-author"> by %4$s</span>.', 'dogcompany' );
} else {
$utility_text = __( 'This entry was posted on %3$s<span class="by-author"> by %4$s</span>.', 'dogcompany' );
}
printf(
$utility_text,
$categories_list,
$tag_list,
$date,
$author
);
}
endif;
Thank you,
I don't know exactly where you are mistaken. Use new wordpress pack comments.php and functions.php codes. I think it will be solve your problem.

Resources