Search results for: admin

Change "Howdy Admin" in Admin Bar

Customize the "Howdy" message in the admin bar.

Use Snippet
php
<?php
function wpcode_snippet_replace_howdy( $wp_admin_bar ) {
	// Edit the line below to set what you want the admin bar to display intead of "Howdy,".
	$new_howdy = 'Welcome,';
	$my_account = $wp_admin_bar->get_node( 'my-account' );
	$wp_admin_bar->add_node(
		array(
			'id'    => 'my-account',
			'title' => str_replace( 'Howdy,', $new_howdy, $my_account->title ),
		)
	);
}
add_filter( 'admin_bar_menu', 'wpcode_snippet_replace_howdy', 25 );
WPCode Team
700+

Completely Disable Comments

Disable comments for all post types, in the admin and the frontend.

Use Snippet
php
<?php
add_action('admin_init', function () {
    // Redirect any user trying to access comments page
    global $pagenow;
    
    if ($pagenow === 'edit-comments.php') {
        wp_safe_redirect(admin_url());
        exit;
    }
    // Remove comments metabox from dashboard
    remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
    // Disable support for comments and trackbacks in post types
    foreach (get_post_types() as $post_type) {
        if (post_type_supports($post_type, 'comments')) {
            remove_post_type_support($post_type, 'comments');
            remove_post_type_support($post_type, 'trackbacks');
        }
    }
});
// Close comments on the front-end
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);
// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);
// Remove comments page in menu
add_action('admin_menu', function () {
    remove_menu_page('edit-comments.php');
});
// Remove comments links from admin bar
add_action('init', function () {
    if (is_admin_bar_showing()) {
        remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
    }
});
WPCode Team
200+

Disable The WP Admin Bar

Hide the WordPress Admin Bar for all users in the frontend.

Use Snippet
php
<?php
/* Disable WordPress Admin Bar for all users */
add_filter( 'show_admin_bar', '__return_false' );
WPCode Team
1.4k

Change Admin Panel Footer Text

Display custom text in the admin panel footer with this snippet.

Use Snippet
php
<?php
add_filter(
	'admin_footer_text',
	function ( $footer_text ) {
		// Edit the line below to customize the footer text.
		$footer_text = 'Powered by <a href="https://www.wordpress.org" target="_blank" rel="noopener">WordPress</a> | WordPress Tutorials: <a href="https://www.wpbeginner.com" target="_blank" rel="noopener">WPBeginner</a>';
		
		return $footer_text;
	}
);
WPCode Team
900+

Disable Search

Completely disable search on your WordPress website.

Use Snippet
php
<?php
// Prevent search queries.
add_action(
	'parse_query',
	function ( $query, $error = true ) {
		if ( is_search() && ! is_admin() ) {
			$query->is_search       = false;
			$query->query_vars['s'] = false;
			$query->query['s']      = false;
			if ( true === $error ) {
				$query->is_404 = true;
			}
		}
	},
	15,
	2
);
// Remove the Search Widget.
add_action(
	'widgets_init',
	function () {
		unregister_widget( 'WP_Widget_Search' );
	}
);
// Remove the search form.
add_filter( 'get_search_form', '__return_empty_string', 999 );
// Remove the core search block.
add_action(
	'init',
	function () {
		if ( ! function_exists( 'unregister_block_type' ) || ! class_exists( 'WP_Block_Type_Registry' ) ) {
			return;
		}
		$block = 'core/search';
		if ( WP_Block_Type_Registry::get_instance()->is_registered( $block ) ) {
			unregister_block_type( $block );
		}
	}
);
// Remove admin bar menu search box.
add_action(
	'admin_bar_menu',
	function ( $wp_admin_bar ) {
		$wp_admin_bar->remove_menu( 'search' );
	},
	11
);
WPCode Team
400+

Duplicate Post/Page Link

Duplicate posts, pages or any post type with 1-click by adding a duplicate link in...

Use Snippet
php
<?php
// Add duplicate button to post/page list of actions.
add_filter( 'post_row_actions', 'wpcode_snippet_duplicate_post_link', 10, 2 );
add_filter( 'page_row_actions', 'wpcode_snippet_duplicate_post_link', 10, 2 );
// Let's make sure the function doesn't already exist.
if ( ! function_exists( 'wpcode_snippet_duplicate_post_link' ) ) {
	/**
	 * @param array   $actions The actions added as links to the admin.
	 * @param WP_Post $post The post object.
	 *
	 * @return array
	 */
	function wpcode_snippet_duplicate_post_link( $actions, $post ) {
		// Don't add action if the current user can't create posts of this post type.
		$post_type_object = get_post_type_object( $post->post_type );
		if ( null === $post_type_object || ! current_user_can( $post_type_object->cap->create_posts ) ) {
			return $actions;
		}
		$url = wp_nonce_url(
			add_query_arg(
				array(
					'action'  => 'wpcode_snippet_duplicate_post',
					'post_id' => $post->ID,
				),
				'admin.php'
			),
			'wpcode_duplicate_post_' . $post->ID,
			'wpcode_duplicate_nonce'
		);
		$actions['wpcode_duplicate'] = '<a href="' . $url . '" title="Duplicate item" rel="permalink">Duplicate</a>';
		return $actions;
	}
}
/**
 * Handle the custom action when clicking the button we added above.
 */
add_action( 'admin_action_wpcode_snippet_duplicate_post', function () {
	if ( empty( $_GET['post_id'] ) ) {
		wp_die( 'No post id set for the duplicate action.' );
	}
	$post_id = absint( $_GET['post_id'] );
	// Check the nonce specific to the post we are duplicating.
	if ( ! isset( $_GET['wpcode_duplicate_nonce'] ) || ! wp_verify_nonce( $_GET['wpcode_duplicate_nonce'], 'wpcode_duplicate_post_' . $post_id ) ) {
		// Display a message if the nonce is invalid, may it expired.
		wp_die( 'The link you followed has expired, please try again.' );
	}
	// Load the post we want to duplicate.
	$post = get_post( $post_id );
	// Create a new post data array from the post loaded.
	if ( $post ) {
		$current_user = wp_get_current_user();
		$new_post     = array(
			'comment_status' => $post->comment_status,
			'menu_order'     => $post->menu_order,
			'ping_status'    => $post->ping_status,
			'post_author'    => $current_user->ID,
			'post_content'   => $post->post_content,
			'post_excerpt'   => $post->post_excerpt,
			'post_name'      => $post->post_name,
			'post_parent'    => $post->post_parent,
			'post_password'  => $post->post_password,
			'post_status'    => 'draft',
			'post_title'     => $post->post_title . ' (copy)',// Add "(copy)" to the title.
			'post_type'      => $post->post_type,
			'to_ping'        => $post->to_ping,
		);
		// Create the new post
		$duplicate_id = wp_insert_post( $new_post );
		// Copy the taxonomy terms.
		$taxonomies = get_object_taxonomies( get_post_type( $post ) );
		if ( $taxonomies ) {
			foreach ( $taxonomies as $taxonomy ) {
				$post_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) );
				wp_set_object_terms( $duplicate_id, $post_terms, $taxonomy );
			}
		}
		// Copy all the custom fields.
		$post_meta = get_post_meta( $post_id );
		if ( $post_meta ) {
			foreach ( $post_meta as $meta_key => $meta_values ) {
				if ( '_wp_old_slug' === $meta_key ) { // skip old slug.
					continue;
				}
				foreach ( $meta_values as $meta_value ) {
					add_post_meta( $duplicate_id, $meta_key, $meta_value );
				}
			}
		}
		// Redirect to edit the new post.
		wp_safe_redirect(
			add_query_arg(
				array(
					'action' => 'edit',
					'post'   => $duplicate_id
				),
				admin_url( 'post.php' )
			)
		);
		exit;
	} else {
		wp_die( 'Error loading post for duplication, please try again.' );
	}
} );
WPCode Team
<10

Disable Gutenberg Code Editing for Non-Admin Users

Prevent non-admin users from using "Edit as HTML" or "Code editor" in the Gutenberg Editor.

Use Snippet
php
<?php
add_filter( 'block_editor_settings_all', function ( $settings ) {
	
	$settings['codeEditingEnabled'] = current_user_can( 'manage_options' );
	return $settings;
} );
WPCode Team
<10

Disable New User Notifications

Prevent the admin user from getting new user notification emails.

Use Snippet
php
<?php
function wpcode_send_new_user_notifications( $user_id, $notify = 'user' ) {
	if ( empty( $notify ) || 'admin' === $notify ) {
		return;
	} elseif ( 'both' === $notify ) {
		// Send new users the email but not the admin.
		$notify = 'user';
	}
	wp_send_new_user_notifications( $user_id, $notify );
}
add_action(
	'init',
	function () {
		// Disable default email notifications.
		remove_action( 'register_new_user', 'wp_send_new_user_notifications' );
		remove_action( 'edit_user_created_user', 'wp_send_new_user_notifications' );
		// Replace with custom function that only sends to user.
		add_action( 'register_new_user', 'wpcode_send_new_user_notifications' );
		add_action( 'edit_user_created_user', 'wpcode_send_new_user_notifications', 10, 2 );
	}
);
WPCode Team
<10

Remove Dashboard Welcome Panel

Hide the Welcome Panel on the WordPress dashboard for all users.

Use Snippet
php
<?php
add_action(
	'admin_init',
	function () {
		remove_action( 'welcome_panel', 'wp_welcome_panel' );
	}
);
WPCode Team
500+

Disable Emojis

Disable Emoji's in WordPress to improve your site's performance

Use Snippet
php
<?php
/**
 * Disable the emojis in WordPress.
 */
add_action( 'init', function () {
	remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
	remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
	remove_action( 'wp_print_styles', 'print_emoji_styles' );
	remove_action( 'admin_print_styles', 'print_emoji_styles' );
	remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
	remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
	remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
	// Remove from TinyMCE.
	add_filter( 'tiny_mce_plugins', function ( $plugins ) {
		if ( is_array( $plugins ) ) {
			return array_diff( $plugins, array( 'wpemoji' ) );
		} else {
			return array();
		}
	} );
	// Remove from dns-prefetch.
	add_filter( 'wp_resource_hints', function ( $urls, $relation_type ) {
		if ( 'dns-prefetch' === $relation_type ) {
			$emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' );
			$urls          = array_diff( $urls, array( $emoji_svg_url ) );
		}
		return $urls;
	}, 10, 2 );
} );
WPCode Team
<10

Hide ‘Screen Options’ Tab

Remove the Screen Options menu at the top of admin pages.

Use Snippet
php
<?php
// Hide admin 'Screen Options' tab
add_filter('screen_options_show_screen', '__return_false');
WPCode Team
100+

Replace WordPress Logo on Login Page

Use your custom logo on the default login page, don't forget to edit the snippet...

Use Snippet
php
<?php
add_filter( 'login_head', function () {
	// Update the line below with the URL to your own logo.
	// Adjust the Width & Height accordingly.
	$custom_logo = 'https://wpcode.com/wp-admin/images/wordpress-logo.svg';
	$logo_width  = 84;
	$logo_height = 84;
	printf(
		'<style>.login h1 a {background-image:url(%1$s) !important; margin:0 auto; width: %2$spx; height: %3$spx; background-size: 100%%;}</style>',
		$custom_logo,
		$logo_width,
		$logo_height
	);
}, 990 );
WPCode Team
<10

Remove Query Strings From Static Files

Use this snippet to remove query string from CSS & JS files and improve performance...

Use Snippet
php
<?php
function wpcode_snippet_remove_query_strings_split( $src ) {
	$output = preg_split( "/(&ver|?ver)/", $src );
	return $output ? $output[0] : '';
}
add_action( 'init', function () {
	if ( ! is_admin() ) {
		add_filter( 'script_loader_src', 'wpcode_snippet_remove_query_strings_split', 15 );
		add_filter( 'style_loader_src', 'wpcode_snippet_remove_query_strings_split', 15 );
	}
} );
WPCode Team
<10

Set a Minimum Word Count for Posts

Force your authors to write posts that have a minimum length.

Use Snippet
php
<?php
/**
 * Prevent publishing posts under a minimum number of words.
 *
 * @param int     $post_id The id of the post.
 * @param WP_Post $post The post object.
 *
 * @return void
 */
function wpcode_snippet_publish_min_words( $post_id, $post ) {
	// Edit the line below to set the desired minimum number of words.
	$word_count = 100;
	if ( str_word_count( $post->post_content ) < $word_count ) {
		wp_die(
			sprintf(
				// Translators: placeholder adds the minimum number of words.
				esc_html__( 'The post content is below the minimum word count. Your post needs to have at least %d words to be published.' ),
				absint( $word_count )
			)
		);
	}
}
add_action( 'publish_post', 'wpcode_snippet_publish_min_words', 9, 2 );
WPCode Team
90+

Set oEmbed Max Width

Set a max width for the embeds using oEmbed in the content.

Use Snippet
php
<?php
function wpcode_snippet_oembed_defaults( $sizes ) {
	return array(
		'width'  => 400,
		'height' => 280,
	);
}
add_filter( 'embed_defaults', 'wpcode_snippet_oembed_defaults' );
WPCode Team
60+
1 2