Must-have code pieces for WordPress Users
There is hardly anyone who uses WordPress and does not need plugins; If someone said that I really used WordPress, I made a transaction on the panel. Because for WordPress, which is an open source sub-software, millions of plugins, applications, code derivatives and solutions that solve the current situation have been produced that serve millions of alternative purposes.
In this article, we will talk about code pieces called “Code Snippet” that can be simple but vital for WordPress users by manually adding them to the theme and core files. Hoping that these pieces of code added to the system for archival purposes will be useful to all of us.
1. Allow users in the Contributor/Contributor role to upload images
Except for Author and Editor (not counting admins), users' ability to upload images is unfortunately not available in the default WordPress core role models. With this small piece of code, you can add images to your members with Content Provider role by adding them to your theme's functions.php file.
if ( current_user_can('contributor') && !current_user_can('upload_files') ) add_action('admin_init', 'TP_low_contribu); function allow_contributor_uploads() { $contributor = get_role('contributor'); $contributor->add_cap('upload_files'); }
2. Show Popular Posts on Your WordPress Single Posts
You should definitely check if this feature is available in the WordPress theme, but if it is not, you either use a plugin or look for remedies to show the posts that are very popular with your theme with such manual coding. With this piece of code, you can automatically show popular posts to users by interfering with your theme's functions.php file.
function count_post_visits() { if( is_single() ) { global $post; $views = get_post_meta( $post->ID, 'my_post_viewed', true ); if( $views == '' ) { update_post_meta( $post->ID, 'my_post_viewed', '1' ); } else { $views_no = intval( $views ); update_post_meta( $post->ID, 'my_post_viewed', ++$views_no ); } } } add_action( 'wp_head', 'count_post_visits' );
With the coding, the data of popular posts is now being drawn in your theme, you can show them by adding these codes exactly where you want in your post in your theme.
$popular_posts_args = array( 'posts_per_page' => 3, 'meta_key' => 'my_post_viewed', 'orderby' => 'meta_value_num', 'order'=> 'DESC' ); $popular_posts_loop = new WP_Query( $popular_posts_args ); while( $popular_posts_loop->have_posts() ): $popular_posts_loop->the_post(); // Loop continues endwhile; wp_reset_query();
3. Turning Off WordPress Search Function
In order not to make an excuse to fight our malicious friends who take advantage of Query vulnerabilities along with many query methods, the WordPress core cancel the basic search function you can. Leveraging the results of search queries and search function vulnerability using attack attempts into the functions.php file a small piece of code We can prevent it by adding
function fb_filter_query( $query, $error = true ) {
if ( is_search() ) {
$query->is_search = false;
$query->query_vars[s] = false;
$query->query[s] = false;
// to error
if ( $error == true )
$query->is_404 = true;
}
}
add_action( 'parse_query', 'fb_filter_query' );
add_filter( 'get_search_form', create_function( '$a', "return null;" ) );
4. Protect Your Site From Suspicious Initiatives
If you use WordPress, it takes more than a firewall to protect your website; You have to make it much safer. Because while it can stabilize the system with at least five plugins and one theme, the thing that scares us is that each plugin or theme component accompanies virus distribution, and viral distribution is much faster, especially in the second-hand market. WordPress suspicious login activity It would be helpful to add a little code to prevent it.
Again, you can try to minimize suspicious activity by adding an appropriate addition to our theme's functions.php file.
global $user_ID; if($user_ID) { if(!current_user_can('administrator')) { if (strlen($_SERVER['REQUEST_URI']) > 255 || stripos($TPUESTURT39;") | stripos($_SERVER['REQUEST_URI'], "CONCAT") || stripos($_SERVER['REQUEST_URI'], "UNION+SELECT") || stripos($_SERVER"; @header("HTTP/1.1 414 Request-URI Too Long"); @header("Status: 414 Request-URI Too Long"); @header("Connection: Close"); @exit; } } }
5. Paginate your site without a plugin
By adding more of the "Forward" and "Back" buttons, which are generally used and mostly used by plugins, to our site with the help of a small code snippet, pagination and transitions can be easily changed. WordPress pagination easily We can provide it using a code snippet. Of course, we still use your theme's functions.php file.
global $wp_query; $total = $wp_query->max_num_pages; // only bother with the rest if we have more than 1 page! if ( $total > 1 ) { // get the current page if ( !$current_page = get_query_var('paged') ) $current_page = 1; // structure of "format" depends on whether we're using pretty permalinks $format = empty( get_option('permalink_structure') ) ? '&page=%#%' : 'page/%#%/'; echo paginate_links(array( 'base' => get_pagenum_link(1) . '%_%', 'format' => $format, 'current' => $current_page, 'total' => $total, 'mid_size' = > 4, 'type' => 'list' )); }
6. Hide the WordPress Admin Bar
When logging into the site with any role, if it is not blocked by your theme WordPress-only system admin bar above are available to users. This means users have access to the background, rather than foreground usage. It can be incredibly risky, especially if WordPress core permissions are not set and there is something missing in role tasks. So whatever we do, if it's not very essential Hide WordPress admin bar Let's apply it on our site without forgetting the process.
/ Remove the admin bar from the front end add_filter( 'show_admin_bar', '__return_false' );
7. Displaying Post Cover Images in RSS Feed
If you are sharing the data feed of your WordPress site with different platforms via RSS Feed, this piece of code will benefit you. Having an image makes a significant difference, especially when broadcasting in applications with discover platforms such as Google News. Again, we add our codes by accessing the functions.php file.
// Put post thumbnails into rss feed function wpfme_feed_post_thumbnail($content) { global $post; if(has_post_thumbnail($post->ID)) { $content = '' . $content; } return $content; } add_filter('the_excerpt_rss', 'wpfme_feed_post_thumbnail'); add_filter('the_content_feed', 'wpfme_feed_post_thumbnail');
8. Change WordPress Author Link Structure
Another feature that comes with the WordPress core and is very likely hosted in every theme can be open to you. As we intervened in the search results, the "link", which has a pure link structure,margarit.com.tr/author/authornameThe ” link display is the focus of auto-type attack attempts. Therefore, by arranging this structure according to ourselves, we can ensure that we are comfortable. The standard "/author/authorname' for example 'margarit.com.tr/authors/authorname” to change the code below to our theme. functions.php add it to the page.
add_action('init', 'cng_author_base'); function cng_author_base() { global $wp_rewrite; $author_slug = 'authors'; // change slug name $wp_rewrite->author_base = $author_slug; }
9. Automatic Twitter Usernames Tagging and Defining
If your website hosts a lot of Twitter content or you get a lot of social feedback from this platform, this piece of code can spice up your site. with @ character assignment @margarit), any tagging or mentioning that is written together will automatically turn into a link to the account name on Twitter, providing a nice redirect for users. We are making additions to our functions.php file to activate the feature.
function content_twitter_mention($content) { return preg_replace('/([^a-zA-Z0-9-_&])@([0-9a-zA-Z_]+)/', "$1<a href="http://twitter.com/$2" target="_blank" rel="nofollow">@$2</a>", $content); } add_filter('the_content', 'content_twitter_mention'); add_filter('comment_text', 'content_twitter_mention');