Change button text from WordPress posts - wordpress

First of all, I have zero knowledge of PHP.
I would like to add a button to each of my blog posts that will have the same text as "Your_Promo". Later I will translate that "Your_Promo" with another text like "Hosting Deal", later could be "Cheap Domain", and so on.
I use the below code in my functions.pho, but it doesn't work.
function custom_wc_translations($translated){
$text = array(
'Your_Promo' => 'Hosting Deal',
);
$translated = str_ireplace( array_keys($text), $text, $translated );
return $translated;
}
add_filter( 'gettext', 'custom_wc_translations', 20 );
Could anybody help?

Add this code in your active theme functions.php file:
function vh_excerpt_read_more($more) {
global $post;
return '... <a class="excerpt-read-more" href="'. get_permalink( $post->ID ) . '" title="'. __( 'Read ', 'domain_name' ) . esc_attr( get_the_title( $post->ID ) ).'">'. __( 'Your_Promo »', 'domain_name' ) .'</a>';
}
add_filter( 'excerpt_more', 'vh_excerpt_read_more' );
Tested & working for me
See the result below:
Thanks in advance.

Related

Show logged in users comments with post title of post

This thread How to display logged in users comments (only) on Wordpress
has gotten me most of the way to where I want to be... However, I am trying to change it slightly so that instead of the author's name it displays the post title and links back to the post they left the comment on.
This is the code I've come up with but it is not working.
if ( is_user_logged_in() ) {
$user_id = get_current_user_id(); $args = array( 'status' => 'approve', 'order' => 'DESC', 'user_id' => $user_id );
$comments = get_comments($args); foreach($comments as $comment) : echo '<p>';
$post_id = $comment->comment_post_ID;
$member_name = get_post( $comment->comment_post_ID );
echo( '' . $member_name . '<br />' . $comment->comment_content);
echo '</p>';
endforeach;
}
Not where I can test this, but you’re grabbing the post into the array $member_name. So to display the post name, replace the output $member_name with $member_name->post_title.
Like:
echo( '' . $member_name->post_title . '<br />' . $comment->comment_content);
Probably could rename the array to something clearer like $post also.

function to replace content display for custom post type

I tried to create function to replace content display for custom post type WordPress Storefront child theme, but the if conditions did not work!
The commented if statements are what I have tried.
//if (is_singular() && (get_post_type() == 'gallery')) {
add_action( 'init', 'customise_storefront' );
//}
function customise_storefront() {
//if(get_post_type( $post->ID ) == 'gallery'){
//if('gallery'== get_post_type()) {
//if (is_singular('gallery')) {
//if (is_single('gallery')) {
//if (is_singular() && (get_post_type() == 'gallery')) {
// Remove the storefromt post content function
remove_action( 'storefront_single_post', 'storefront_post_content', 30 );
remove_action( 'storefront_loop_post', 'storefront_post_content', 30 );
// Add our own custom function
add_action( 'storefront_single_post', 'gallery_post_content', 30 );
add_action( 'storefront_loop_post', 'gallery_post_content', 30 );
//}
}
/* Gallery post content*/
if ( ! function_exists( 'gallery_post_content' ) ) {
function gallery_post_content() {
?>
<div class="entry-content" itemprop="articleBody">
<?php
the_content(
sprintf(
__( 'Continue reading %s', 'storefront' ),
'<span class="screen-reader-text">' . get_the_title() . '</span>'
)
);
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'storefront' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
<?php
You're hooking your action to init. No post data is available at that point, so that's why your if tests fail. Try using a later hook once the post data has loaded e.g.
add_action('the_post','customise_storefront');
You should also include a global $post reference at the start of customise_storefront if you're going to try accessing the $post object there.
Though if you do use the_post hook, it passes the post object by reference anyway, so you could modify to:
function customise_storefront ($post_object) {
if (get_post_type ($post_object->ID) ...
See https://codex.wordpress.org/Plugin_API/Action_Reference/the_post
add_action( 'template_redirect', 'customise_storefront' );
function customise_storefront() {
if (is_singular('gallery')) {
// Remove the storefromt post content function
remove_action( 'storefront_single_post', 'storefront_post_content', 30 );
remove_action( 'storefront_loop_post', 'storefront_post_content', 30 );
// Add our own custom function
add_action( 'storefront_single_post', 'gallery_post_content', 30 );
add_action( 'storefront_loop_post', 'gallery_post_content', 30 );
}
}
/* Gallery post content*/
if ( ! function_exists( 'gallery_post_content' ) ) {
function gallery_post_content() {
?>
<div class="entry-content" itemprop="articleBody">
<?php
the_content(
sprintf(
__( 'Continue reading %s', 'storefront' ),
'<span class="screen-reader-text">' . get_the_title() . '</span>'
)
);
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'storefront' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
<?php
}
}

Display content on single product page in Woocommerce based on category

I'm sooooo close - but I can't get this last little bit.
I am adding the authors name to books we have for sale in woo. The style needs to be different from the book title so I'm using a custom meta field to put it under the title and style it the way I want. I have everything working and where I want it, now I just need for it only show on a particular category instead of on all products.
There seems to me to be two ways to do this.
1 - Only display it for products in a particular category
2 - Only display it if there is content.
I'm currently trying to only display if it's a particular category, but while writing this, its seems a more elegant solution to only display if content exists.
Here's what I have
function cn_add_add_subhead_to_title() {
global $woocommerce, $post;
if ( is_product() && has_term( 'books-media', 'product_cat' ) ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );
This doesn't display anywhere, but when I take out the if statement it shows on all products.
Any idea where I went awry?
Here is the answer in context Helga - this snippet will add a custom meta field to the woocommerce product editing dashboard, save the content and display it under the product title on the single product view.
/* add product subhead custom field to woocommerce product dashboard */
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => '_text_field',
'label' => __( 'MY-LABEL', 'woocommerce' ),
'placeholder' => 'Author',
'desc_tip' => 'true',
'description' => __( 'This text will show below the product title.', 'woocommerce' )
)
);
echo '</div>';
}
/* Save woo custom field */
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_text_field = $_POST['_text_field'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, '_text_field', esc_attr( $woocommerce_text_field ) );
}
/* add author below title on single product */
function cn_add_subhead_to_title() {
global $woocommerce, $post;
$meta = get_post_meta( $post->ID, '_text_field', true );
if( $meta != '' ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_subhead_to_title', 6 );
What if you simply test for the presence of the meta? If you don't save the meta in the other categories it will never display:
function cn_add_add_subhead_to_title() {
global $post;
if ( $meta = get_post_meta( $post->ID, '_text_field', true ) ) {
?>
<div class="cn-subhead">
<?php printf( __( 'by %s', 'textdomain' ), $meta ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );
Helga - Thanks for your interest and help. I had a little time so I decided to try the other route of only displaying the meta if there was content instead of based on category. This may be a more flexible solution anyway, leaving the field open to any sort of subhead they want instead of limiting it only to Authors.
This the code that worked:
/* add author below title on single product */
function cn_add_add_subhead_to_title() {
global $woocommerce, $post;
$meta = get_post_meta( $post->ID, '_text_field', true );
if( $meta != '' ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );

Add order metadata to WooCommerce admin order overview

I am developing a plugin for WooCommerce. I want to override the order details template of admin. i have read about on https://www.skyverge.com/blog/override-woocommerce-template-file-within-a-plugin/ , but still I don't understand how to override the order detail template of admin. following is my code:
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
if ( ! class_exists( 'Test' ) ) {
load_plugin_textdomain( 'test', false, dirname( plugin_basename( __FILE__ ) ) . '/' );
}
}
class Test {
public function __construct() {
add_action( 'init', array( $this, 'include_template_functions' ), 20 );
add_action( 'woocommerce_init', array( $this, 'woocommerce_loaded' ) );
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
do_action( 'woocommerce_admin_order_data_after_order_details', 'hello' );
}
public function hello() {
echo "order detail template has loaded";
}
public function include_template_functions() {
include( 'woocommerce-template.php' );
echo "template has loaded";
}
public function woocommerce_loaded() {
}
public function plugins_loaded() {
}
}
$GLOBALS['wc_acme'] = new Test();
It's not calling the hook associated with woocommerce_admin_order_data_after_order_details.
Can anyone please suggest or share some example of editing the order details template editing via plugin. Please note that I am referring to the order detail template inside the administrator, where administrator can view the detail of any order from the list.
From my tutorial on customizing WooCommerce checkout fields this is how you'd display some extra order meta data in the Order Details metabox:
// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){ ?>
<div class="order_data_column">
<h4><?php _e( 'Extra Details' ); ?></h4>
<?php
echo '<p><strong>' . __( 'Some field' ) . ':</strong>' . get_post_meta( $order->id, '_some_field', true ) . '</p>';
echo '<p><strong>' . __( 'Another field' ) . ':</strong>' . get_post_meta( $order->id, '_another_field', true ) . '</p>'; ?>
</div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );
This assumes that you have collected the data on checkout and saved the data as post meta for the $order in question.
You could use woocommerce_admin_order_data_after_billing_address action:
function order_phone_backend($order){
echo "<p><strong>Billing phone 2:</strong> " . get_post_meta( $order->id, '_billing_phone_new', true ) . "</p><br>";
}
add_action( 'woocommerce_admin_order_data_after_billing_address','order_phone_backend', 10, 1 );

Only displaying a posts selected taxonomy terms?

When using wp_get_post_terms() I can produce a list of taxonomy terms associated with a post. However, I only want to show the taxonomy terms that have been selected for that post. Using the aforementioned function and get_terms() will successfully find the taxonomy terms, but it will show all of the terms. Not only the ones that have been selected. In the $args array for the functions I've looked for a 'selected' filter, but I found none and when I tried it, it didn't work.
Am I trying to do something that can't be done? I'm sure it's something that is starring me right in the face. I just want to ask the pro's before I make major changes to the way I'm doing things.
wp_get_post_terms only returns terms that have been selected for that post, it doesn't return all taxonomy terms.
http://codex.wordpress.org/Function_Reference/wp_get_post_terms
<?php
$the_selected = $_GET['cat'];
$args = array( 'post_type' => 'portfolio_item', 'posts_per_page' => 11, 'orderby' => 'id', 'order' => 'DESC', 'themes_categories' => "$the_selected");
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
This works well for me. I simply send the taxonomy slug to browser, and itterate through them with the code above.
I send by this:
<li>Filter By:</li>
<?php
$categories=get_categories($args);
foreach($categories as $category) {
echo '<li><a href="' . get_category_link( $category->term_id ) . '?cat=' . $category->slug.'" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </li> ';
}
?>
You can try out this code, it worked for me. I have a taxonomy named 'stores' and i wanted to display 2 selected taxonomy from it. so i used a include feature.
<?php
$taxonomy = 'stores';
$args1=array(
'include'=> array(12,30)
);
$terms = get_terms('stores',$args1 );
echo '<ul>';
foreach ($terms as $term) {
//Always check if it's an error before continuing. get_term_link() can be finicky sometimes
$term_link = get_term_link( $term, 'stores' );
if( is_wp_error( $term_link ) )
continue;
//We successfully got a link. Print it out.
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
?>
<?php echo get_the_term_list( $post->ID, 'your_taxonamy'); ?>
And if you want it without the term linking you can use this
<?php $terms_as_text = get_the_term_list( $post->ID,'your_taxonamy'); if (!empty($terms_as_text)) echo '', strip_tags($terms_as_text) ,''; ?>

Resources