• Resolved drhur

    (@drhur)


    I would like to add this jquery script to remove p-tags around images:

    $('p > a > img').removeAttr('height').removeAttr('width').parent().unwrap();

    How do I make it work? How do I add the script? What do I need to do? Thank you for your help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    There’s a number of ways. A quick and dirty way would be to reference an external jQuery library with a <script> block in your theme’s header.php template, then place your script in another <script> block. That’s not really the “right” way.

    The right way seems quite convoluted for a one line script, but it’s good to be acquainted with it anyway. First you need a plugin or child theme to contain your code. This protects it during updates. The quick and dirty method mentioned earlier would be lost when you update your theme.

    Next put your script in an external .js file. Then on your plugin code page or child theme functions.php, enqueue your script file using wp_enqueue_script(). Be sure to specify ‘jquery’ as your script’s dependency. It must be in an array even if it’s the only dependency. Also note the section on noConflict wrappers and the first item in the Notes section.

    It’s not all as bad as it sounds, though it’s still a bit much for a one liner. It is however, The Way?.

    Thread Starter drhur

    (@drhur)

    Thank you bcworkz!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to add jquery script?’ is closed to new replies.