• August 5, 2018

    +https://codeable.io/wordpress-hacks-without-plugins/

    One of the things I love about WordPress is the easiness to empower it with (almost) endless features thanks to plugins. With 38000+ free plugins and an uncountable number of premium and custom ones, there’s a WordPress plugin for every need and budget. And if you look online to get WordPress tips, tricks or hacks, you’ll surely end on a blog post that’s covering a list of (great) plugins that will provide you with what you were looking for. Actually, it’s quite difficult to find a great list of WordPress tips, tricks and hacks that doesn’t count a plugin in it.

    I know, they are powerful tools, but plugins aren’t always required to enable a specific featurebecause WordPress alone can do cool things too.

    So my question is: how many interesting features can be enabled on a WordPress website without installing a single plugin? I’ve put together a list of the best WordPress code snippets (or WordPress hacks, you choose) that you can play with right away and see for yourself you don’t always need a plugin.

    The following piece of codes can break your website, so please pay attention and make a backup before getting your hands dirty with them.

    WordPress Code Tweaks: Admin and dashboard

    Change the login logo with yours

    Let’s start with one about branding: if you ever wanted to change the WordPress logo on the login page with yours (or a client’s), that’s the code you’ll need. It’s easy: just open your functions.phpfile and paste the following:

    function my_custom_login_logo() {
        echo '<style type="text/css">
            h1 a { background-image:url('.get_bloginfo('template_directory').'/images/custom-login-logo.gif) !important; }
        </style>';
    }
    
    add_action('login_head', 'my_custom_login_logo');
    

    Reinforcing your brand communication with this little tweak is just the first step, you’ll see how many things you can do without installing plugins. [via]

    Have you ever wanted to “feel” the WordPress dashboard closer to your business? How about featuring a new client’s logo in there? That would be cool but installing a new plugin just for that sounds like too much. How about I tell you can do this with no plugin at all? Open the functions.php file and paste this:

    function custom_admin_logo() {
      echo '<style type="text/css">
              #header-logo { background-image: url('.get_bloginfo('template_directory').'/images/admin_logo.png) !important; }
            </style>';
    }
    add_action('admin_head', 'custom_admin_logo');
    

    You now should put your logo in the wp-images folder and call it admin_logo.png. [via]

    Disable WordPress Login Hints

    Keeping your WordPress website secure is important (see how to improve it), and there’s a little thing that will make a hacker’s life more difficult: not providing detailed error messages on the WordPress login page. To disable these warnings you need to open your functions.php and copy this:

    function no_wordpress_errors(){
      return 'GET OFF MY LAWN !! RIGHT NOW !!';
    }
    add_filter( 'login_errors', 'no_wordpress_errors' );
    

    This way you no longer provide useful hints that could be exploited for malicious activities. [via]

    Keep logged in WordPress for a longer period

    When you work on public wi-fi networks, or not on your computer, it’s always good practice to log out from those devices. But when you’re on your home network and your computer, getting kicked out after a while might be annoying to some. How about extending the time of your WordPress login session? This code will do the trick, just open the functions.php file and copy it:

    add_filter( 'auth_cookie_expiration', 'stay_logged_in_for_1_year' );
    function stay_logged_in_for_1_year( $expire ) {
      return 31556926; // 1 year in seconds
    }
    

    By the default, WordPress will keep you logged in for 2 weeks when you check the “Remember Me” option at login. You can set the expiry date of the authorization login cookie by replacing the “31556926” with your preferred time span. [via]

    Replace “Howdy” with “Logged in as” in WordPress bar

    If the default “Howdy” is too informal or you just want another message on the WordPress menu bar, head over to your functions.php and add this:

    function replace_howdy( $wp_admin_bar ) {
        $my_account=$wp_admin_bar->get_node('my-account');
        $newtitle = str_replace( 'Howdy,', 'Logged in as', $my_account->title );
        $wp_admin_bar->add_node( array(
            'id' => 'my-account',
            'title' => $newtitle,
        ) );
    }
    add_filter( 'admin_bar_menu', 'replace_howdy',25 );
    

    You just need to insert your new message as the 2nd element within the $newtitle array, and you’re done. [via]

    Change the footer text on WordPress dashboard

    Branding is about messaging and consistency. So if you’re building up a website for a client, you’d want them to feel happy about what you did and feel important too. So, besides having their logo on their login page and dashboard, how about adding their tagline or some cool text on the dashboard footer too? Cool, just open up the functions.php file and go with this:

    function remove_footer_admin () {
      echo "Your own text";
    } 
    
    add_filter('admin_footer_text', 'remove_footer_admin');
    

    Now it’s all about your creativity to impress them, make them smile, or even quote some motivational words. [via]

    Add a shortcode to widget

    Shortcodes are super-useful because they can just replace what that could be a long piece of code with a little line between square brackets. By default, WordPress widgets aren’t enabled to manage shortcodes and they handle them like regular text. But there’s the possibility to empower WordPress widgets with the ability to use shortcodes as well thanks to the following piece of code added in the functions.php file:

    add_filter('widget_text', 'do_shortcode');
    

    This code tweak will make you take advantage of shortcodes on other great tools WordPress comes with: widgets. [via]

    7 WordPress admin and dashboard hacks you can do without pluginsClick To Tweet

    WordPress Code Tweaks: Posts and Pages

    Require a featured image before you can publish posts

    Publishing a blog post or a page on WordPress is easy but in many cases words alone aren’t enough: news, product pages, etc. they all need images to stand out and communicate better. So how about making it mandatory for your users to add a featured image to their post or page unless they aren’t able to publish it? Great, open functions.php and apply the following code:

    add_action('save_post', 'wpds_check_thumbnail');
    add_action('admin_notices', 'wpds_thumbnail_error');
    
    function wpds_check_thumbnail( $post_id ) {
      // change to any custom post type 
      if( get_post_type($post_id) != 'post' )
          return;
    
      if ( ! has_post_thumbnail( $post_id ) ) {
        // set a transient to show the users an admin message
        set_transient( "has_post_thumbnail", "no" );
        // unhook this function so it doesn't loop infinitely
        remove_action('save_post', 'wpds_check_thumbnail');
        // update the post set it to draft
        wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));
    
        add_action('save_post', 'wpds_check_thumbnail');
      } else {
        delete_transient( "has_post_thumbnail" );
      }
    }
    
    function wpds_thumbnail_error() {
      // check if the transient is set, and display the error message
      if ( get_transient( "has_post_thumbnail" ) == "no" ) {
        echo "<div id='message' class='error'><p><strong>You must add a Featured Image before publishing this. Don't panic, your post is saved.</strong></p></div>";
        delete_transient( "has_post_thumbnail" );
      }
    }
    

    Isn’t this useful? Of course, you can set your custom alert message by editing whatever fits your needs. [via]

    Reduce Post Revisions

    Revisions are the WordPress built-in time machine to the edits of your content. By default, there’s no limit to the number of post revisions that are stored in your database, but “infinite” is a huge number that hardly would be useful to the purpose of your website. That’s why you can set a specific number of revisions you want to be saved. For this tweak, you should open the wp-config.php file and add:

    define( 'WP_POST_REVISIONS', 3 );
    

    Pick a number that works for you and put it in it. If you want to disable the storage of revisions (and just have the autosave), use “-1” instead.[via]

    Delay posting to my RSS feeds for 60 minutes

    Imagine this: you just published the latest post on your blog and the RSS feed is already sending it to your subscribers, when you see there’s a typo in the headline. That’s a bummer, if only you had some more time to check everything out better… With this piece of code you can! Specifically, you delay the posting to your RSS feeds so you’ll have enough time to check for the last time. If you like the idea, open the functions.php file and add this:

    function Delay_RSS_After_Publish($where) {
      global $wpdb;
    
      if (is_feed()) {
        $now = gmdate('Y-m-d H:i:s');
        $wait = '60';
        $device = 'MINUTE';
        $where.=" AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, '$now') > $wait ";
      }
      return $where;
    }
    
    add_filter('posts_where', 'Delay_RSS_After_Publish');
    

    This is useful to check if there are typos, broken links etc. To change the delayed timeframe, just edit the $wait = ’60’; with another value that better works for you. [via]

    Change the length of excerpts

    For some scenarios, the default excerpts don’t fit the layout, so you need to change it accordingly. If that’s what you need, with your functions.php file opened, copy in it the following code:

    function custom_excerpt_length( $length ) {
      return 20;
    }
    add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
    

    Tweak the “return 20” line by adding the number of words you’d like to be shown in your excerpts. Remember: the WordPress default value for excerpt length is 55. [via]

    Change the post auto-save interval

    The auto-save feature is your safety net: automagically WordPress saves your work, so you don’t have to worry about it in the event of a browser crash or a blackout. For some users though, the 1 minute default could be a little too much and they keep interrupting their work to push the save button. For these little crazy monkeys (count me in), it’d be great to decrease the time span between auto-saves without a plugin. And this is easily achieved by adding the following code to the wp-config.php:

    define( 'AUTOSAVE_INTERVAL', 45 );
    

    Of course the code tweak works the other way around, so if you want to increase the auto-save interval, just set a higher value like 120 seconds or even more, you adrenaline junkies :D.[via]

    5 WordPress posts and pages hacks you can do without pluginsClick To Tweet

    WordPress Code Tweaks: Search

    Show the number of results found

    Search results pages are important for a website UX, so it should be pretty useful to the user. Problem is many search results page give no information about how many pages on what I’m searching for there are in a website. Thanks to the following line of code in your search.php file, you’ll be able to show how many items are related to that search:

    <h2 class="pagetitle">Search Result for <?php /* Search Count */ $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e(''); _e('<span class="search-terms">'); echo $key; _e('</span>'); _e(''); echo $count . ' '; _e('articles'); wp_reset_query(); ?></h2>
    

    This way a generic and uninformative title such as “Search Results” becomes a valuable one by providing the exact number of articles related to the search like “Search Result for search terms – 12 Articles”. [via]

    Exclude categories from search

    If you want to have a deeper control over the results users can get, you might need a way not to show specific categories within the results page. The use cases are endless: promotional material, press release, translated content, portfolio items, just to name a few. Interested in how to do that? Open your functions.php file and add:

    function SearchFilter($query) {
      if ( $query->is_search && ! is_admin() ) {
        $query->set('cat','8,15'); 
      }
      return $query; 
    }
    add_filter('pre_get_posts','SearchFilter');
    

    With the possibility to exclude specific categories from your search results page, you’ll also improve the quality of the information you deliver to your users and readers. [via]

    Exclude pages from search

    Same story goes for pages we you might want to keep out of results. So, add the following code to your functions.php file:

    function modify_search_filter($query) {
      if ($query->is_search) {
        $query->set('post_type', 'post');
      }
      return $query;
    }
    
    add_filter('pre_get_posts','modify_search_filter');
    


เวอไนน์ไอคอร์ส

ประหยัดเวลากว่า 100 เท่า!






เวอไนน์เว็บไซต์⚡️
สร้างเว็บไซต์ ดูแลเว็บไซต์

Categories