WordPress ScrollTo Hash

October 31, 2017 (3 years ago)
WordPress, JS

I needed to pass a hash, and scroll to the matching anchor on a new page.

Not as easy as it sounds in WordPress. (Pretty permalinks make it not work.)

After much testing and research, here’s the solution that worked best:

<script>
jQuery( document ).ready(function($) {
   var thehash = window.location.hash.replace('#', '');
   $('body, html').animate({ scrollTop: $('.'+thehash).offset().top -150 }, 1000);
});
</script>

I added the “-150” after the offset to show the header above the anchor.

WooCommerce – Conditional Ship to a Different Address Selection

October 31, 2017 (3 years ago)
WooCommerce, WordPress, JS, Functions

The scenario is this:

The site has multiple shipping methods (multiple client locations, and the option to ship to “Other”.)

I wanted to show the address panel if “Other” location was chosen, and hide the “Ship to Another Location”  option heading on the shipping page if a client location was chosen.

None of this was strictly necessary, but it makes for a smoother customer experience.

This went into my functions.php file.

/* added with my other enqueued scripts */
   wp_register_script( 'hideshipto', get_bloginfo( 'stylesheet_directory' ) . '/hide_ship_to.js', array( 'jquery' ) );


add_action( 'init', 'fb_ship_to_open' ); // opens the ship to for church dlievery or hides the ship to a different address for all others
function fb_ship_to_open () {
  global $woocommerce;
    if ( !is_admin() && WC()->session->get( 'chosen_shipping_methods' ) ) {
    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = $chosen_methods[0]; 
      if ('your_shipping_method_goes_here' == $chosen_shipping ) {
        add_filter( 'woocommerce_ship_to_different_address_checked', '__return_true' );
      } else {
        add_action( 'wp_enqueue_scripts', 'fb_load_ship_script' );
          function fb_load_ship_script() {
        wp_enqueue_script( 'hideshipto');
        }
      }
    }
  }

Then, this is the hide_ship_to.js script file. (uploaded to the theme directory)

jQuery( document ).ready(function($) {
    $('#ship-to-different-address').hide();
});

 

A Simple jQuery Accordion

February 27, 2017 (4 years ago)
JS, Functions

No need for the overhead of a plugin.

In this case, I have a custom post type for FAQs, where the title is the question, and the content is the answer.

This script hides all the answers until one of the questions is clicked.

Here’s the JavaScript:

<script type="text/javascript">
    jQuery(document).ready(function($) {
        $('.accordion').on('click', function() {
            $parent_box = $(this).closest('.faqbox');
            $parent_box.siblings().find('.panel').hide();
            $parent_box.find('.panel').toggle();
        });
    });
</script>

And the HTML:

<?php while ( have_posts() ) : the_post(); ?>

     <div class="faqbox">
          <h4 class="accordion"><i class="fa fa-plus-square" aria-hidden="true"></i> <?php the_title(); ?></h4>
          <div class="panel"><?php the_content(); ?></div>
     </div>

<?php    endwhile; ?>

 

Then, this little bit of CSS brings it together:

.faqbox { margin: 1em 0 0;
  .panel { display: none;  margin-left: 1em; }
  .accordion { cursor:pointer; }
}

 

Remove Emoji Support from WordPress 4.2

May 19, 2015 (5 years ago)
WordPress, JS, Functions

4.2 added the ability to use Emojicons in WP posts. If you don’t use them, you can remove the (unnecessary) scripts this adds to the header on all your site pages. Here’s the code for your functions.php file:

function pw_remove_emojicons() 
{
    // Remove from comment feed and RSS
    remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
    remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );

    // Remove from emails
    remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );

    // Remove from head tag
    remove_action( 'wp_head', 'print_emoji_detection_script', 7 );

    // Remove from print related styling
    remove_action( 'wp_print_styles', 'print_emoji_styles' );

    // Remove from admin area
    remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
    remove_action( 'admin_print_styles', 'print_emoji_styles' );
}
add_action( 'init', 'pw_remove_emojicons' );

 Thank you, Paulund!

http://www.paulund.co.uk/disable-emojicons-introduced-in-wordpress-4-2

Skip to toolbar