Adding javascript to wordpress theme - wordpress

I am new to wordpress. I need to create a website using wordpress so i need to add javascript file to wordpress themes..
For example what i want is the background color of header will be change when i scroll the page of my wordpress site.. For that i use javascript but its not worked for me..
I used like that below,
functions.php
function my_theme_enqueue_script() {
wp_register_script( 'scrolldown' , get_template_directory_uri() . '/js/scroll.js' );
wp_enqueue_script( 'scrolldown');
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_script' );
scroll.js
$(window).on("scroll", function() {
if($(window).scrollTop() > 50) {
$(".site-header").addClass("active");
} else {
$(".site-header").removeClass("active");
}
});
style.css
.site-header {
height: 100px;
width: 1200px;
transition: all 0.2s ease-in-out;
z-index:10000;
margin-left:-90px;
position:fixed;
background-color: transparent;
margin-top:-70px;
}
.site-header.active {
background: #353535 !important;
}
I don't know what I did wrong.. Can anyone help me to solve my problem..
When i go to console, I have this window below screen shot,

STEP 1:
in your functions.php replace this:
function my_theme_enqueue_script() {
wp_register_script( 'scrolldown' , get_template_directory_uri() . '/js/scroll.js' );
wp_enqueue_script( 'scrolldown');
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_script' );
with this:
function my_theme_enqueue_script() {
wp_enqueue_script( 'scrolldown' , get_template_directory_uri() . '/js/scroll.js', array( 'jquery' ), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_script', 50, 0 );
STEP: 2
in your scroll.js replace this:
$(window).on("scroll", function() {
if($(window).scrollTop() > 50) {
$(".site-header").addClass("active");
} else {
$(".site-header").removeClass("active");
}
});
with this:
(function($){
$(window).on("scroll", function() {
if($(window).scrollTop() > 50) {
$(".site-header").addClass("active");
} else {
$(".site-header").removeClass("active");
}
});
})(jQuery)

Related

Gallery metabox doesn´t show new images / save changes since Wordpress 6.0

I use the code below to display images in posts. It basically works, but changes made via "Add Image" are no longer saved since WP 6.0 or 6.1.
The manual variant via the "post_banner_image" field below by entering the image ID still works.
here a front-end screenshot
<?php
// Add Meta Box to post
add_action( 'add_meta_boxes', 'multi_media_uploader_meta_box' );
function multi_media_uploader_meta_box() {
add_meta_box( 'my-post-box', 'Media Field', 'multi_media_uploader_meta_box_func', 'post', 'normal', 'high' );
}
function multi_media_uploader_meta_box_func($post) {
$banner_img = get_post_meta($post->ID,'post_banner_img', true);
?>
<style type="text/css">
.multi-upload-medias ul li .delete-img { position: absolute; right: 3px; top: 2px; background: aliceblue; border-radius: 50%; cursor: pointer; font-size: 14px; line-height: 20px; color: red; }
.multi-upload-medias ul li { width: 120px; display: inline-block; vertical-align: middle; margin: 5px; position: relative; }
.multi-upload-medias ul li img { width: 100%; }
</style>
<table cellspacing="10" cellpadding="10">
<tr>
<td>Banner Image</td>
<td>
<?php echo multi_media_uploader_field( 'post_banner_img', $banner_img ); ?>
</td>
</tr>
</table>
<script type="text/javascript">
jQuery(function($) {
$('body').on('click', '.wc_multi_upload_image_button', function(e) {
e.preventDefault();
var button = $(this),
custom_uploader = wp.media({
title: 'Insert image',
button: { text: 'Use this image' },
multiple: true
}).on('select', function() {
var attech_ids = '';
attachments
var attachments = custom_uploader.state().get('selection'),
attachment_ids = new Array(),
i = 0;
attachments.each(function(attachment) {
attachment_ids[i] = attachment['id'];
attech_ids += ',' + attachment['id'];
if (attachment.attributes.type == 'image') {
$(button).siblings('ul').append('<li data-attechment-id="' + attachment['id'] + '"><img class="true_pre_image" src="' + attachment.attributes.url + '" /><i class=" dashicons dashicons-no delete-img"></i></li>');
} else {
$(button).siblings('ul').append('<li data-attechment-id="' + attachment['id'] + '"><img class="true_pre_image" src="' + attachment.attributes.icon + '" /><i class=" dashicons dashicons-no delete-img"></i></li>');
}
i++;
});
var ids = $(button).siblings('.attechments-ids').attr('value');
if (ids) {
var ids = ids + attech_ids;
$(button).siblings('.attechments-ids').attr('value', ids);
} else {
$(button).siblings('.attechments-ids').attr('value', attachment_ids);
}
$(button).siblings('.wc_multi_remove_image_button').show();
})
.open();
});
$('body').on('click', '.wc_multi_remove_image_button', function() {
$(this).hide().prev().val('').prev().addClass('button').html('Add Media');
$(this).parent().find('ul').empty();
return false;
});
});
jQuery(document).ready(function() {
jQuery(document).on('click', '.multi-upload-medias ul li i.delete-img', function() {
var ids = [];
var this_c = jQuery(this);
jQuery(this).parent().remove();
jQuery('.multi-upload-medias ul li').each(function() {
ids.push(jQuery(this).attr('data-attechment-id'));
});
jQuery('.multi-upload-medias').find('input[type="hidden"]').attr('value', ids);
});
})
</script>
<?php
}
function multi_media_uploader_field($name, $value = '') {
$image = '">Add Media';
$image_str = '';
$image_size = 'full';
$display = 'none';
$value = explode(',', $value);
if (!empty($value)) {
foreach ($value as $values) {
if ($image_attributes = wp_get_attachment_image_src($values, $image_size)) {
$image_str .= '<li data-attechment-id=' . $values . '><img src="' . $image_attributes[0] . '" /><i class="dashicons dashicons-no delete-img"></i></li>';
}
}
}
if($image_str){
$display = 'inline-block';
}
return '<div class="multi-upload-medias"><ul>' . $image_str . '</ul>Remove media</div>';
}
// Save Meta Box values.
add_action( 'save_post', 'wc_meta_box_save' );
function wc_meta_box_save( $post_id ) {
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if( !current_user_can( 'edit_post' ) ){
return;
}
if( isset( $_POST['post_banner_img'] ) ){
update_post_meta( $post_id, 'post_banner_img', $_POST['post_banner_img'] );
}
}
?>
Can someone help me to get the other part working again?
current_user_can('edit_post') is no longer valid in WordPress 6.1. Here's the relevant core trac ticket.
You must now use one of these formats:
current_user_can( 'edit_posts' );
current_user_can( 'edit_post', $post->ID );
current_user_can( 'edit_post_meta', $post->ID, $meta_key );

How to center text on "Empty Cart" button?

I have the below code for adding a "clear cart"button to my woocommerce cart page. The text on the button is not centered on mobile and would like it to be. The button also is not completely round and looks wrong. Please visit my site https://www.livestainable.co.za add an item to the cart to replicate my issue. I have also added some screenshots of the issue. I would very much appreciate if someone can help me out with this. Thanks guys
here is the screenshot of the issue on mobile.
add_action( 'woocommerce_cart_coupon', 'custom_woocommerce_empty_cart_button' );
function custom_woocommerce_empty_cart_button() {
echo '' . esc_html( 'Empty Cart', 'woocommerce' ) . '';
}
add_action( 'wp_loaded', 'custom_woocommerce_empty_cart_action', 20 );
function custom_woocommerce_empty_cart_action() {
if ( isset( $_GET['empty_cart'] ) && 'yes' === esc_html( $_GET['empty_cart'] ) ) {
WC()->cart->empty_cart();
$referer = wp_get_referer() ? esc_url( remove_query_arg( 'empty_cart' ) ) : wc_get_cart_url();
wp_safe_redirect( $referer );
}
}
You simply have to add some lines of code to your style
/* centering text on mobile */
#media (max-width:767px) {
table.shop_table.cart .coupon .button {
text-align: center;
}
}
/* fixing border radius on dekstop */
#media (min-width:768px) {
table.cart .coupon .button:not(:last-child) {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
}

Customize cart item removed notice in Woocommerce 3

I am using the WooCommerce 3.4.4 version and I am trying to edit the notice message when you press the "X" button to remove the product from the cart page.
Currently the notice is "<product name>" removed. Undo?
I want to remove the quotations and change the message to Product name has been removed. [Undo button]
I have managed to remove the quotation marks from the product name by writing
add_filter( 'woocommerce_cart_item_removed_title', 'removed_from_cart', 10, 2);
function removed_from_cart( $message, $product_data ) {
$product = get_the_title($product_data['product_id']);
$message = $product;
return $message;
}
But I am a bit confused on how to do the rest of the edits. Any help is appreciated.
Updated
The only available hook is woocommerce_cart_item_removed_title that you are using already. and displays the product name between quotes. You can also use gettex filter hook to remove the ? after "Undo" text:
add_filter( 'woocommerce_cart_item_removed_title', 'removed_from_cart_title', 12, 2);
function removed_from_cart_title( $message, $cart_item ) {
$product = wc_get_product( $cart_item['product_id'] );
if( $product )
$message = sprintf( __('Product %s has been'), $product->get_name() );
return $message;
}
add_filter('gettext', 'cart_undo_translation', 35, 3 );
function cart_undo_translation( $translation, $text, $domain ) {
if( $text === 'Undo?' ) {
$translation = __( 'Undo', $domain );
}
return $translation;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
But you can not change or add button tag class the the <a> html tag…
Instead use the existing restore-item tag class adding some custom CSS styles to it.
Below some CSS styles example, that you can add to the styles.css file of your active child theme:
.woocommerce-message .restore-item, {
float: right;
padding: 0 0 0 1em;
background: 0 0;
color: #fff;
box-shadow: none;
line-height: 1.618;
border-width: 0 0 0 1px;
border-left-style: solid;
border-left-color: rgba(255,255,255,.25)!important;
border-radius: 0;
}
.woocommerce-message .restore-item:hover {
background: 0 0;
color: #fff;
opacity: .8;
}
This is what you will get:

How to display block or trigger the style on Woocommerce if !is_in_stock

I am trying to display a block or DIV when the product is out of stock but the condition does not appear to work
add_action( 'woocommerce_get_availability', 'display_welcome', 10);
function display_welcome() {
global $_product;
if ( ! $_product->is_in_stock() ) {
?>
<style>
#hello {display:block }
</style>
<?
}
}
html
<div id="hello">Try this product instead</div>
add_action('woocommerce_get_availability', 'display_welcome', 10, 2);
function display_welcome($availability_class, $_product) {
if ($availability_class['availability'] == 'Out of stock') {
$availability_class['class'] = $availability_class['class'] . ' my-customclass';
add_action('woocommerce_single_product_summary', 'hooks_add_div', 33);
function hooks_add_div() {
echo '<div id="helllo">mujeebu rahman</div>';
}
}
}

slideToggle, fadeIn, fadeOut animation shifts up during animation

I'm creating a simple "Read More/Read Less" button for a WordPress site that when pressed reveals hidden content using slideToggle. It's in the form of a WordPress plugin. The buttons fadeIn and fadeOut. And I'm using php/html to display the page.
Everything is working great except one little hiccup. For some reason, when the Read More button is clicked and the animation starts, the content div (or p tag - I can't tell for sure) starts a few pixels down and then shifts up after it finishes the animation. And vice versa, when the Read Less button is clicked, the animation does the same thing.
I've looked through the php and css and I can't find anything. I've even inspected the elements and removed all the css that way but it's not helping.
I've included all the code and a short video to show you what I mean.
Here is the link to the video: https://youtu.be/3LnmaPglyPI
Javascript
jQuery('.wpsm-content').addClass('wpsm-content-hide');
jQuery('.wpsm-show, .wpsm-hide').removeClass('wpsm-content-hide');
jQuery('.wpsm-show').on('click', function (e) {
"use strict";
var currentContent = jQuery(this);
function complete() {
jQuery(currentContent).next('.wpsm-content').slideToggle(800);
}
jQuery(this).fadeOut(200, complete);
e.preventDefault();
});
jQuery('.wpsm-hide').on('click', function (e) {
"use strict";
var wpsm = jQuery(this).parent('.wpsm-content');
function complete() {
wpsm.prev('.wpsm-show').fadeIn(200); // Read More
}
wpsm.slideToggle(1250, complete);
e.preventDefault();
});
CSS
.wpsm-show a, .wpsm-show:active, .wpsm-show:visited {
cursor: pointer;
text-decoration: none;
}
.wpsm-show:hover {
cursor: pointer;
text-decoration: underline;
}
.wpsm-hide a, .wpsm-hide:active, .wpsm-hide:visited {
cursor: pointer;
text-decoration: none;
}
.wpsm-hide:hover {
cursor: pointer;
text-decoration: underline;
}
.wpsm-content-hide {
display: none;
}
PHP/HTML
add_shortcode( 'show_more', 'wpsm');
function wpsm( $attr, $smcontent ) {
if (!isset($attr['color'])) $attr['color'] = '#cc0000';
if (!isset($attr['list'])) $attr['list'] = '';
if (!isset($attr['more'])) $attr['more'] = 'show more';
if (!isset($attr['less'])) $attr['less'] = 'show less';
$wpsm_string = '<div class="show_more">';
$wpsm_string .= '<p class="wpsm-show" style="color: ' . $attr['color'] . ' ">';
$wpsm_string .= $attr['list']. ' ' . $attr['more'];
$wpsm_string .= '</p><div class="wpsm-content">';
$wpsm_string .= $smcontent;
$wpsm_string .= ' <p class="wpsm-hide" style="color: ' . $attr['color'] . ' ">';
$wpsm_string .= $attr['list']. ' ' . $attr['less'];
$wpsm_string .= '</p>';
$wpsm_string .= '</div></div>';
return $wpsm_string;
}
add_action( 'wp_enqueue_scripts', 'sm_scripts');
function sm_scripts (){
$plugin_url = plugins_url( '/', __FILE__ );
wp_enqueue_style (
'sm-style',
$plugin_url . 'wpsm-style.css'
);
wp_enqueue_script (
'sm-script',
$plugin_url . 'wpsm-script-mine.js',
array( 'jquery' ),
'1.0.1',
true
);
}
add_action('wp_footer', 'sm_scripts');
?>

Resources