Internet

Codes Required for Your WordPress Site

We wanted to share useful codes for your WordPress site:

Kodları kullandığınız temanın functions.php dosyasının en altında bulunan ?> den hemen önceki satıra eklemelisiniz.

/* Admin Bar Hide */
  show_admin_bar(false);
  • You can hide Admin Bar from all users using the code above.

/* Admin Bar hiding for all users except admin */
add_action('after_setup_theme', 'remove_admin_bar');

function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
  show_admin_bar(false);
}
}
  • You can hide Admin Bar for all users except Admin using the code above.

if( !function_exists('custom_user_login_redirect') ) {
function custom_user_login_redirect() {
$redirect_to = 'https://www.site.com/';
return $redirect_to;
}
add_filter('login_redirect','custom_user_login_redirect',10,3);
}
  • If you are using the Buddypress plugin, you can redirect it to the home page when your members log in thanks to the code above.

add_action('init', function(){
   global $wp_rewrite;
        $wp_rewrite->pagination_base = 'page';
	$wp_rewrite->author_base = 'author';
        $wp_rewrite->flush_rules();
});
  • Using the code above, you can make the /author and /page extension in your language.

function remove_email_url($fields) {
    unset($fields['url']);
    unset($fields['email']);
    return $fields;
}

add_filter('comment_form_default_fields', 'remove_email_url');
  • With the code above, you can remove the Email and URL fields from the WordPress comment field. If you just want to remove the URL field, unset ($ fields [’email’]); Delete the place where it says.

add_filter('use_block_editor_for_post', '__return_false');
  • You can use the above code to use the WordPress old editor.

// Function to change email address

function wpb_sender_email( $original_email_address ) {
    return '[email protected]';
}

// Function to change sender name
function wpb_sender_name( $original_email_from ) {
	return 'Site Name';
}

// Hooking up our functions to WordPress filters 
add_filter( 'wp_mail_from', 'wpb_sender_email' );
add_filter( 'wp_mail_from_name', 'wpb_sender_name' );
  • You can change the WordPress post address with the above code.

/*Remove WordPress menu from admin bar*/
add_action( 'admin_bar_menu', 'remove_wp_logo', 999 );
function remove_wp_logo( $wp_admin_bar ) {
	$wp_admin_bar->remove_node( 'wp-logo' );
}
  • Use the above code to remove the WordPress menu from the Admin Bar.

// Changing the WordPress login page logo
function custom_loginlogo() {
echo '<style type="text/css"> 
body.login div#login h1 a {
background-image: url(https://www.site.com/wp-content/uploads/2020/01/logo.png);
}
.login h1 a {
background-size: 300px;
width: 300px;
height: 75px;
</style>';
}
add_action('login_head', 'custom_loginlogo');

add_filter( 'login_headerurl', 'custom_loginlogo_url' );
function custom_loginlogo_url($url) {
    return 'https://www.site.com';
}

function my_login_logo_url_title() {
  return 'Site Name';
}
add_filter( 'login_headertitle', 'my_login_logo_url_title' );
  • Above is the code you can use to replace the login WordPress logo on the /wp-login.php page with your own logo.

//jQuery Google Library
function replace_jquery() {
 if (!is_admin()) {
 // comment out the next two lines to load the local copy of jQuery
 wp_deregister_script('jquery');
 wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js', false, '1.11.3');
 wp_enqueue_script('jquery');
 }
}
add_action('init', 'replace_jquery');
  • You can use the above code to pull WordPress jQuery files via Google CDN. With this code, you reduce the load on the server.

// wordpress versiyonu gizleme
function complete_version_removal() {
 return '';
}
add_filter('the_generator', 'complete_version_removal');
  • You can hide version information to protect your WordPress site from exploits in past versions.

// yorumlar istenemeyen & sil linkleri ekleme
function delete_comment_link($id) {
 if (current_user_can('edit_post')) {
 echo '| <a href="'.get_bloginfo('wpurl').'/wp-admin/comment.php?action=cdc&c='.$id.'">del</a> ';
 echo '| <a href="'.get_bloginfo('wpurl').'/wp-admin/comment.php?action=cdc&dt=spam&c='.$id.'">spam</a>';
 }
}
  • In WordPress, you can add unwanted and delete links to the comments on the page you share without entering the Admin Panel.

/**
 * Restrict previews on functions unless specified in wp-config.php
 */
if (!defined('WP_POST_REVISIONS')) define('WP_POST_REVISIONS', 5);
  • When you preview while writing a post it creates a record in the database. You can limit this to avoid burdening the database.

function ajx_sharpen_resized_files( $resized_file ) {
$image = wp_load_image( $resized_file );
if ( !is_resource( $image ) )
return new WP_Error( 'error_loading_image', $image, $file );
$size = @getimagesize( $resized_file );
if ( !$size )
return new WP_Error('invalid_image', __('Could not read image size'), $file);
list($orig_w, $orig_h, $orig_type) = $size;
switch ( $orig_type ) {
case IMAGETYPE_JPEG:
$matrix = array(
array(-1, -1, -1),
array(-1, 16, -1),
array(-1, -1, -1),
);
$divisor = array_sum(array_map('array_sum', $matrix));
$offset = 0; 
imageconvolution($image, $matrix, $divisor, $offset);
imagejpeg($image, $resized_file,apply_filters( 'jpeg_quality', 90, 'edit_image' ));
break;
case IMAGETYPE_PNG:
return $resized_file;
case IMAGETYPE_GIF:
return $resized_file;
}
return $resized_file;
}   
add_filter('image_make_intermediate_size', 'ajx_sharpen_resized_files',900);
  • When you preview while writing a post it creates a record in the database. You can limit this to avoid burdening the database.

function new_excerpt_length($length) { 
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');
  • The article summary in WordPress consists of 55 words. If there is no section in your theme to change this number, you can do so thanks to the code above. You can change the number 100 to the number of words you want.

/****** Displaying Featured Images in Post/Page Tab ******/
if ( !function_exists('AddThumbColumn') && function_exists('add_theme_support') ) {
// for post and page
add_theme_support('post-thumbnails', array( 'post', 'page' ) );
function AddThumbColumn($cols) {
$cols['thumbnail'] = __('Thumbnail');
return $cols;
}
function AddThumbValue($column_name, $post_id) {
$width = (int) 35;
$height = (int) 35;
if ( 'thumbnail' == $column_name ) {
// thumbnail of WP 2.9
$thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true );
// image from gallery
$attachments = get_children( array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image') );
if ($thumbnail_id)
$thumb = wp_get_attachment_image( $thumbnail_id, array($width, $height), true );
elseif ($attachments) {
foreach ( $attachments as $attachment_id => $attachment ) {
$thumb = wp_get_attachment_image( $attachment_id, array($width, $height), true );
}
}
if ( isset($thumb) && $thumb ) {
echo $thumb;
} else {
echo __('None');
}
}
}
// for posts
add_filter( 'manage_posts_columns', 'AddThumbColumn' );
add_action( 'manage_posts_custom_column', 'AddThumbValue', 10, 2 );
// for pages
add_filter( 'manage_pages_columns', 'AddThumbColumn' );
add_action( 'manage_pages_custom_column', 'AddThumbValue', 10, 2 );
}
  • You can use the above code to show the featured images in the Posts/Pages section in the WordPress Admin Panel.

//site içi ping kapatma
function no_self_ping( &$links ) {
$home = get_option( 'home' );
foreach ( $links as $l => $link )
if ( 0 === strpos( $link, $home ) )
unset($links[$l]);
}
add_action( 'pre_ping', 'no_self_ping' );
  • When you link within the site, you will receive pingback (feedback). Use the above code to turn this off.

add_filter('get_post_metadata', function($value, $object_id, $meta_key, $single) {
if ($meta_key !== '_thumbnail_id' || $value) {
return $value;
}
preg_match('~<img[^>]+wp-image-(\\d+)~', get_post_field('post_content', $object_id), $matches);
if ($matches) {
return $matches[1];
}
return $value;
}, 10, 4);
  • Use the above code to set the first image in your posts as a featured image.

// unregister all default WP Widgets
function unregister_default_wp_widgets() {
unregister_widget('WP_Widget_Pages');
unregister_widget('WP_Widget_Calendar');
unregister_widget('WP_Widget_Archives');
unregister_widget('WP_Widget_Links');
unregister_widget('WP_Widget_Meta');
unregister_widget('WP_Widget_Search');
unregister_widget('WP_Widget_Categories');
unregister_widget('WP_Widget_Recent_Posts');
unregister_widget('WP_Widget_Recent_Comments');
unregister_widget('WP_Widget_RSS');
unregister_widget('WP_Widget_Tag_Cloud');
}
add_action('widgets_init', 'unregister_default_wp_widgets', 1);
  • When you first install WordPress, a bunch of plugins come by default. If you think these plugins won’t work for you, you can remove them.

// Admin Footer
function custom_admin_footer() {
echo 'Site Name - Description';
} 
add_filter('admin_footer_text', 'custom_admin_footer');
  • “Thanks for creating with WordPress.” in the footer section of the admin panel. You may want to change the text. You can use the above code for this.

function keep_me_logged_in_for_1_year( $expirein ) {
return 31556926; // 1 year in seconds
}
add_filter( 'auth_cookie_expiration', 'keep_me_logged_in_for_1_year' );
  • When you log in to your WordPress site, you will see that you are automatically logged out after a certain period of time. You can use this code to extend this period.

function wp_autolink_featured_images( $html, $post_id, $post_image_id ) {
If (! is_singular()) { 
$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
return $html;
} else {
return $html;
}
}
add_filter( 'post_thumbnail_html', 'wp_autolink_featured_images', 10, 3 );
  • The visual featured in some themes is not linked to the article. The solution is to use the above code.

function modify_read_more_link() {
return '<a class="more-link" href="' . get_permalink() . '">Read More</a>';
}
add_filter( 'the_content_more_link', 'modify_read_more_link' );
  • Above is the code that you can use to replace the Read More text.

add_filter( 'avatar_defaults', 'wp_yeni_gravatar' );
function wp_yeni_gravatar ($avatar_defaults) {
$myavatar = 'https://site.com/wp-content/uploads/2020/01/avatar.png';
$avatar_defaults[$myavatar] = "Default Gravatar";
return $avatar_defaults;
}
  • You can replace the default mystery man avatar with the image you uploaded yourself. After adding the above code to the functions.php file, go to Admin Panel >> Settings >> Discussion tab, select the gravatar image that will appear at the bottom and save it.

Benzer Yazılar

Leave a Comment