Skip to content

Instantly share code, notes, and snippets.

@donini
donini / 1-restrict-data-in-list-by-user-role.php
Last active September 3, 2019 02:34
Restrict data in lists by user role
<?php
add_action('pre_get_posts', 'filter_posts_list');
function filter_posts_list($query)
{
global $pagenow;
global $current_user;
get_currentuserinfo();
@donini
donini / 4-custom-column-in-lists.php
Last active September 3, 2019 17:02
Custom columns in posts lists
<?php
add_action( 'manage_posts_custom_column' , 'custom_columns', 10, 2 );
add_action( 'manage_store_posts_columns' , 'add_store_columns', 10, 2 );
/**
** FOR CUSTOM POST TYPE USE:
** manage_{$post_type}_posts_custom_column
** manage_{$post_type}_posts_columns
*/
@donini
donini / 6-extending-post-save.php
Last active September 3, 2019 17:26
Override/extend post save
<?php
add_action( 'save_post', 'update_post_title' );
function update_post_title( $post_id ){
global $wpdb;
$post_type = get_post_type( $post_id );
$title = '';
if ( $post_type === 'store' ) {
$name = get_post_meta( $post_id, 'name', true );
@donini
donini / 5-add-custom-buttons-in-lists.php
Last active September 3, 2019 17:13
Add custom buttons in posts lists
<?php
add_action( 'restrict_manage_users', 'customized_filters' );
add_action( 'restrict_manage_posts', 'customized_filters' );
function customized_filters()
{
?>
<input id="post-query-export" class="button" type="button" value="Export CSV list" name="" onclick="document.location.href='<?php echo get_stylesheet_directory_uri()?>/csv/export.php';">
<?php
}
@donini
donini / 3-change-from-mail.php
Last active September 3, 2019 02:47
Change from name
<?php
/** notification mail email change.
**
** @param string $mail Custom e-mail address.
**/
add_filter('wp_mail_from','custom_email_from');
function custom_email_from($mail) {
$mail = '[YOURMAIL@YOURSITE.COM]';
return $mail;
@donini
donini / 2-change-from-name.php
Last active September 3, 2019 02:40
Change from name
<?php
/** Set a custom from name.
**
** @param string $name Custom name.
*/
add_filter('wp_mail_from_name','custom_email_from_name');
function custom_email_from_name($name) {
$name = 'My Site Name';
return $name;
@donini
donini / limit-excerpt-length.php
Last active September 24, 2016 03:08
Limit excerpt length
<?php
function getExcerpt($str, $startPos=0, $maxLength=100) {
if(strlen($str) > $maxLength) {
$excerpt = substr($str, $startPos, $maxLength-3);
$lastSpace = strrpos($excerpt, ' ');
$excerpt = substr($excerpt, 0, $lastSpace);
$excerpt .= '...';
} else {
$excerpt = $str;
}
@donini
donini / using-different-sizes-of-images.php
Created September 24, 2016 03:09
Using different sizes of images
<?php
add_theme_support( 'post-thumbnails' );
add_image_size( 'description-thumbnail', '385', '224' );
add_image_size( 'description-medium', '385', '224' );
add_image_size( 'description-large', '385', '224' );
?>
@donini
donini / 7-add-new-taxonomy-filter.php
Last active September 7, 2019 20:57
7-add-new-taxonomy-filter.php
<?php
add_action( 'restrict_manage_posts', 'filter_posts_by_topics', 10, 2 );
function filter_posts_by_topics( $post_type, $which ) {
/* Apply this only on a specific post type */
if ( 'post' !== $post_type ) {
return;
}
@donini
donini / 8-hide-wordpress-updates.php
Created September 7, 2019 21:08
8-hide-wordpress-updates.php
<?php
add_filter( 'pre_site_transient_update_core', 'remove_core_updates' ); /* Hide WordPress Core updates */
add_filter( 'pre_site_transient_update_plugins', 'remove_core_updates' ); /* Hide Plugins updates */
add_filter( 'pre_site_transient_update_themes', 'remove_core_updates' ); /* Hide Theme updates */
/* Hide update notifications */
function remove_core_updates(){
global $wp_version;
return (object) array(