{"id":4250,"date":"2020-09-06T16:18:26","date_gmt":"2020-09-06T13:18:26","guid":{"rendered":"https:\/\/www.margarit.com.tr\/?p=4250"},"modified":"2020-09-10T00:46:59","modified_gmt":"2020-09-09T21:46:59","slug":"wordpress-kullanicilari-icin-olmazsa-olmaz-kod-parcalari","status":"publish","type":"post","link":"https:\/\/www.margarit.com.tr\/en\/wordpress-kullanicilari-icin-olmazsa-olmaz-kod-parcalari\/","title":{"rendered":"Must-have code pieces for WordPress Users"},"content":{"rendered":"<p>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.<\/p>\n\n\n\n<p>In this article, we will talk about code pieces called \u201cCode Snippet\u201d 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.<\/p>\n\n\n\n<h3>1. <strong>Allow users in the Contributor\/Contributor role to upload images<\/strong><\/h3>\n\n\n\n<p>Except for Author and Editor (not counting admins), users&#039; 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&#039;s functions.php file.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>if ( current_user_can(&#039;contributor&#039;) &amp;&amp; !current_user_can(&#039;upload_files&#039;) ) add_action(&#039;admin_init&#039;, &#039;TP_low_contribu); function allow_contributor_uploads() { $contributor = get_role(&#039;contributor&#039;); $contributor-&gt;add_cap(&#039;upload_files&#039;); }<\/code><\/pre><\/div>\n\n\n\n<h3><strong>2. Show Popular Posts on Your WordPress Single Posts<\/strong><\/h3>\n\n\n\n<p>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&#039;s functions.php file.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>function count_post_visits() { if( is_single() ) { global $post; $views = get_post_meta( $post-&gt;ID, &#039;my_post_viewed&#039;, true ); if( $views == &#039;&#039; ) { update_post_meta( $post-&gt;ID, &#039;my_post_viewed&#039;, &#039;1&#039; ); } else { $views_no = intval( $views ); update_post_meta( $post-&gt;ID, &#039;my_post_viewed&#039;, ++$views_no ); } } } add_action( &#039;wp_head&#039;, &#039;count_post_visits&#039; );<\/code><\/pre><\/div>\n\n\n\n<p>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.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>$popular_posts_args = array( &#039;posts_per_page&#039; =&gt; 3, &#039;meta_key&#039; =&gt; &#039;my_post_viewed&#039;, &#039;orderby&#039; =&gt; &#039;meta_value_num&#039;, &#039;order&#039;=&gt; &#039;DESC&#039; ); $popular_posts_loop = new WP_Query( $popular_posts_args ); while( $popular_posts_loop-&gt;have_posts() ): $popular_posts_loop-&gt;the_post(); \/\/ Loop continues endwhile; wp_reset_query();<\/code><\/pre><\/div>\n\n\n\n<h3>3. Turning Off WordPress Search Function<\/h3>\n\n\n\n<p>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 <strong>cancel the basic search function <\/strong>you can. Leveraging the results of search queries and <strong>search function vulnerability<\/strong> using <strong>attack attempts<\/strong> into the functions.php file <strong>a small piece of code <\/strong>We can prevent it by adding<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>function fb_filter_query( $query, $error = true ) {\nif ( is_search() ) {\n$query-&gt;is_search = false;\n$query-&gt;query_vars[s] = false;\n$query-&gt;query[s] = false;\n\/\/ to error\nif ( $error == true )\n$query-&gt;is_404 = true;\n}\n}\nadd_action( &#39;parse_query&#39;, &#39;fb_filter_query&#39; );\nadd_filter( &#39;get_search_form&#39;, create_function( &#39;$a&#39;, &quot;return null;&quot; ) );<\/code><\/pre><\/div>\n\n\n\n<h3>4. Protect Your Site From Suspicious Initiatives<\/h3>\n\n\n\n<p>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. <strong>WordPress suspicious login activity <\/strong>It would be helpful to add a little code to prevent it.<\/p>\n\n\n\n<p>Again, you can try to minimize suspicious activity by adding an appropriate addition to our theme&#039;s functions.php file.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>global $user_ID; if($user_ID) { if(!current_user_can(&#039;administrator&#039;)) { if (strlen($_SERVER[&#039;REQUEST_URI&#039;]) &gt; 255 || stripos($TPUESTURT39;&quot;) | stripos($_SERVER[&#039;REQUEST_URI&#039;], &quot;CONCAT&quot;) || stripos($_SERVER[&#039;REQUEST_URI&#039;], &quot;UNION+SELECT&quot;) || stripos($_SERVER&quot;; @header(&quot;HTTP\/1.1 414 Request-URI Too Long&quot;); @header(&quot;Status: 414 Request-URI Too Long&quot;); @header(&quot;Connection: Close&quot;); @exit; } } }<\/code><\/pre><\/div>\n\n\n\n<h3>5. Paginate your site without a plugin<\/h3>\n\n\n\n<p>By adding more of the &quot;Forward&quot; and &quot;Back&quot; 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.<strong> WordPress pagination easily<\/strong> We can provide it using a code snippet. Of course, we still use your theme&#039;s functions.php file.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>global $wp_query; $total = $wp_query-&gt;max_num_pages; \/\/ only bother with the rest if we have more than 1 page! if ( $total &gt; 1 ) { \/\/ get the current page if ( !$current_page = get_query_var(&#039;paged&#039;) ) $current_page = 1; \/\/ structure of &quot;format&quot; depends on whether we&#039;re using pretty permalinks $format = empty( get_option(&#039;permalink_structure&#039;) ) ? &#039;&amp;page=%#%&#039; : &#039;page\/%#%\/&#039;; echo paginate_links(array( &#039;base&#039; =&gt; get_pagenum_link(1) . &#039;%_%&#039;, &#039;format&#039; =&gt; $format, &#039;current&#039; =&gt; $current_page, &#039;total&#039; =&gt; $total, &#039;mid_size&#039; = &gt; 4, &#039;type&#039; =&gt; &#039;list&#039; )); }<\/code><\/pre><\/div>\n\n\n\n<h3>6. Hide the WordPress Admin Bar<\/h3>\n\n\n\n<p>When logging into the site with any role, if it is not blocked by your theme <strong>WordPress-only system admin bar<\/strong> 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&#039;s not very essential <strong>Hide WordPress admin bar<\/strong> Let&#039;s apply it on our site without forgetting the process.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>\/ Remove the admin bar from the front end add_filter( &#039;show_admin_bar&#039;, &#039;__return_false&#039; );<\/code><\/pre><\/div>\n\n\n\n<h3>7. Displaying Post Cover Images in RSS Feed<\/h3>\n\n\n\n<p>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.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>\/\/ Put post thumbnails into rss feed function wpfme_feed_post_thumbnail($content) { global $post; if(has_post_thumbnail($post-&gt;ID)) { $content = &#039;&#039; . $content; } return $content; } add_filter(&#039;the_excerpt_rss&#039;, &#039;wpfme_feed_post_thumbnail&#039;); add_filter(&#039;the_content_feed&#039;, &#039;wpfme_feed_post_thumbnail&#039;);<\/code><\/pre><\/div>\n\n\n\n<p>8. Change WordPress Author Link Structure<\/p>\n\n\n\n<p>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 &quot;link&quot;, which has a pure link structure,<strong>margarit.com.tr\/author\/authorname<\/strong>The \u201d 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 &quot;<strong>\/author\/authorname<\/strong>&#039; for example &#039;<strong>margarit.com.tr\/authors\/authorname<\/strong>\u201d to change the code below to our theme. <strong>functions.php<\/strong> add it to the page.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>add_action(&#039;init&#039;, &#039;cng_author_base&#039;); function cng_author_base() { global $wp_rewrite; $author_slug = &#039;authors&#039;; \/\/ change slug name $wp_rewrite-&gt;author_base = $author_slug; }<\/code><\/pre><\/div>\n\n\n\n<p>9. Automatic Twitter Usernames Tagging and Defining<\/p>\n\n\n\n<p>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. <strong>with @ character assignment<\/strong> @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.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>function content_twitter_mention($content) { return preg_replace(&amp;#039;\/([^a-zA-Z0-9-_&amp;amp;])@([0-9a-zA-Z_]+)\/&amp;#039;, &amp;quot;$1&lt;a href=&quot;http:\/\/twitter.com\/$2&quot; target=&quot;_blank&quot; rel=&quot;nofollow&quot;&gt;@$2&lt;\/a&gt;&amp;quot;, $content); } add_filter(&amp;#039;the_content&amp;#039;, &amp;#039;content_twitter_mention&amp;#039;); add_filter(&amp;#039;comment_text&amp;#039;, &amp;#039;content_twitter_mention&amp;#039;);<\/code><\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>In our article, we will talk about code pieces nicknamed &quot;Code Snippet&quot; for Wordpress, which are simple but can create convenience in vital issues.<\/p>","protected":false},"author":1,"featured_media":4284,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[187,188,186,185,190,195,197,189],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.margarit.com.tr\/en\/wp-json\/wp\/v2\/posts\/4250"}],"collection":[{"href":"https:\/\/www.margarit.com.tr\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.margarit.com.tr\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.margarit.com.tr\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.margarit.com.tr\/en\/wp-json\/wp\/v2\/comments?post=4250"}],"version-history":[{"count":3,"href":"https:\/\/www.margarit.com.tr\/en\/wp-json\/wp\/v2\/posts\/4250\/revisions"}],"predecessor-version":[{"id":4256,"href":"https:\/\/www.margarit.com.tr\/en\/wp-json\/wp\/v2\/posts\/4250\/revisions\/4256"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.margarit.com.tr\/en\/wp-json\/wp\/v2\/media\/4284"}],"wp:attachment":[{"href":"https:\/\/www.margarit.com.tr\/en\/wp-json\/wp\/v2\/media?parent=4250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.margarit.com.tr\/en\/wp-json\/wp\/v2\/categories?post=4250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.margarit.com.tr\/en\/wp-json\/wp\/v2\/tags?post=4250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}