WordPress Hooks

Posted On May 24, 2018

Kodewulf

Web developer and code junkie. Dabbling in just about anything that interests me.

Some useful utility functions for WordPress.

Allow shortcode within HTML

add_filter( 'wp_kses_allowed_html', function ( $allowedposttags, $context ) { if ( $context == 'post' ) { $allowedposttags['input']['value'] = 1; } return $allowedposttags; }, 10, 2 );

Removes the “?” from styles/scripts

/**
 * WordPress remove script/style versions
 */
function blanktheme_remove_loader_src( $src ){
    $parts = explode( '?ver', $src );
    return $parts[0];
}
add_filter( 'script_loader_src', 'blanktheme_remove_loader_src', 9999, 1 );
add_filter( 'style_loader_src', 'blanktheme_remove_loader_src', 9999, 1 );

Merge all JavaScripts

function blanktheme_scripts_merge() {
    global $wp_scripts;
    $wp_scripts->all_deps( $wp_scripts->queue );
    $merged_file_location = get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . 'merged.js';
    $merged_script = '';
    // Loop javascript files and save to $merged_script variable
    foreach( $wp_scripts->to_do as $handle) {
        $src = strtok($wp_scripts->registered[$handle]->src, '?');
        if ( strpos( $src, 'http' ) !== false ) {
            $site_url = site_url();
            if ( strpos( $src, $site_url ) !== false )
                $js_file_path = str_replace( $site_url, '', $src );
            else
                $js_file_path = $src;
            $js_file_path = ltrim( $js_file_path, '/' );
        } else {
            $js_file_path = ltrim($src, '/');
        }
        if  ( file_exists( $js_file_path ) ) {
            $localize = '';
            if (@key_exists('data', $wp_scripts->registered[$handle]->extra)) {
                $localize = $obj->extra['data'] . ';';
            }
            $merged_script .=  $localize . file_get_contents($js_file_path) . ';';
        }
    }
    $merged_script = preg_replace( '!/*.*?*/!s', '', $merged_script );
    $merged_script = preg_replace( '/ns*n/', "n", $merged_script );
    file_put_contents ( $merged_file_location , $merged_script );
    wp_enqueue_script( 'merged-scripts', get_stylesheet_directory_uri() . '/js/merged.js' );
    foreach ( $wp_scripts->to_do as $handle ) {
        wp_deregister_script( $handle );
    }
}
add_action( 'wp_enqueue_scripts', 'blanktheme_scripts_merge', 9999 );

Redirect only on 404 for specific url

// See https://developer.wordpress.org/reference/hooks/template_redirect/
add_action( 'template_redirect', blanktheme_redirect_not_found_paths' );
function blanktheme_redirect_not_found_paths() {
    // See https://developer.wordpress.org/reference/functions/is_404/
    if (is_404()) {
        // Initialise $redirects array
        $redirects = array();
        // Redirect '/?page_id=' to '/'
        $redirects[] = array(
            'match' => '/?page_id=',
            'redirect' => '/'
        );
        // Redirect '/category/hsc-subjects/page/7' to '/category/hsc-subjects'
        $redirects[] = array(
            'match' => '/category/hsc-subjects/page',
            'redirect' => '/category/hsc-subjects'
        );
        foreach ($redirects as $match) {
            if (strpos($_SERVER['REQUEST_URI'], $match['match']) !== false) {
                wp_redirect($match['redirect']);
                exit;
            }
        }
    }
}

Convert `$_POST` data to shortcodes

/**
 * Shortcodes convert $_POST data to shortcodes
 */
if ($_REQUEST) {
    foreach ( $_REQUEST as $key => $value ) {
        add_shortcode( $key, 'wp1434_post_data_shortcode' );
    }
}
function wp1434_post_data_shortcode( $atts = array(), $content = false, $tag ) {
    return !empty( $_REQUEST[ $tag ] ) ? $_REQUEST[ $tag ] : '';
}

More adventures

Javascript sticky header magic

Some sticky header magic.  If a sticky header overlaps some content at the top of the page, this will move page content down a bit.

Google Event Tracking

Google gtag.js event code To be included in HEAD, replacing #### with UA identifier <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=UA-#########-1"></script> <script>...

Share This

Share this post with your friends!