disable media wordpress and use the browser file uploader to upload - php

I use the One User Avatar plugin, Inside the settings, there is a check box with the title "Always use the browser file uploader to upload avatars". I tick it. But when I try to change the default avatar, it still uses WordPress media, I came across the following code in the file below :
path : /plugins/one-user-avatar/includes/class-wp-user-avatar-admin.php
public function wpua_add_default_avatar() {
global $avatar_default, $mustache_admin, $mustache_medium, $wpua_avatar_default, $wpua_disable_gravatar, $wpua_functions;
// Remove get_avatar filter
remove_filter( 'get_avatar', array( $wpua_functions, 'wpua_get_avatar_filter' ) );
// Set avatar_list variable
$avatar_list = '';
// Set avatar defaults
$avatar_defaults = array(
'mystery' => __( 'Mystery Man', 'one-user-avatar'),
'blank' => __( 'Blank', 'one-user-avatar'),
'gravatar_default' => __( 'Gravatar Logo', 'one-user-avatar'),
'identicon' => __( 'Identicon (Generated)', 'one-user-avatar'),
'wavatar' => __( 'Wavatar (Generated)', 'one-user-avatar'),
'monsterid' => __( 'MonsterID (Generated)', 'one-user-avatar'),
'retro' => __( 'Retro (Generated)', 'one-user-avatar')
);
$avatar_defaults = apply_filters( 'avatar_defaults', $avatar_defaults );
// No Default Avatar, set to Mystery Man
if ( empty( $avatar_default ) ) {
$avatar_default = 'mystery';
}
// Take avatar_defaults and get examples for unknown#gravatar.com
foreach ( $avatar_defaults as $default_key => $default_name ) {
$avatar = get_avatar( 'unknown#gravatar.com', 32, $default_key );
$selected = ( $avatar_default == $default_key ) ? ' checked="checked" ' : '';
$avatar_list .= sprintf(
'<label><input type="radio" name="avatar_default" id="avatar_%1$s" value="%1$s" %2$s/> ',
esc_attr( $default_key ),
$selected
);
$avatar_list .= preg_replace( "/src='(.+?)'/", "src='\$1&forcedefault=1'", $avatar );
$avatar_list .= ' ' . $default_name . '</label>';
$avatar_list .= '<br />';
}
// Show remove link if custom Default Avatar is set
if ( ! empty( $wpua_avatar_default ) && $wpua_functions->wpua_attachment_is_image( $wpua_avatar_default ) ) {
$avatar_thumb_src = $wpua_functions->wpua_get_attachment_image_src( $wpua_avatar_default, array( 32, 32 ) );
$avatar_thumb = $avatar_thumb_src[0];
$hide_remove = '';
} else {
$avatar_thumb = $mustache_admin;
$hide_remove = ' class="wpua-hide"';
}
// Default Avatar is wp_user_avatar, check the radio button next to it
$selected_avatar = ( 1 == (bool) $wpua_disable_gravatar || 'wp_user_avatar' == $avatar_default ) ? ' checked="checked" ' : '';
// Wrap WPUA in div
$avatar_thumb_img = sprintf( '<div id="wpua-preview"><img src="%s" width="32" /></div>', esc_url( $avatar_thumb ) );
// Add WPUA to list
$wpua_list = sprintf(
'<label><input type="radio" name="avatar_default" id="wp_user_avatar_radio" value="wp_user_avatar" %s /> ',
$selected_avatar
);
$wpua_list .= preg_replace( "/src='(.+?)'/", "src='\$1'", $avatar_thumb_img );
$wpua_list .= ' ' . __( 'One User Avatar', 'one-user-avatar' ) . '</label>';
$wpua_list .= '<p id="wpua-edit"><button type="button" class="button" id="wpua-add" name="wpua-add" data-avatar_default="true" data-title="' . __( 'Choose Image', 'one-user-avatar' ) . ': ' . __( 'Default Avatar', 'one-user-avatar' ) . '">' . __( 'Choose Image', 'one-user-avatar' ) . '</button>';
$wpua_list .= '<span id="wpua-remove-button"' . $hide_remove . '>' . __( 'Remove', 'one-user-avatar' ) . '</span><span id="wpua-undo-button">' . __( 'Undo', 'one-user-avatar' ) . '</span></p>';
$wpua_list .= '<input type="hidden" id="wp-user-avatar" name="avatar_default_wp_user_avatar" value="' . $wpua_avatar_default . '">';
$wpua_list .= '<div id="wpua-modal"></div>';
if ( 1 != (bool) $wpua_disable_gravatar ) {
return $wpua_list . '<div id="wp-avatars">' . $avatar_list . '</div>';
} else {
return $wpua_list . '<div id="wp-avatars" style="display:none;">' . $avatar_list . '</div>';
}
}
How can I disable WordPress media and choose and upload a file from my computer?
I found the following code :
$wpua_list .= '<p id="wpua-edit"><button type="button" class="button" id="wpua-add" name="wpua-add" data-avatar_default="true" data-title="' . __( 'Choose Image', 'one-user-avatar' ) . ': ' . __( 'Default Avatar', 'one-user-avatar' ) . '">' . __( 'Choose Image', 'one-user-avatar' ) . '</button>';
I replaced it with the following code :
$wpua_list .= '<p id="wpua-edit"><input name="wpua-file" id="wpua-file" type="file"><button type="submit" class="button" id="wpua-upload" name="wpua-upload" data-avatar_default="true" data-title="' . __( 'Upload', 'one-user-avatar' ) . ': ' . __( 'Default Avatar', 'one-user-avatar' ) . '">' . __( 'Upload', 'one-user-avatar' ) . '</button>';
But when I go to wp-admin/options-discussion.php address and select and upload a photo from the computer, nothing is uploaded.
Where am I doing wrong? Thank you for taking the time to read my question.

Related

Sending Email Notifications To Admin When New Posts/Pages Are Pending

We have a script to notify admins by email when a new page or post is created by a contributor and is therefore 'pending' but the notifications are duplicating. The pages/posts are using gutenberg editor which I think is whats causing this (see here: https://github.com/WordPress/gutenberg/issues/15094) but I dont know what the solution is.
add_action( 'transition_post_status', 'pending_submission_notifications_send_email', 10, 3 );
function pending_submission_notifications_send_email( $new_status, $old_status, $post ) {
// Notify Admin that Contributor has written a post.
if ( 'pending' === $new_status && user_can( $post->post_author, 'edit_posts' ) && ! user_can( $post->post_author, 'publish_posts' ) ) {
$pending_submission_email = 'example#email.com';
$admins = ( empty( $pending_submission_email ) ) ? get_option( 'admin_email' ) : $pending_submission_email;
$edit_link = get_edit_post_link( $post->ID, '' );
$preview_link = get_permalink( $post->ID ) . '&preview=true';
$username = get_userdata( $post->post_author );
$username_last_edit = get_the_modified_author();
$subject = __( 'New post or page pending review', 'pending-submission-notifications' ) . ': "' . $post->post_title . '"';
$message = __( 'A new post or page is pending review.', 'pending-submission-notifications' );
$message .= "\r\n\r\n";
$message .= __( 'Author', 'pending-submission-notifications' ) . ': ' . $username->user_login . "\r\n";
$message .= __( 'Title', 'pending-submission-notifications' ) . ': ' . $post->post_title . "\r\n";
$message .= "\r\n\r\n";
$message .= __( 'Edit the submission', 'pending-submission-notifications' ) . ': ' . $edit_link . "\r\n";
$message .= __( 'Preview the submission', 'pending-submission-notifications' ) . ': ' . $preview_link;
$result = wp_mail( $admins, $subject, $message );
} // Notify Contributor that Admin has published their post.
elseif ( 'pending' === $old_status && 'publish' === $new_status && user_can( $post->post_author, 'edit_posts' ) && ! user_can( $post->post_author, 'publish_posts' ) ) {
$username = get_userdata( $post->post_author );
$url = get_permalink( $post->ID );
$subject = __( 'Your submission is now live! ', 'pending-submission-notifications' );
$message = '"' . $post->post_title . '" ' . __( 'was just published ', 'pending-submission-notifications' ) . "! \r\n\r\n";
$message .= $url;
$result = wp_mail( $username->user_email, $subject, $message );
}
}
You're correct, it does seem to be directly linked to the Gutenberg issue you linked to. (The reason why was answered in that thread here https://github.com/WordPress/gutenberg/issues/15094#issuecomment-558986406).
Essentially, if you use a plugin that adds custom meta data to a Gutenberg enabled post, the hook is fired twice for backwards compatibility to ensure that plugins that can't use the Gutenberg REST calls can still save their data.
To get your case working, you'll have to add a conditional check to your script to make sure you're running it on the correct pass (ie the non Rest call)
add_action( 'transition_post_status', 'pending_submission_notifications_send_email', 10, 3 );
function pending_submission_notifications_send_email( $new_status, $old_status, $post ) {
// If this is running in the Gutenberg REST call, ignore it
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { return false; }
// Notify Admin that Contributor has written a post.
if ( 'pending' === $new_status && user_can( $post->post_author, 'edit_posts' ) && ! user_can( $post->post_author, 'publish_posts' ) ) {
$pending_submission_email = 'example#email.com';
$admins = ( empty( $pending_submission_email ) ) ? get_option( 'admin_email' ) : $pending_submission_email;
$edit_link = get_edit_post_link( $post->ID, '' );
$preview_link = get_permalink( $post->ID ) . '&preview=true';
$username = get_userdata( $post->post_author );
$username_last_edit = get_the_modified_author();
$subject = __( 'New post or page pending review', 'pending-submission-notifications' ) . ': "' . $post->post_title . '"';
$message = __( 'A new post or page is pending review.', 'pending-submission-notifications' );
$message .= "\r\n\r\n";
$message .= __( 'Author', 'pending-submission-notifications' ) . ': ' . $username->user_login . "\r\n";
$message .= __( 'Title', 'pending-submission-notifications' ) . ': ' . $post->post_title . "\r\n";
$message .= "\r\n\r\n";
$message .= __( 'Edit the submission', 'pending-submission-notifications' ) . ': ' . $edit_link . "\r\n";
$message .= __( 'Preview the submission', 'pending-submission-notifications' ) . ': ' . $preview_link;
$result = wp_mail( $admins, $subject, $message );
} // Notify Contributor that Admin has published their post.
elseif ( 'pending' === $old_status && 'publish' === $new_status && user_can( $post->post_author, 'edit_posts' ) && ! user_can( $post->post_author, 'publish_posts' ) ) {
$username = get_userdata( $post->post_author );
$url = get_permalink( $post->ID );
$subject = __( 'Your submission is now live! ', 'pending-submission-notifications' );
$message = '"' . $post->post_title . '" ' . __( 'was just published ', 'pending-submission-notifications' ) . "! \r\n\r\n";
$message .= $url;
$result = wp_mail( $username->user_email, $subject, $message );
}
}
Bear in mind this is untested, but according to the issue report linked above, this should ignore the first pass through the Gutenberg editor and only pass in the version with the correct post data.

Making custom field appear in captions for image attachments (Wordpress)

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.

How to rearrange/customize html in woocommerce checkout form fields

I want to accomplish 2 things in my woocommerce checkout form:
1. Put some text between some groups of fields, for example (h3):
<h3>my custom heading</h3>
<p class="form-row form-row validate-required">
<input type="email">...
</p>
<h3>my other custom heading</h3>
<p class="form-row form-row validate-required">
<input type="text">...
</p>
<p class="form-row form-row validate-required">
<input type="text">...
</p>
2. Display input tag first and label tag as second in html (woocommerce display label before input by default)
I figured out that checkout form is displayed by this code (for billing):
<?php foreach ( $checkout->checkout_fields['billing'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
How can I customize it in way I described?
I'm not sure on part 1, but for part 2, you can modify the output of woocommerce_form_field() by filtering woocommerce_form_field_$type.
So your part 2 can be solved like so:
function so_39267627_form_field( $field, $key, $args, $value ){
if ( $args['required'] ) {
$args['class'][] = 'validate-required';
$required = ' <abbr class="required" title="' . esc_attr__( 'required', 'woocommerce' ) . '">*</abbr>';
} else {
$required = '';
}
$args['maxlength'] = ( $args['maxlength'] ) ? 'maxlength="' . absint( $args['maxlength'] ) . '"' : '';
$args['autocomplete'] = ( $args['autocomplete'] ) ? 'autocomplete="' . esc_attr( $args['autocomplete'] ) . '"' : '';
if ( is_string( $args['label_class'] ) ) {
$args['label_class'] = array( $args['label_class'] );
}
if ( is_null( $value ) ) {
$value = $args['default'];
}
// Custom attribute handling
$custom_attributes = array();
// Custom attribute handling
$custom_attributes = array();
if ( ! empty( $args['custom_attributes'] ) && is_array( $args['custom_attributes'] ) ) {
foreach ( $args['custom_attributes'] as $attribute => $attribute_value ) {
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
}
}
$field = '';
$label_id = $args['id'];
$field_container = '<p class="form-row %1$s" id="%2$s">%3$s</p>';
$field .= '<input type="' . esc_attr( $args['type'] ) . '" class="input-text ' . esc_attr( implode( ' ', $args['input_class'] ) ) .'" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" ' . $args['maxlength'] . ' ' . $args['autocomplete'] . ' value="' . esc_attr( $value ) . '" ' . implode( ' ', $custom_attributes ) . ' />';
if ( ! empty( $field ) ) {
$field_html = '';
$field_html .= $field;
if ( $args['description'] ) {
$field_html .= '<span class="description">' . esc_html( $args['description'] ) . '</span>';
}
if ( $args['label'] && 'checkbox' != $args['type'] ) {
$field_html .= '<label for="' . esc_attr( $label_id ) . '" class="' . esc_attr( implode( ' ', $args['label_class'] ) ) .'">' . $args['label'] . $required . '</label>';
}
$container_class = 'form-row ' . esc_attr( implode( ' ', $args['class'] ) );
$container_id = esc_attr( $args['id'] ) . '_field';
$after = ! empty( $args['clear'] ) ? '<div class="clear"></div>' : '';
$field = sprintf( $field_container, $container_class, $container_id, $field_html ) . $after;
}
return $field;
}
add_filter( 'woocommerce_form_field_password', 'so_39267627_form_field', 10, 4 );
add_filter( 'woocommerce_form_field_text', 'so_39267627_form_field', 10, 4 );
add_filter( 'woocommerce_form_field_email', 'so_39267627_form_field', 10, 4 );
add_filter( 'woocommerce_form_field_tel', 'so_39267627_form_field', 10, 4 );
add_filter( 'woocommerce_form_field_number', 'so_39267627_form_field', 10, 4 );
You would need to write a few more functions (mostly I copied and pasted whole swaths of code from WooCommerce and then just swapped the label part around ) for other other field types, but this should serve as an example.
For the first one you can do the following to display single input field
<?php
woocommerce_form_field(
"billing_first_name",
$checkout->checkout_fields['billing']['billing_first_name'],
$checkout->get_value( 'billing_first_name' )
);
?>
where you can replace first_name with any key of the checkout billing fields
So for your example it will be something like this
<h3>my custom heading</h3>
<p class="form-row form-row validate-required">
<?php woocommerce_form_field( "billing_email", $checkout->checkout_fields['billing']['billing_email'], $checkout->get_value( 'billing_email' ) ); ?>
</p>
As for the second I'm not sure how you can achieve that. I needed something similar and i used javascript to reposition the labels.
something like :
jQuery('form.checkout label').each(function(){
jQuery(this).insertAfter(jQuery(this).parent().find('input'));
})
As far as I think, woo commerce does not support html, there's a plugin known as woocommerce checkout field editor, see if it solves your issue.

How to calculate time range between sale start and sale end including the second?

Let me say about the condition I need to ask:
I display the start sale and end sale for a product on WooCommerce. The code on loop like this:
<?php $thepostid = get_the_ID();
if ( $date = get_post_meta( $thepostid, '_sale_price_dates_from', true ) ) {
$sale_price_dates_from =
'<div class="fromsale">' .
'<span class="promodesc">' . esc_html__( 'Sale start:', 'mytheme' ). '</span>' .
'<span class="m">' . date_i18n( 'M', $date ) . '</span> ' .
'<span class="d">' . date_i18n( 'j', $date ) . '</span> ' .
'</div>'
;
} else {
$sale_price_dates_from =
'<div class="endsale">' .
'<span class="promodesc">' . esc_html__( 'No Schedule', 'mytheme' ). '</span> ' .
'</div>'
; }
echo $sale_price_dates_from ;
?>
<?php $thepostid = get_the_ID();
if ( $date = get_post_meta( $thepostid, '_sale_price_dates_to', true ) ) {
$sale_price_dates_to =
'<div class="endsale">' .
'<span class="promodesc">' . esc_html__( 'Sale end:', 'mytheme' ). '</span>' .
'<span class="m">' . date_i18n( 'M', $date ) . '</span> ' .
'<span class="d">' . date_i18n( 'j', $date ) . '</span> ' .
'</div>'
;
} else {
$sale_price_dates_to =
'<div class="endsale">' .
'<span class="promodesc">' . esc_html__( 'No Schedule', 'mytheme' ). '</span> ' .
'</div>'
; }
echo $sale_price_dates_to ;
?>
The result is like this:
Sale start: May 13
Sale end: May 22
I am curious, how to get times range between the start and the end? In this case I want to display the countdown like this (for example):
Sale will be end: 7days 20hours 33minutes 22seconds.
Can anyone help me?
Thank for any kind of helps.

Wordpress list category in custom post type

I have a query below which I want to list ONLY those categories assigned to the current post I am viewing.
At the moment, it lists ALL of the categories for my custom post type.
Is it possible to list just those for the individual post? The post type is called 'resource' and the category attached to this post type is called 'resource-category'.
<?php
$taxonomy = 'resource-category';
$tax_terms = get_terms($taxonomy);
?>
<?php
foreach ($tax_terms as $tax_term) {
echo '' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a> ';
}
?>
You can use wp_get_post_terms:
<?php
$taxonomy = 'resource-category';
$tax_terms = wp_get_post_terms($post->ID, $taxonomy, array("fields" => "all"));
foreach ($tax_terms as $tax_term) {
echo '' . '<a href="' . esc_attr(get_term_link($tax_term->term_id, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a> ';
}
?>

Categories