I have this line I need to replace/edit to fit my needs. This code is extracting first name and last name from wordpress database:
'<strong>' . get_the_author_meta( 'display_name', $freelancer ) . '</strong>',
What I need is to show on website only first name and initial of the last name.
I have used bellow code in other context and working fine but I don't know how to adapt it to above code:
<?php
$first_name = get_the_author_meta( 'first_name', $freelancer );
$last_name = substr( get_the_author_meta( 'last_name', $freelancer ), 0, 1 ) . '.';
$display_name = $first_name . ' ' . $last_name;
echo $display_name;
?>
So my second code is working perfect but I need to replace first code with proper code to get the same result , I don't know how, anyway this is more of context code I need to alter:
$message = sprintf( __( '%s cancelled his project %s', ET_DOMAIN ),
'<strong>' . get_the_author_meta( 'first_name', $freelancer ) . '</strong>',
'<strong>' . get_the_title( $project ) . '</strong>'
);
Related
I am trying to change the way post dates are displayed in my Wordpress child theme. As of now the time shown is the last modified, and I want it to be the date I manually set in each post (i.e. original publication date).
I have found a file in the parent theme that I think might contain the code culprit. I admit I don't quite understand what this code does though, and I've been trying by trial and error to make it work in a different way, even to the point of deleting it entirely, but to no avail. Whatever I do, the output on my page remains the same. There haven't even been any error messages, either. I have a suspicion that the file in question, which I copied to the child theme's folder, isn't somehow properly queued and thus no changes are reflected. The file is called 'template-tags.php' and it resides in a folder called 'inc'. I have created an analoguous folder in my child theme's directory and copied the file there for editing.
if ( ! function_exists( 'writings_posted_on' ) ) :
/**
* Prints HTML with meta information for the current post-date/time and author.
*/
function writings_posted_on() {
$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="updated" datetime="%3$s">%4$s</time><time class="entry-date published" datetime="%1$s">%2$s</time>';
}
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() )
);
$posted_on = sprintf(
/* translators: %s: post date. */
esc_html_x( 'Posted on %s', 'post date', 'writings' ),
'' . $time_string . ''
);
$byline = sprintf(
/* translators: %s: post author. */
esc_html_x( 'by %s', 'post author', 'writings' ),
'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
);
echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>'; // WPCS: XSS OK.
}
I expect the output on the page to be the original publication date, and not last modified date as it is now
You can copy the main function to the child theme and customize as per your requirement. I have modified the function to ignore updated date in the post meta. You can try following. Copy following code in the functions.php of your child theme and paste it.
function writings_posted_on() {
$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() )
);
$posted_on = sprintf(
/* translators: %s: post date. */
esc_html_x( 'Posted on %s', 'post date', 'writings' ),
'' . $time_string . ''
);
$byline = sprintf(
/* translators: %s: post author. */
esc_html_x( 'by %s', 'post author', 'writings' ),
'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
);
echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>'; // WPCS: XSS OK.
}
I'm trying to remove the order date from the woocommerce emails, but as I'm not that confident in PHP I'm finding myself a bit stuck.
Below is some code I've found to a similar question (instead removing the order number but leaving the date...I want to do the opposite.
<?php
// Targetting specific email notificatoins
$email_ids = array('new_order', 'customer_on_hold_order');
$date = sprintf( '<time datetime="%s">%s</time>', $order->get_date_created()->format( 'c' ), wc_format_datetime( $order->get_date_created() ) );
// Displaying order number except for "New Order" and "Customer On Hold Order" notifications
if( ! in_array($email->id, $email_ids) ){
$order_number = sprintf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() );
$date = '('.$date.')';
} else {
$date = __('Order date:', 'woocommerce') . ' ' . $date;
$order_number = '';
}
if ( $sent_to_admin ) {
$before = '<a class="link" href="' . esc_url(
$order->get_edit_order_url() ) . '">';
$after = '</a> ';
} else {
$before = '';
$after = ' ';
}
?>
<h2><?php echo $before . $order_number . $after . $date; ?></h2>
Managed to figure this one out myself...(although not sure it's good practice...but it works!)
Simply added a class to the h2 tag within your-child-theme/woocommerce/emails/email-order-details.php, like below:
<h2 class="fbc-time">
<?php
if ( $sent_to_admin ) {
$before = '<a class="link" href="' . esc_url( $order->get_edit_order_url() ) . '">';
$after = '</a>';
} else {
$before = '';
$after = '';
}
/* translators: %s: Order ID. */
echo wp_kses_post( $before . sprintf( __( 'Order #%s', 'woocommerce' ) . $after . ' <time datetime="%s">%s</time>', $order->get_order_number(), $order->get_date_created()->format( 'c' ), wc_format_datetime( $order->get_date_created() ) ) );
?>
Then selected the class in email-styles.php and used display: none
I'm a designer (not a developer) working on a Wordpress site for a customer. Using the ACF-plugin I've set up a custom field on media files for photo credits. This works fine on featured images, where I can call it in single.php like this:
$post_thumbnail = get_post(get_post_thumbnail_id());
$credit = get_field('media_credit', $post_thumbnail);
if($credit):
echo '<div class="media-credit"><p>Photo: '.$credit.'</p></div>';
endif;
So I know the custom field works, and outputs the right data. However, I can't get it to work on image attachments in posts. What I have is this:
add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 );
function my_img_caption_shortcode( $empty, $attr, $content ){
$attr = shortcode_atts( array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr );
if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {
return '';
}
if ( $attr['id'] ) {
$attr['id'] = 'id="' . esc_attr( $attr['id'] ) . '" ';
}
//OUTPUT CREDIT
$photographer = get_field( 'media_credit', $attachment_id );
if ($photographer):$media_byline = '<br/><span class="media-credit">Photo: '.$photographer.'</span>';endif;
return '<div ' . $attr['id']
. 'class="wp-caption ' . esc_attr( $attr['align'] ) . '" '
. do_shortcode( $content )
. '<p class="wp-caption-text">' . $attr['caption'] . '' . $media_byline . '</p>'
. '</div>';
}
If I remove the if-statement in OUTPUT it shows «Photo: » within the captions, after the text like it should, but it doesn't get any data. What am I missing?
(BTW – I know there are plugins that outputs image credits, but they tend to have styles and features I have to override, resulting in a spaghetti mess I'd hate to hand over to the next guy working on this site.)
Finally got this to work! :-D Instead of using $attachment_id, I got the ID from $attr, and then stripped the 'attachment_' prefix from output.
I also made separate fields for photographer and bureau, but I guess that's beside the point.
Here is the code:
function my_img_caption_shortcode( $empty, $attr, $content ){
$attr = shortcode_atts( array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr );
if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {
return '';
}
$credit_id = $attr['id'];
$credit_id = str_replace( 'attachment_', '', $credit_id );
$photographer = get_field( 'media_credit', $credit_id );
$bureau_credit = get_field( 'media_bureau', $credit_id );
if ( $photographer && $bureau_credit ): $dash = ' / ';
endif;
if ( $photographer || $bureau_credit ): $media_byline = '<br/><span class="media-credit">PHOTO: '
. $photographer . ''
. $dash . '<span class="bureau-credit">'
. $bureau_credit
. '</span></span>';
endif;
return '<div id="attachment_' . $credit_id . '"'
. 'class="wp-caption ' . esc_attr( $attr['align'] ) . '" '
. do_shortcode( $content )
. '<p class="wp-caption-text">' . $attr['caption'] . '' . $media_byline . '</p>'
. '</div>';
}
add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 );
This solution is something I lifted from the AFC Media Credit-plugin, so credits to the developer.
I hope this is useful for anybody who wants to achieve something similar.
On the admin side of Gravity form, I want to add a note if an admin user changes the status of the form entry. I have an admin only field so that bit is working and I know how to add note.
But can't work out, how to add note only if a certain field is changed. I need it to say something like 'status was updated from "approved" to "closed" by xxx'
Any help would be much appreciated.
Thanks.
add_action( 'gform_after_update_entry', function ( $form, $entry_id ) {
$current_user = wp_get_current_user();
$note = 'status updated from' . $status_from . ' to ' . $status_to . ' by ' . $current_user;
RGFormsModel::add_note( $entry_id, $current_user->ID, $current_user->display_name, $not );
}, 10, 2 );
Worked it out. For anyone who may need it. Answer below :)
add_action( 'gform_after_update_entry', 'update_entry', 10, 3 );
function update_entry( $form, $id, $original ) {
$entry = GFAPI::get_entry( $id );
$status_from = $original[ID_OF_THE_FIELD];
$status_to = $entry[ID_OF_THE_FIELD];
if($status_from != $status_to) {
$current_user = wp_get_current_user();
$message = 'Status updated from ' . $status_from . ' to ' . $status_to . ' by ' . $current_user->display_name;
RGFormsModel::add_note( $entry_id, $current_user->ID, $current_user->display_name, $message );
}
}
I want to display for users once logged in instead of the default word My Account I want to display the user's name, I tried this code but it doesnt display anything!
It seems it doesn't recognized the variable $current_userin the file located at: wp-content/themes/themeName/framework/functions/woo-account.php
printf( __( '%s', 'wpdance' ),$current_user->user_lastname);
it was:
printf( __( 'My Account', 'wpdance' ));
And I tried also to get every thing using this code:
<?php global $current_user;
get_currentuserinfo();
echo 'Username: ' . $current_user->user_login . "\n";
echo 'User email: ' . $current_user->user_email . "\n";
echo 'User level: ' . $current_user->user_level . "\n";
echo 'User first name: ' . $current_user->user_firstname . "\n";
echo 'User last name: ' . $current_user->user_lastname . "\n";
echo 'User display name: ' . $current_user->display_name . "\n";
echo 'User ID: ' . $current_user->ID . "\n";
?>
But User first name: and User last name: were empty!
Does someone have any suggestion or idea?
Thank you in advanced!
The best way is to use wp_get_current_user() (no need of any global variable) and a conditional to be sure that the user is logged in:
if ( is_user_logged_in() ) {
$user_info = wp_get_current_user();
$user_last_name = $user_info->user_lastname;
printf( __( '%s', 'wpdance' ), $user_last_name );
}
Or with complete name:
if ( is_user_logged_in() ) {
$user_info = wp_get_current_user();
$user_complete_name = $user_info->user_firstname . ' ' . $user_info->user_lastname;
printf( __( '%s', 'wpdance' ), $user_complete_name );
}
References:
Get currentuserinfo firstname and lastname
Function Reference/wp get current user
Try calling
global $current_user;
get_currentuserinfo();
before
printf( __( '%s', 'wpdance' ),$current_user->user_lastname);
See https://codex.wordpress.org/Function_Reference/get_currentuserinfo#Examples
And are you sure the lastname is always set? Probably you can make sure $current_user works, if $current_user->ID at least returns a value.
And enable debugging in your wp_config.php might help as well to display all notices and errors:
define( 'WP_DEBUG', true );
See https://codex.wordpress.org/Debugging_in_WordPress