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');
?>
Related
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 );
I have this code that retrieves all images for the active page:
$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') );
foreach ( $attachments as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id, 'thumbnail' );
I need some help to open the images in a lightbox with the post/page title and navigation such as this https://simplelightbox.com/ one.
I am just learning the code so I desperately need some help.
I tried using fancy lightbox plugin but that did not work as the lightbox did not launch. For now I am hiding and showing the images using javascript.
From your code, I've created the following shortcode:
function so_all_images_lightbox() {
$output = '';
$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') );
foreach ( $attachments as $attachment_id => $attachment ) {
$output .= '<a class="lightboximage" href="'.wp_get_attachment_url($attachment_id).'">';
$output .= wp_get_attachment_image($attachment_id, 'thumbnail');
$output .= '</a>';
}
return $output;
}
add_shortcode('all_images_lightbox','so_all_images_lightbox');
Add [all_images_lightbox] to the site you want the gallery to be on. And it will display the images with a link to the image file.
Then you'll also need a lightbox-container somewhere in your documents (preferably short before the footer to avoid stacking problems):
function so_output_lightbox_container() { ?>
<div id="imagelightbox" class="image-lightbox">
<a id="close-lightbox" title="Close Lightbox" class="close-lightbox"></a>
<div id="imagecontainer"></div>
</div>
<?php }
add_action('get_footer', 'so_output_lightbox_container');
and the styling for it (you could also put this in the customizer css or your child theme css)
function so_output_lightbox_container_css() { ?>
<style>
.image-lightbox {
display: none;
z-index: -1;
background-color: rgba(0,0,0,.90);
width: 0;
height: 0;
position: fixed;
top: 0;
left: 0;
}
.image-lightbox.active {
display: block;
z-index: 99;
width: 100%;
height: 100%;
}
#imagecontainer {
display: flex;
align-items: center;
justify-content: center; height: 100vh;
position: relative;
}
</style>
<?php }
add_action('wp_head', 'so_output_lightbox_container_css');
Finally, you'd need some javascript to load the according images to the lightbox instead of follwing the link:
function so_output_lightbox_container_js() { ?>
<script>
( function() {
lightboxInit = function () {
const lightboximage = document.getElementsByClassName('lightboximage');
const imagelightbox = document.getElementById('imagelightbox');
const imagecontainer = document.getElementById('imagecontainer');
const lightboxbclose = document.getElementById('close-lightbox');
Array.prototype.forEach.call(lightboximage, function (el) {
el.addEventListener('click', function () {
event.preventDefault();
let imgfile = this.getAttribute("href");
imagelightbox.classList.add("active");
imagecontainer.innerHTML = '<img alt="Click to close" src="' + imgfile + '">';
});
});
lightboxbclose.addEventListener('click', function () {
imagelightbox.classList.remove("active");
imagecontainer.style.height = '';
imagecontainer.style.paddingBottom = '';
});
imagelightbox.addEventListener('click', function () {
imagelightbox.classList.remove("active");
});
}
lightboxInit();
} )();
</script>
<?php }
add_action('wp_footer', 'so_output_lightbox_container_js');
Tested by quickly throwing all of it in my functions.php and adding the shortcode to a site: Works. Click anywhere to close the lightbox. The styling of the additional close-button, I'll leave to you ;-)
Update/Edit:
To run the function from a template, just call the function within the template instead of running the shortcode. Add this to the template where needed:
echo so_output_lightbox_container();
You can then remove the line
add_shortcode('all_images_lightbox','so_all_images_lightbox');
of the first code block then, or leave it in place in case you may need the shortcode as well later.
Update 2:
To use an icon as a link, try replacing the first block with:
function so_all_images_lightbox() {
$output = '';
$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') );
foreach ( $attachments as $attachment_id => $attachment ) {
$output .= '<span class="imgbx"><a class="lightboximage" href="'.wp_get_attachment_url($attachment_id).'">';
$output .= '<i class="fas fa-camera"></i>';
$output .= '</a>';
$output .= wp_get_attachment_image($attachment_id, 'thumbnail').'</span>';
}
return $output;
}
add_shortcode('all_images_lightbox','so_all_images_lightbox');
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)
How do i share my website content to wordpress blog and google's blogger like facebook share, twitter share, without any third party tool.
Thanks
Step 1:
Go to your theme’s function.php file and paste below code. This will add sharing button at the bottom of the post
function crunchify_social_sharing_buttons($content) {
if(is_singular() || is_home()){
// Get current page URL
$crunchifyURL = get_permalink();
// Get current page title
$crunchifyTitle = str_replace( ' ', '%20', get_the_title());
// Get Post Thumbnail for pinterest
$crunchifyThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
// Construct sharing URL without using any script
$twitterURL = 'https://twitter.com/intent/tweet?text='.$crunchifyTitle.'&url='.$crunchifyURL.'&via=Crunchify';
$facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$crunchifyURL;
$googleURL = 'https://plus.google.com/share?url='.$crunchifyURL;
$bufferURL = 'https://bufferapp.com/add?url='.$crunchifyURL.'&text='.$crunchifyTitle;
// Based on popular demand added Pinterest too
$pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$crunchifyURL.'&media='.$crunchifyThumbnail[0].'&description='.$crunchifyTitle;
// Add sharing button at the end of page/page content
$content .= '<div class="crunchify-social">';
$content .= '<h5>SHARE ON</h5> <a class="crunchify-link crunchify-twitter" href="'. $twitterURL .'" target="_blank">Twitter</a>';
$content .= '<a class="crunchify-link crunchify-facebook" href="'.$facebookURL.'" target="_blank">Facebook</a>';
$content .= '<a class="crunchify-link crunchify-googleplus" href="'.$googleURL.'" target="_blank">Google+</a>';
$content .= '<a class="crunchify-link crunchify-buffer" href="'.$bufferURL.'" target="_blank">Buffer</a>';
$content .= '<a class="crunchify-link crunchify-pinterest" href="'.$pinterestURL.'" target="_blank">Pin It</a>';
$content .= '</div>';
return $content;
}else{
// if not a post/page then don't include sharing button
return $content;
}
};
add_filter( 'the_content', 'crunchify_social_sharing_buttons');
Step 2:
Open style.css file of your WordPress theme and put below code for better styling.
.crunchify-link {
padding: 4px 8px 6px 8px;
color: white;
font-size: 12px;
border-radius: 2px;
margin-right: 2px;
cursor: pointer;
-moz-background-clip: padding;
-webkit-background-clip: padding-box;
box-shadow: inset 0 -3px 0 rgba(0,0,0,.2);
-moz-box-shadow: inset 0 -3px 0 rgba(0,0,0,.2);
-webkit-box-shadow: inset 0 -3px 0 rgba(0,0,0,.2);
}
.crunchify-link:hover,.crunchify-link:active {
color: white;
}
.crunchify-twitter {
background: #00aced;
}
.crunchify-twitter:hover,.crunchify-twitter:active {
background: #0084b4;
}
.crunchify-facebook {
background: #3B5997;
}
.crunchify-facebook:hover,.crunchify-facebook:active {
background: #2d4372;
}
.crunchify-googleplus {
background: #D64937;
}
.crunchify-googleplus:hover,.crunchify-googleplus:active {
background: #b53525;
}
.crunchify-buffer {
background: #444;
}
.crunchify-buffer:hover,.crunchify-buffer:active {
background: #222;
}
.crunchify-pinterest {
background: #bd081c;
}
.crunchify-pinterest:hover,.crunchify-pinterest:active {
background: #bd081c;
}
.crunchify-social {
margin: 20px 0px 25px 0px;
-webkit-font-smoothing: antialiased;
font-size: 12px;
}
If you want to show Sharing button at the top of the post then use this code:
function crunchify_social_sharing_buttons($content) {
if(is_singular() || is_home()){
// Get current page URL
$crunchifyURL = get_permalink();
// Get current page title
$crunchifyTitle = str_replace( ' ', '%20', get_the_title());
// Get Post Thumbnail for pinterest
$crunchifyThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
// Construct sharing URL without using any script
$twitterURL = 'https://twitter.com/intent/tweet?text='.$crunchifyTitle.'&url='.$crunchifyURL.'&via=Crunchify';
$facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$crunchifyURL;
$googleURL = 'https://plus.google.com/share?url='.$crunchifyURL;
$bufferURL = 'https://bufferapp.com/add?url='.$crunchifyURL.'&text='.$crunchifyTitle;
// Based on popular demand added Pinterest too
$pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$crunchifyURL.'&media='.$crunchifyThumbnail[0].'&description='.$crunchifyTitle;
// Add sharing button at the end of page/page content
$variable .= '<div class="crunchify-social">';
$variable .= '<a class="crunchify-link crunchify-twitter" href="'. $twitterURL .'" target="_blank">Twitter</a>';
$variable .= '<a class="crunchify-link crunchify-facebook" href="'.$facebookURL.'" target="_blank">Facebook</a>';
$variable .= '<a class="crunchify-link crunchify-googleplus" href="'.$googleURL.'" target="_blank">Google+</a>';
$variable .= '<a class="crunchify-link crunchify-buffer" href="'.$bufferURL.'" target="_blank">Buffer</a>';
$variable .= '<a class="crunchify-link crunchify-pinterest" href="'.$pinterestURL.'" target="_blank">Pin It</a>';
$variable .= '</div>';
return $variable.$content;
}else{
// if not a post/page then don't include sharing button
return $variable.$content;
}
};
add_filter( 'the_content', 'crunchify_social_sharing_buttons');
Take a look here
I'm trying to generate dynamicly css file.
global $my_options;
WP_Filesystem();
global $wp_filesystem;
$css = '';
$file = './css/compile.css';
$css .= '
a{
color: '. $my_options['link-color']['regular'] .';
}
a:hover{
color: '. $my_options['link-color']['hover'] .';
}
';
if(!$wp_filesystem->put_contents($file, $css, FS_CHMOD_FILE)) {
echo 'Generating CSS error!';
}
var_dump($wp_filesystem->get_contents($file));
var_dump returns string(69) " a{ color: #81d742; } a:hover{ color: #1e73be; } ", but when I observing file compile.css - it hasn't any changes. I can't figure out what is the reason for this problem.
try with replacing two lines:
global $my_options;
global $wp_filesystem;
WP_Filesystem();
$css = '';
$file = './css/compile.css';
$css .= '
a{
color: '. $my_options['link-color']['regular'] .';
}
a:hover{
color: '. $my_options['link-color']['hover'] .';
}
';
if(!$wp_filesystem->put_contents($file, $css, FS_CHMOD_FILE)) {
echo 'Generating CSS error!';
}
var_dump($wp_filesystem->get_contents($file));
i have the same problem, solved by doing this
thanks
replace
$file = './css/compile.css';
with
$file = plugin_dir_path( __FILE__ ).'css/compile.css';
it will work fine..