Hooks
{: .no_toc }
Table of Contents
{: .no_toc .text-delta }
- TOC {:toc}
wp_head
<?php function add_to_head() { ?>
<!-- META START -->
<meta name="theme-color" content="hsl(343, 100%, 48%)">
<meta name="msapplication-navbutton-color" content="hsl(343, 100%, 48%)">
<meta name="apple-mobile-web-app-status-bar-style" content="hsl(343, 100%, 48%)">
<!-- META END -->
<!-- DATALAYER START (Set up data layer) -->
<script> window.dataLayer = window.dataLayer || []; </script>
<!-- Push traffic_type 'internal' if logged in -->
<?php if ( is_user_logged_in() ) { ?>
<script>
dataLayer.push({
'event': 'traffic_type',
'traffic_type': 'internal'
});
</script>
<?php } ?>
<!-- DATALAYER END -->
<!-- ANALYTICS SCRIPTS START (Add if needed) -->
<script>
</script>
<!-- ANALYTICS SCRIPTS END -->
<?php } ?>
<?php add_action('wp_head', 'add_to_head'); ?>
add_to_body
<?php function add_to_body() { ?>
<script>
</script>
<?php } ?>
<?php add_action('wp_body_open', 'add_to_body'); ?>
wp_enqueue
<?php
function enqueue_custom_styles() {
wp_register_style(
'hook-name',
'$FILEPATH'
);
wp_register_script(
'hook-name',
'$FILEPATH',
'',
'',
true // Load script in footer
);
wp_enqueue_style( 'hook-name' );
wp_enqueue_script( 'hook-name' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_custom_styles' );
?>
wp_dequeue (front-end)
<?php
function dequeue_custom_styles(){
wp_deregister_style( 'hook-name' );
wp_dequeue_style( 'hook-name' );
}
add_action( 'wp_enqueue_scripts', 'dequeue_custom_styles');
wp_dequeue (admin)
function dequeue_custom_styles_admin(){
wp_deregister_style( 'hook-name' );
wp_dequeue_style( 'hook-name' );
}
add_action( 'admin_enqueue_scripts', 'dequeue_custom_styles_admin');
?>
register_post_type
<?php
// Add Custom Post Type
add_action( 'init', function () {
$name = 'Example';
$slug = 'example';
$singular_name = 'Example';
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( $name, 'Post Type General Name', 'twentytwenty' ),
'singular_name' => _x( $singular_name, 'Post Type Singular Name', 'twentytwenty' ),
'menu_name' => __( $name, 'twentytwenty' ),
'parent_item_colon' => __( 'Parent', 'twentytwenty' ),
'all_items' => __( 'Näytä kaikki', 'twentytwenty' ),
'view_item' => __( 'Näytä', 'twentytwenty' ),
'add_new_item' => __( 'Lisää uusi', 'twentytwenty' ),
'add_new' => __( 'Lisää uusi', 'twentytwenty' ),
'edit_item' => __( 'Muokkaa', 'twentytwenty' ),
'update_item' => __( 'Päivitä', 'twentytwenty' ),
'search_items' => __( 'Etsi', 'twentytwenty' ),
'not_found' => __( 'Ei loydy', 'twentytwenty' ),
'not_found_in_trash' => __( 'Ei loydy roskakorista', 'twentytwenty' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( $slug, 'twentytwenty' ),
'description' => __( $name, 'twentytwenty' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor'),
// You can associate this CPT with a taxonomy or custom taxonomy.
'taxonomies' => array('category'),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => false,
'show_ui' => true,
'menu_icon' => 'dashicons-editor-help',
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 6,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'capability_type' => 'post',
'show_in_rest' => true
);
// Registering your Custom Post Type
register_post_type( $slug, $args );
}, 0 );
?>