Snippets repository for WordPress developpers

Browse

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// Core updates
add_filter( 'auto_core_update_send_email', 'wpb_stop_auto_update_emails', 10, 4 );
function wpb_stop_update_emails( $send, $type, $core_update, $result ) {
if ( ! empty( $type ) && $type == 'success' ) {
return false;
}
return true;
}
// Plugins updates
add_filter( 'auto_plugin_update_send_email', '__return_false' );
//Themes updates
add_filter( 'auto_theme_update_send_email', '__return_false' );
// Core updates add_filter( 'auto_core_update_send_email', 'wpb_stop_auto_update_emails', 10, 4 ); function wpb_stop_update_emails( $send, $type, $core_update, $result ) { if ( ! empty( $type ) && $type == 'success' ) { return false; } return true; } // Plugins updates add_filter( 'auto_plugin_update_send_email', '__return_false' ); //Themes updates add_filter( 'auto_theme_update_send_email', '__return_false' );
// Core updates
add_filter( 'auto_core_update_send_email', 'wpb_stop_auto_update_emails', 10, 4 );
 
function wpb_stop_update_emails( $send, $type, $core_update, $result ) {
if ( ! empty( $type ) && $type == 'success' ) {
return false;
}
return true;
}

// Plugins updates
add_filter( 'auto_plugin_update_send_email', '__return_false' );

//Themes updates
add_filter( 'auto_theme_update_send_email', '__return_false' );
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wpc_fix_svg_size_attributes($out, $id) {
$image_url = wp_get_attachment_url($id);
$file_ext = pathinfo($image_url, PATHINFO_EXTENSION);
if (is_admin() || 'svg' !== $file_ext) {
return false;
}
return array($image_url, null, null, false);
}
add_filter('image_downsize', 'wpc_fix_svg_size_attributes', 10, 2);
function wpc_fix_svg_size_attributes($out, $id) { $image_url = wp_get_attachment_url($id); $file_ext = pathinfo($image_url, PATHINFO_EXTENSION); if (is_admin() || 'svg' !== $file_ext) { return false; } return array($image_url, null, null, false); } add_filter('image_downsize', 'wpc_fix_svg_size_attributes', 10, 2);
function wpc_fix_svg_size_attributes($out, $id) {
	$image_url = wp_get_attachment_url($id);
	$file_ext = pathinfo($image_url, PATHINFO_EXTENSION);
	if (is_admin() || 'svg' !== $file_ext) {
		return false;
	}
	return array($image_url, null, null, false);
}
add_filter('image_downsize', 'wpc_fix_svg_size_attributes', 10, 2);
Tags:
Source
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//ad ID to li menu tag
function menu_id_page($item, $args)
{
$page = get_post($args->object_id);
$item = $page->post_name;
return $item;
}
add_filter('nav_menu_item_id', 'menu_id_page', 10, 2);
//ad ID to li menu tag function menu_id_page($item, $args) { $page = get_post($args->object_id); $item = $page->post_name; return $item; } add_filter('nav_menu_item_id', 'menu_id_page', 10, 2);
//ad ID to li menu tag
function menu_id_page($item, $args)
{
  $page = get_post($args->object_id);
  $item = $page->post_name;
  return $item;
}
add_filter('nav_menu_item_id', 'menu_id_page', 10, 2);
Tags:
Source
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/**
* Reusable Blocks accessible in backend
* @link https://www.billerickson.net/reusable-blocks-accessible-in-wordpress-admin-area
*
*/
function be_reusable_blocks_admin_menu() {
add_menu_page( 'Reusable Blocks', 'Reusable Blocks', 'edit_posts', 'edit.php?post_type=wp_block', '', 'dashicons-editor-table', 22 );
}
add_action( 'admin_menu', 'be_reusable_blocks_admin_menu' );
/** * Reusable Blocks accessible in backend * @link https://www.billerickson.net/reusable-blocks-accessible-in-wordpress-admin-area * */ function be_reusable_blocks_admin_menu() { add_menu_page( 'Reusable Blocks', 'Reusable Blocks', 'edit_posts', 'edit.php?post_type=wp_block', '', 'dashicons-editor-table', 22 ); } add_action( 'admin_menu', 'be_reusable_blocks_admin_menu' );
/**
 * Reusable Blocks accessible in backend
 * @link https://www.billerickson.net/reusable-blocks-accessible-in-wordpress-admin-area
 *
 */
function be_reusable_blocks_admin_menu() {
    add_menu_page( 'Reusable Blocks', 'Reusable Blocks', 'edit_posts', 'edit.php?post_type=wp_block', '', 'dashicons-editor-table', 22 );
}
add_action( 'admin_menu', 'be_reusable_blocks_admin_menu' );
Tags:
Source
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// into a separated js files
wp.domReady( () => {
//Register 2 styles of headings
wp.blocks.registerBlockStyle( 'core/heading', [
{
name: 'default',
label: 'Default',
isDefault: true
},
{
name: 'display',
label: 'Display'
}
]);
} );
// into a separated js files wp.domReady( () => { //Register 2 styles of headings wp.blocks.registerBlockStyle( 'core/heading', [ { name: 'default', label: 'Default', isDefault: true }, { name: 'display', label: 'Display' } ]); } );
// into a separated js files
wp.domReady( () => {
  //Register 2 styles of headings
  wp.blocks.registerBlockStyle( 'core/heading', [ 
    {
      name: 'default',
      label: 'Default',
      isDefault: true
    },
    {
      name: 'display',
      label: 'Display'
    }
  ]);
} );
Tags: , ,
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// into any page or template
$orig_post = $post;
global $post;
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=> 2, // Number of related posts that will be shown.
'ignore_sticky_posts'=>1
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) { ?>
<h4>Related posts</h4>
<?php
while( $my_query->have_posts() ) {
$my_query->the_post();
get_template_part( '/partials/article-card' );
} ?>
<?php
}
}
$post = $orig_post;
wp_reset_postdata();
// into any page or template $orig_post = $post; global $post; $categories = get_the_category($post->ID); if ($categories) { $category_ids = array(); foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id; $args=array( 'category__in' => $category_ids, 'post__not_in' => array($post->ID), 'posts_per_page'=> 2, // Number of related posts that will be shown. 'ignore_sticky_posts'=>1 ); $my_query = new wp_query( $args ); if( $my_query->have_posts() ) { ?> <h4>Related posts</h4> <?php while( $my_query->have_posts() ) { $my_query->the_post(); get_template_part( '/partials/article-card' ); } ?> <?php } } $post = $orig_post; wp_reset_postdata();
// into any page or template
$orig_post = $post;
global $post;
$categories = get_the_category($post->ID);
if ($categories) {
  $category_ids = array();
  foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
  $args=array(
    'category__in' => $category_ids,
    'post__not_in' => array($post->ID),
    'posts_per_page'=> 2, // Number of related posts that will be shown.
    'ignore_sticky_posts'=>1
  );

  $my_query = new wp_query( $args );
  if( $my_query->have_posts() ) { ?>
    <h4>Related posts</h4>
    <?php
    while( $my_query->have_posts() ) {
      $my_query->the_post();
      get_template_part( '/partials/article-card' );
    } ?>
    <?php	
  }
}
$post = $orig_post;
wp_reset_postdata();
Source
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//functions.php
function sp_wp_nav_menu_objects( $items, $args ) {
// loop
foreach( $items as &$item ) {
// vars
$icon = get_field('call-to-action_option', $item);
// append icon
if( $icon ) {
$item->classes[] = 'cta-btn';
}
}
// return
return $items;
}
add_filter('wp_nav_menu_objects', 'sp_wp_nav_menu_objects', 10, 2);
//functions.php function sp_wp_nav_menu_objects( $items, $args ) { // loop foreach( $items as &$item ) { // vars $icon = get_field('call-to-action_option', $item); // append icon if( $icon ) { $item->classes[] = 'cta-btn'; } } // return return $items; } add_filter('wp_nav_menu_objects', 'sp_wp_nav_menu_objects', 10, 2);
//functions.php
function sp_wp_nav_menu_objects( $items, $args ) {
  // loop
  foreach( $items as &$item ) {
    // vars
    $icon = get_field('call-to-action_option', $item);
    // append icon
    if( $icon ) {
      $item->classes[] = 'cta-btn';
    }
  }
  // return
  return $items;
}
add_filter('wp_nav_menu_objects', 'sp_wp_nav_menu_objects', 10, 2);
Tags: ,
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// functions.php
function luckywp_output( $args ) {
$args = array(
'container' => 'nav',
'container_class' => 'main-menu',
'items_wrap' => '<ul>%3$s</ul>',
'menu_id' => '',
'menu_class' => ''
);
return $args;
}
// add the filter
add_filter( 'lwpamf_wp_nav_menu_args', ' luckywp_output', 10, 2 );
// functions.php function luckywp_output( $args ) { $args = array( 'container' => 'nav', 'container_class' => 'main-menu', 'items_wrap' => '<ul>%3$s</ul>', 'menu_id' => '', 'menu_class' => '' ); return $args; } // add the filter add_filter( 'lwpamf_wp_nav_menu_args', ' luckywp_output', 10, 2 );
// functions.php
function luckywp_output( $args ) {
  $args = array(
    'container' => 'nav',
    'container_class'	=> 'main-menu',
    'items_wrap' => '<ul>%3$s</ul>',
    'menu_id'	=> '',
    'menu_class' => ''
  );
  return $args;
}
// add the filter 
add_filter( 'lwpamf_wp_nav_menu_args', ' luckywp_output', 10, 2 ); 
Tags: ,
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
$tags = get_tags();
if ( $tags ) :
foreach ( $tags as $tag ) : ?>
<li><a href="<?php echo esc_url( get_tag_link( $tag->term_id ) ); ?>" title="<?php echo esc_attr( $tag->name ); ?>"><?php echo esc_html( $tag->name ); ?></a></li>
<?php endforeach; ?>
<?php endif; ?>
<?php $tags = get_tags(); if ( $tags ) : foreach ( $tags as $tag ) : ?> <li><a href="<?php echo esc_url( get_tag_link( $tag->term_id ) ); ?>" title="<?php echo esc_attr( $tag->name ); ?>"><?php echo esc_html( $tag->name ); ?></a></li> <?php endforeach; ?> <?php endif; ?>
<?php
$tags = get_tags();
if ( $tags ) :
   foreach ( $tags as $tag ) : ?>
    <li><a href="<?php echo esc_url( get_tag_link( $tag->term_id ) ); ?>" title="<?php echo esc_attr( $tag->name ); ?>"><?php echo esc_html( $tag->name ); ?></a></li>
   <?php endforeach; ?>
<?php endif; ?>
Tags:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// functions.php
// Will only leave Image, Paragraph, Heading and List blocks
function misha_allowed_block_types( $allowed_blocks, $post ) {
$allowed_blocks = array(
'core/image',
'core/paragraph',
'core/heading',
'core/list'
);
// Only for Page post type
if( $post->post_type === 'page' ) {
$allowed_blocks[] = 'core/shortcode';
}
return $allowed_blocks;
}
add_filter( 'allowed_block_types', 'misha_allowed_block_types', 10, 2 );
// functions.php // Will only leave Image, Paragraph, Heading and List blocks function misha_allowed_block_types( $allowed_blocks, $post ) { $allowed_blocks = array( 'core/image', 'core/paragraph', 'core/heading', 'core/list' ); // Only for Page post type if( $post->post_type === 'page' ) { $allowed_blocks[] = 'core/shortcode'; } return $allowed_blocks; } add_filter( 'allowed_block_types', 'misha_allowed_block_types', 10, 2 );
// functions.php
// Will only leave Image, Paragraph, Heading and List blocks 
function misha_allowed_block_types( $allowed_blocks, $post ) {
  $allowed_blocks = array(
    'core/image',
    'core/paragraph',
    'core/heading',
    'core/list'
  );
  // Only for Page post type
  if( $post->post_type === 'page' ) {
    $allowed_blocks[] = 'core/shortcode';
  }
  return $allowed_blocks;
}
add_filter( 'allowed_block_types', 'misha_allowed_block_types', 10, 2 );

Tags:
Source

Categories

Tags