// =========================================================================
// ADD SUBPAGE BODY CLASS
// =========================================================================
function body_class_subpage($classes) {
global $wpdb, $post;
if(!is_front_page()){
$classes[] = 'sub-page';
}
return $classes;
}
add_filter('body_class','body_class_subpage');
PHP
Remove menu items from WordPress admin
Remove menu items for specific user roles
Add category and tag support to pages in WordPress
// =========================================================================
// ADD CATEGORY SUPPORT TO PAGES
// =========================================================================
function add_categories_to_pages() {
register_taxonomy_for_object_type( 'category', 'page' );
}
add_action( 'init', 'add_categories_to_pages' );
// =========================================================================
// ADD TAG SUPPORT TO PAGES
// =========================================================================
function tags_support_all() {
register_taxonomy_for_object_type('post_tag', 'page');
}
add_action('init', 'tags_support_all');
How to list categories in a dropdown menu
<?php
if( $terms = get_terms( array(
'taxonomy' => 'category', // to make it simple I use default categories
'orderby' => 'name'
) ) ) :
// if categories exist, display the dropdown
echo '<div class="isotope-filter-wrapper">';
echo '<select name="categoryfilter"><option value="">Select category...</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as an option value
endforeach;
echo '</select>';
echo '</div>';
endif;
?>
Add custom image size to media image
// ========================================================================= // ADD CUSTOM IMAGE SIZE // ========================================================================= add_image_size( 'custom-size', 800, 540, array( 'left', 'top' ) ); // Hard crop left top
Add class to the excerpt with filter
// =========================================================================
// ADD CLASS TO EXCERPT
// =========================================================================
add_filter( "the_excerpt", "add_class_to_excerpt" );
function add_class_to_excerpt( $excerpt ) {
return str_replace('<p', '<p class="card-text"', $excerpt);
}
Shortcode with attributes – custom link and text
// =========================================================================
// BUTTON SHORTCODE WITH ATTRIBUTES
// =========================================================================
function btn_shortcode( $atts, $content = null ) {
$a = shortcode_atts( array(
'href' => '#',
'text' => ''
), $atts );
return '<a class="btn-cta" href="' . esc_attr($a['href']) . '">' . esc_attr($a['text']) . '</a>';
}
add_shortcode( 'button', 'btn_shortcode' );
Add parent page slug to body_class
// =========================================================================
// ADD PARENT PAGE SLUG TO BODY CLASS
// =========================================================================
function body_class_section($classes) {
global $wpdb, $post;
if (is_page()) {
if ($post->post_parent) {
$parent = end(get_post_ancestors($current_page_id));
} else {
$parent = $post->ID;
}
$post_data = get_post($parent, ARRAY_A);
$classes[] = 'parent-' . $post_data['post_name'];
}
return $classes;
}
add_filter('body_class','body_class_section');
Add Facebook Pixel to wp_head
// =========================================================================
// ADD FACEBOOK PIXEL TO WP_HEAD
// =========================================================================
function add_facebook_pixel() { ?>
<!-- Facebook Pixel Code -->
<script>
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', 'xxxxxxxxxxx');
fbq('track', 'PageView');
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=xxxxxxxxxxx&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code -->
<?php }
add_action('wp_head', 'add_facebook_pixel');
// =========================================================================
// ADD FACEBOOK CONVERSION EVENT SNIPPET TO THANK YOU PAGE
// =========================================================================
function conversion_tracking_thank_you_page() { ?>
<script>
fbq('track', 'Purchase');
</script>';
<?php }
add_action( 'woocommerce_thankyou', 'conversion_tracking_thank_you_page' );
How to get slug of current page?
<?php
global $post;
$post_slug=$post->post_name;
?>
How to set different posts_per_page on mobile devices
<?php
wp_reset_postdata();
if( wp_is_mobile() ) {
$posts_per_page = 15;
} else{
$posts_per_page = -1;
}
$args = array(
'post_type' => 'my_post_type',
'posts_per_page' => $posts_per_page,
);
How to hide WordPress admin bar
function disable_admin_bar() {
if (current_user_can('administrator') || current_user_can('contributor') ) {
show_admin_bar(true);
} else {
// hide admin bar
show_admin_bar(false);
}
}
add_action('after_setup_theme', 'disable_admin_bar');
How to hide WordPress admin menus?
function hide_menu_items(){
remove_menu_page( 'index.php' ); // Dashboard
remove_menu_page( 'edit.php' ); // Posts
remove_menu_page( 'upload.php' ); // Media
remove_menu_page( 'edit.php?post_type=page' );
// Pages
remove_menu_page( 'edit-comments.php' );
// Comments
remove_menu_page( 'themes.php' ); // Appearance
remove_menu_page( 'plugins.php' ); // Plugins
remove_menu_page( 'users.php' ); // Users
remove_menu_page( 'tools.php' ); // Tools
remove_menu_page( 'options-general.php' );
// Settings
}
add_action( 'admin_menu', 'hide_menu_items' );
Ninja Forms: How do I Edit or Translate “Fields marked with an * are required?”
// =========================================================================
// TRANSLATE REQUIRED FIELDS IN NINJA FORMS
// =========================================================================
function my_custom_ninja_forms_i18n_front_end( $strings ) {
$strings['fieldsMarkedRequired'] = 'A *-gal jelölt mezők kitöltése kötelező';
return $strings;
}
add_filter( 'ninja_forms_i18n_front_end', 'my_custom_ninja_forms_i18n_front_end' );
How to create Custom Post Type Taxonomy
// =========================================================================
// CUSTOM POST TYPE TAXONOMY
// =========================================================================
function create_custom_post_types_taxonomy() {
register_taxonomy(
'POST_TYPE_cat',
'POST_TYPE',
array(
'label' => __( 'Kategória' ),
'rewrite' => array( 'slug' => 'POST_TYPE-type' ),
'hierarchical' => true,
'show_admin_column' => true,
)
);
}
add_action( 'init', 'create_custom_post_types_taxonomy' );
Add Cover Image Upload Option to Customizer
Add this to functions.php
// =========================================================================
// CUSTOMIZER COVER
// =========================================================================
function genesis_cover_customize_register( $wp_customize ) {
// Add Settings
$wp_customize->add_setting('customizer_setting_cover_image', array(
'transport' => 'refresh',
'height' => 325,
));
// Add Section
$wp_customize->add_section('cover_section', array(
'title' => __('Cover image', 'name-theme'),
'priority' => 70,
));
// Add Controls
$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'customizer_setting_cover_image', array(
'label' => __('Add cover image', 'name-theme'),
'section' => 'cover_section',
'settings' => 'customizer_setting_cover_image',
)));
}
add_action('customize_register', 'genesis_cover_customize_register');
Call
<img src="<?php echo esc_url( get_theme_mod( 'customizer_setting_cover_image' ) ); ?>" alt="Cover image">
Hook cover v1
// =========================================================================
// COVER
// =========================================================================
add_action('genesis_before_content', 'cover');
function cover(){
if ( is_front_page() ){
echo '<img class="cover" src="'.esc_url( get_theme_mod( 'customizer_setting_cover_image' ) ).'" alt="Cover Image">';
}
}
Hook cover v2
// =========================================================================
// COVER
// =========================================================================
add_action('genesis_after_header', 'add_cover');
function add_cover(){
if( is_front_page() ){
include('inc/cover.php');
}
}
include('functions/add-cover-to-customizer.php');
Remove junk from WP Head
Custom Post Type Taxonomy Filter in Admin
<?php
add_action('restrict_manage_posts', 'tsm_filter_post_type_by_taxonomy');
function tsm_filter_post_type_by_taxonomy() {
global $typenow;
$post_type = 'YOUR-POST-TYPE'; // change to your post type
$taxonomy = 'YOUR-TAXONOMY'; // change to your taxonomy
if ($typenow == $post_type) {
$selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
$info_taxonomy = get_taxonomy($taxonomy);
wp_dropdown_categories(array(
'show_option_all' => __("Összes kategória"),
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'orderby' => 'name',
'selected' => $selected,
'show_count' => true,
'hide_empty' => true,
));
};
}
add_filter('parse_query', 'tsm_convert_id_to_term_in_query');
function tsm_convert_id_to_term_in_query($query) {
global $pagenow;
$post_type = 'YOUR-POST-TYPE'; // change to your post type
$taxonomy = 'YOUR-TAXONOMY'; // change to your taxonomy
$q_vars = &$query->query_vars;
if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
$term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
$q_vars[$taxonomy] = $term->slug;
}
}
How to loop related posts by tags
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo '<h3>Kapcsolódó bejegyzések</h3>';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'posts_per_page'=>5,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args); ?>
<div class="row">
<?php if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="col-md-4">
<div class="card">
<div class="item-hover circle effect13 top_to_bottom">
<a href="<?php the_permalink(); ?>">
<div class="card-body">
<h3 class="card-title"><?php the_title(); ?></h3>
<p class="readmore"><i class="fas fa-angle-right"></i> <?php include('translate/tovabb.php'); ?></p>
</div>
<div class="info">
<div class="info-back home-posts">
<h3 class="label"><?php the_title(); ?></h3>
<p class="readmore-postsback"><i class="fas fa-angle-right"></i><?php include('translate/tovabb.php'); ?></p>
</div>
</div>
</a>
</div>
</div>
</div>
<?php
endwhile;
}
?>
</div>
<?php
wp_reset_query();
}
?>
WordPress Debug Mode
define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true ); define( 'WP_DEBUG_DISPLAY', false );