How to add defer=”defer” tag in plugin javascripts?

As of WordPress 4.1 there is a filter: script_loader_tag. You can use it to find the correct script: add_filter( ‘script_loader_tag’, function ( $tag, $handle ) { if ( ‘contact-form-7’ !== $handle ) return $tag; return str_replace( ‘ src’, ‘ defer=”defer” src’, $tag ); }, 10, 2 ); Old answer There is no dedicated filter available … Read more

WordPress hooks/filters insert before content or after title

Just use the the_content filter, e.g.: <?php function theme_slug_filter_the_content( $content ) { $custom_content=”YOUR CONTENT GOES HERE”; $custom_content .= $content; return $custom_content; } add_filter( ‘the_content’, ‘theme_slug_filter_the_content’ ); ?> Basically, you append the post content after your custom content, then return the result. Edit As Franky @bueltge points out in his comment, the process is the same … Read more

Get a list of all registered actions

Filters and actions are both assigned to hooks. Functions assigned to hooks are stored in global $wp_filter variable. So all you have to do is to print_r it. print_r($GLOBALS[‘wp_filter’]); PS. add_action function makes a add_filter call. And the latter does $wp_filter[$tag][$priority][$idx]. NOTE: you can directly add this code in functions.php, and you will see a … Read more

Filter to remove image dimension attributes?

Thanks all! The image_send_to_editor filter was the one I was looking for… thanks @t31os for pointing it out. Here’s my functions now. add_filter( ‘post_thumbnail_html’, ‘remove_thumbnail_dimensions’, 10 ); add_filter( ‘image_send_to_editor’, ‘remove_thumbnail_dimensions’, 10 ); function remove_thumbnail_dimensions( $html ) { $html = preg_replace( ‘/(width|height)=\”\d*\”\s/’, “”, $html ); return $html; } This removes inline dimension attributes from images retrieved … Read more

How to know what functions are hooked to an action/filter?

Look into the global variable $wp_filter. See my plugin for a list of all comment filters for an example: <?php /* Plugin Name: List Comment Filters Description: List all comment filters on wp_footer Version: 1.1 Author: Fuxia Scholz License: GPL v2 */ add_action( ‘wp_footer’, ‘list_comment_filters’ ); function list_comment_filters() { global $wp_filter; $comment_filters = array (); … Read more

remove empty paragraphs from the_content?

WordPress will automatically insert <p> and </p> tags which separate content breaks within a post or page. If, for some reason, you want or need to remove these, you can use either of the following code snippets. To completely disable the wpautop filter, you can use: remove_filter(‘the_content’, ‘wpautop’); If you still want this to function … Read more

Add filter menu to admin list of posts (of custom type) to filter posts by custom field values

Simple to do, first create the dropdown with just the meta values you want and then catch the submit of the filter, just change POST_TYPE to the name of your post type and META_KEY to the name of your meta key: <?php /* Plugin Name: Admin Filter BY Custom Fields Plugin URI: http://en.bainternet.info Description: answer … Read more