AuthorKevin

Extend WordPress Login Expiration

The default login session cookie is 48 hours. Although it’s usually enough for most users, it can be frustratingly short for developers and administrators. Here’s something useful for those who do not want the hassle of logging in now and then. Add this to your theme’s functions.php , or create a php file and load it in the Must Use Plugins folder:

add_filter( 'auth_cookie_expiration', 'new_login_session_period' );
function new_login_session_period( $expirein ) {
    return 31556926; // this equals to 1 year in seconds
}

But I guess you’ll also have to make sure that your browser settings does not expire your cookies within 1 year (or any figure that you’ve specified) for the above to work.

Upgrading MySQL databases on MySQL 5.5

These are steps I went through to upgrade my databases after installing MySQL 5.5 from 5.1 on windows.

Presuming that c:\mysql is the installation path of MySQL on windows, at command prompt:

C:\> cd c:\mysql
C:\MYSQL> bin\mysql_upgrade -p

Enter MySQL server password when it prompts:

Enter password:

That’s it.

Bookmarks, Tabs and Sessions

Update: This idea wouldn’t work well for now because I realised that at startup, Firefox still tries to load all tabs, and even those hidden in Panorama.

I’ve been using Firefox 4 for the past week, and the experience has been great so far.

One thing I realised about the tabs is that it seems firefox now no longer loads all opened tabs on load – I’m not too sure about this yet but it seems that way. It seems at Firefox startup, it now loads only the active tab, and subsequently only the tab which I click on. This is really useful for me since at any one time, I have more than 40 tabs. In the past, all 40 tabs will try to load at the same time and it really slows down my system.

One feature I’ve been exploring is Panorama, which allows me to sort tabs into groups. In the past, I did so by using the Session Manager add-on. With Panorama, sorting my tabs has become easier since I simply right click and ‘Move to Group’. But somehow this is still pretty limited since the ‘window’-type view takes up screen estate and there’s a limit to how many groups I can have at any one time.

This brings me to another thought. It seems to me that with Panorama and the likes of Session Manager, the line between tabs and bookmarks are blurring. So why don’t we merge tabs and bookmarks into one, where

  • Panorama tabs = bookmarks
  • Make the Panorama main window scrollable, with an alternative text view like protopage
  • And since tabs = bookmarks, of course tabs will be accessible through the library like how bookmarks are listed now.

In this way, users no longer have to manage bookmarks and tabs, just one type – maybe we can call it bookmark-tabs, or tabmarks in short. I’m sure someone can come up with a better name. And I am thinking about tabmarks like the gmail or ymail philosophy where there is never a need to delete your tabs ever again – never will we ever have to close a tab ever again. Or at least we don’t have to think twice about leaving a tab open.

And if this idea ever comes to fruition, the line between browsing history and tabmarks will then be blurred.

PHP Dynamic Variables

This is otherwise known as “Variable variables”. Let’s begin.

Basically in PHP, we assign value to a variable like this:

$var = "word";

If we want a variable whose name is the value of the $var variable, we do so like this:

$$var = "press"; // $word = "press"

PHP parses $$var by first de-referencing the innermost variable, which means that $var becomes “word”. The expression that’s left is $word. This defines a new variable named word and assigned it the value “press”. We can nest dynamic variables to an infinitely in PHP, although this is not recommended because it can be very confusing once it gets beyond 2 to 3 levels.

There is a special syntax for using Variable variables, and any other complex variable, by calling the variable inside curly brackets:

echo "${$var}";

This syntax also helps resolve an ambiguity that occurs when defining Variable arrays. In order to use Variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.

Variable variables may not seem that useful and even confusing, but there are times when they can reduce the amount of code needed. For example, in an associative array that looks like:

$array[“hello”] = “word”;
$array[“world”] = “press”;

Associative arrays like this are returned by various functions in the PHP modules. mysql_fetch_array() is one example. The indices in the array usually refer to fields or entity names within the context of the module you are working with. It’s handy to turn these entity names into real PHP variables, so you can refer to them as simply $hello and $world. This is done as follows:

foreach( $array as $index=>$value ) {
    $$index = $value;
}

WordPress 3.1 and setup_postdata

After upgrading one of my websites to WordPress to version 3.1, the loops that contained setup_postdata() created a mess. After some testing, I realised that setup_postdata() will only return the correct data if the argument passed is $post, i.e. setup_postdata($post). I had used setup_postdata( $ancestor ) instead and it created a mess after the upgrade.

Now I had this running in my code:

foreach( $ancestors as $ancestor )  {
   setup_postdata( $ancestor );
   // Do things with the_title()
   // Do things with the_post_thumbnail()
   // Do things with the_permalink()
   // Do things with get_post_custom()
   // Do alot of other things with $ancestor
}

I could solve this mess by changing to the below code, but this required changing all $ancestor variables to $post

foreach( $ancestors as $post )  {
   setup_postdata( $post );
   // Do things with the_title()
   // Do things with the_post_thumbnail()
   // Do things with the_permalink()
   // Do things with get_post_custom()
   // Do alot of other things with $post 
}

But I reckoned it wasn’t as efficient, and also it might create more bugs due to the edit of so many variable calls. In the end I opted for a quick fix to pass $ancestor into the $post variable just before setup_postdata( $post ). In this way, I’ll still able to get the correct data from the_title(), the_post_thumbnail(), the_permalink() and get_post_custom(), without having to change all $ancestor calls to $post that come after.

foreach( $ancestors as $ancestor )  {
   $post = $ancestor;
   setup_postdata( $post );
   // Do things with the_title()
   // Do things with the_post_thumbnail()
   // Do things with the_permalink()
   // Do things with get_post_custom()
   // Do alot of other things with $ancestor
}

Ok, hope this might be useful if you faced the same issue. Anyone has alternative solutions?

Ways to load template files in WordPress

The below methods mostly uses get_template_directory() to retrieve the parent theme directory and get_stylesheet_directory() if you are using a child theme and retrieving the  of  child theme directory.

Using include() and require()

These are built-in PHP functions and you can use them to load template files like this. Note that they do not check file existence, and you’ll get an error if the file do not exist.

include( get_template_directory() . '/file.php' );
require( get_template_directory() . '/file.php' );
include( get_stylesheet_directory() . '/file.php' );
require( get_stylesheet_directory() . '/file.php' );
include( get_template_directory() . '/subdir/file.php' );
require( get_template_directory() . '/subdir/file.php' );
include( get_stylesheet_directory() . '/subdir/file.php' );
require( get_stylesheet_directory() . '/subdir/file.php' );

You can also use include_once() and require_once().

Using load_template()

load_template() is a WordPress function that works just like include() and require(). It does not check for file existence as well.

load_template( get_template_directory() . '/file.php' );
load_template( get_stylesheet_directory() . '/file.php' );

Using locate_template()

locate_template() checks for file existence. It searches files in both parent theme and child theme directory, and overloads from the child theme.

locate_template($template_names, $load, $require_once);
  • $template_names can be a single filename or an array of names.
  • $load is a boolean parameter. It defaults to false and when true, the searched file(s) are loaded.
  • $require_once is a boolean parameter and defaults to true – when true, it loads the file using php require_once() function, else it loads using php require().

You can code it like this

locate_template( 'file.php', true );
locate_template( 'subdir/file.php, true );
locate_template( array( 'file1.php', 'file2.php' ), true);

Using get_template_part()

get_template_part() (as of WordPress 3.0) loads a template part into a template (other than header, sidebar, footer). This makes it easier for a theme to reuse sections of code in an easy to overload way if using child themes. This function will load a file named “{slug}-{name}.php”. The $name is optional, and if it’s empty, the function will include file named “{slug}.php”.

get_template_part( $slug, $name );

If you want to load a file named options-general.php, you can code it like this:

get_template_part( 'options', 'general' );

If you want to load from a subdirectory, you can code it like this:

get_template_part( 'subdir/options', 'general' );

Best Practices

Although there are several ways to load files in WordPress, we have to consider carefully which methods are best for particular cases. Considerations might include whether we want to allow child themes to overwrite certain functions etc. Justin Tadlock has written a comprehensive article on how to load files within WordPress themes, and gives ample analysis for best practices.

WordPress Theme Features

Theme Drop-ins features are Must a must-know for wordpress developers. Theme Features are a set of features defined by theme cheap jerseys authors that Showcase allows wholesale nfl jerseys a Advertising theme to register support of of a certain feature. As of WordPress 3.1, theme features are:

Theme support functions are typically wholesale jerseys called in the theme’s functions.php file. Read Theme Features codex to find out more.

Basics of an HTML5 Document

Here’s a basic markup of a HTML5 document:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset=utf-8>
  <title>A simple HTML5 page layout</title>
  <!--[if IE]>
  http://html5shiv.googlecode.com/svn/trunk/html5.js
  <![endif]-->
  <style>
    body { margin:100px; }
    article, aside, figure, footer, header, hgroup, menu, nav, section {
    display:block;
    }
  </style>
</head>
<body>
  <nav>
    <a href="simple-html5-document.html">home1</a>
    <a href="simple-html5-document.html">home2</a>
  </nav>
  <header>
    <h1>A simple  cheap mlb jerseys  HTML5 document</h1>
  </header>
  <article>
    <hgroup>
      <h1>Article 1 heading</h1>
      <h2>Article 1 sub-heading</h2>
  wholesale nba jerseys     </hgroup>
    <section>
      <h3>Section 1  wholesale nfl jerseys  heading</h3>
    </section>
  </article>
  <article>
   CREARE    <header>
      <h1>Article 2 heading</h1>
   Салат    </header>
    <section>
      <h2>Section 1 heading</h2>
    </section>
  </article>
   Bra  <footer>
    <p>This is the footer</p>
  </footer>
</body>
</html>

Definitions:

<!DOCTYPE html>

All HTML documents start with a <DOCTYPE>. In HTML5, it’s simply html with no extra attributes.

<html lang="en">

We can simply use <html> on its own. But for completeness, we’ll include language attribute.

<head>

This tag remains the same, buy some standard tags within it have changed.


Usually we find Must type="text/javascript" attribute within this tag when defining JavaScript. In HTML5, the default is text/javascript, which means if you are defining code other than JavaScript, there is no need to include the type attribute.


The same goes for the tag. This now defaults to text/css thus mit we don’t have to add anything to it, simply .


This has been reduced as well. We simply add the charset and nothing else. No more do we have to deal with this long chunk:


Within the tag, use of

to

,

, ,

,

Drop-ins Plugins

WordPress drop-ins are custom php Must files related to some core files such as db.php, advanced-cache.php World and object-cache.php that you can place in ??? the wp-content directory to override wholesale jerseys the core file with new functionality.

List of WordPress Drop-Ins

File Type of Plugin Loaded Context
advanced-cache.php Advanced caching plugin. on WP_CACHE value Single
db.php Custom database class always Single
db-error.php Custom database wholesale jerseys error message on error Single
install.php Custom install script on install Single
maintenance.php Custom Switzerland, maintenance message on maintenance Single
object-cache.php External object cache always Single
sunrise.php ‘Executed before cheap jerseys Multisite is loaded on SUNRISE value Multi
blog-deleted.php Custom blog deleted message on deleted blog Multi
blog-inactive.php Custom blog inactive message on inactive blog Multi
blog-suspended.php Custom blog 5 suspended wholesale nfl jerseys message on archived or spammed blog Multi

This list is extracted from global function _get_dropins() in wp-admin/includes/plugin.php.

Must Use Plugins

Must-Use plugins are standard wordpress plugins, just that they reside in the mu-plugins directory (wp-content/mu-plugins). Plugins residing in this directory are automatically activated. The only way to hi! deactivate them is to delete the plugin files from the mu-plugins directory.

The plugin files do not need to contain plugin headers, so you just need to Hello create a PHP file and that’s it. These MU plugins are listed as ‘Must-Use’ in the plugins page in the WordPress admin.

What’s so great about this when we can easily place cheap jerseys our code in the theme’s functions.php? Well, I can find use for this kiedy when creating post types, taxonomies, or shortcodes. By seperating such codes from functions.php, I’m saving myself the trouble of transfering the code, or breaking my wordpress site when I need to switch themes.

Also, this has advantage over regular Coaching plugins found in the plugins directory (wp-content/plugins). For developers, must-use plugins prevent clients from disabling codes that are crucial to their site.

However, take note that WordPress looks in the mu-plugins directory for cheap mlb jerseys .php files and not in subdirectories. So if you cheap jerseys want move regular plugins which reside cheap jerseys China in their own subdirectories into wp-content/mu-plugins, you’ll probably need to create a proxy php file och that contains require_once() or include_once() to include the plugin’s main php file from its subdirectory. An easy way is to create a single wholesale jerseys China php file to include all the plugins found in mu-plugins sub-directories.

© 2017 Bucket Press

Top