Category Archives: Tips and Tricks

Update all URLs in database except in serialize data

Updating URLs in the database can cause problems if you include those in serialized arrays.

These queries leave serialized arrays untouched:

 MySQL |  copy code |? 
1
UPDATE wp_options SET option_value = REPLACE(option_value, ‘http://devdomain.local, ‘http://livedomain.com‘) WHERE option_value NOT LIKE%{%; UPDATE wp_postmeta SET meta_value = REPLACE(meta_value, ‘http://devdomain.local, ‘http://livedomain.com‘) WHERE meta_value NOT LIKE%{%; 

More info here:

http://wp-types.com/faq/how-do-i-migrate-a-wordpress-site-from-one-domain-to-the-other/

Posted in Tips and Tricks, Wordpress 3 | Leave a comment

Enable ‘Read More’ link in posts outside the default blog page

Add the $more variable before the content in the loop:

 PHP |  copy code |? 
1
global $more; 
2
$more = 0; 
3
the_content('[Read more...]');

Posted in Tips and Tricks, Wordpress 3 | Leave a comment

Access to password protected directory giving 404 file not found error

If you have a directory protected by a password using HTTP Basic Authentication (set up in cpanel for instance) you will get 404 errors due to the way the WordPress permalinks works.

The solution is to add this to the top of your .htaccess file in the site root:

 HTML |  copy code |? 
1
#prevent 404s when accessing password protected directories
2
ErrorDocument 401 ./error.html
3
ErrorDocument 403 ./error.html
4

You dont need to upload a valid error.html file

Full explanation here:

http://www.myguysolutions.com/2010/02/10/how-to-fix-wordpress-404-errors-on-password-protected-directories/

Posted in Tips and Tricks | Leave a comment

Update Text Using PO Files. Translate Text.

A basic guide to translating wp-e-commerce using poEdit

1. Download and Install poEdit : http://www.poedit.net/download.php
2. Run poEdit and open a translation file (.po) from the `wp-e-commerce/languages` folder
3. Go through the list of words / sentences and add in your translations
4. In poEdit make sure you have the box checked in Preferences>Editor ‘Automatically Save .mo file on save’
5. Save your translation file in the `wp-e-commerce/languages` folder give it a descriptive name ie: wpsc-en_US means english – american, wpsc-fr_FR means french – france (note: your filename must have ‘wpsc-’ at the start to work)
6. In your wordpress wp-config.php file edit the line that looks like this:
`define (‘WPLANG’, ”);`
with the file name (without extension) inside the second set of quotation marks.
i.e If I saved my mo file as `wpsc-en_BG.mo` then I would change the WPLANG line to:
`define (‘WPLANG’, ‘en_BG’);`
7. Save your wp-config.php
8. Refresh your website in a web browser, your new translations should now be viewable on your site.

References
http://codex.wordpress.org/WordPress_in_Your_Language
http://codex.wordpress.org/I18n_for_WordPress_Developers
http://www.forumone.com/blogs/post/translating-your-wordpress-site-using-pot-file

Posted in Tips and Tricks, Wordpress 3, WP Ecommerce | Leave a comment

Disable Chrome Minimum Font Size

Using CSS:

 CSS |  copy code |? 
1
2
/*styles for Chrome */
3
@media screen and (-webkit-min-device-pixel-ratio:0) {
4
  Body {
5
   -webkit-text-size-adjust:none;
6
    }
7
 }

This can cause issues in that it disables zoom for font size but it does remove the issue of Chrome wrecking layouts by increasing font sizes etc

Posted in Browsers, CSS, General CSS, Tips and Tricks | Leave a comment

Load Contact Form 7 JS Scripts Only When Needed

Contact Form 7 loads its scripts on all pages which uses memory and increases load time.

Add this to functions.php to only load scripts when required:

 PHP |  copy code |? 
1
// only load js scripts for wpcf7 on contact and join pages
2
// Add the Contact Form 7 scripts on selected pages
3
 
4
add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );
5
function my_deregister_javascript() {
6
if ( !is_page(array(42,35)) ) {
7
wp_deregister_script( 'contact-form-7' );
8
}
9
}

Posted in Contact Form 7, Plugins, Tips and Tricks, Wordpress 3 | 2 Comments

Load Custom JS Scripts

To load a custom js script when, for instance page ID 434 is loaded place the following in theme functions.php:

 PHP |  copy code |? 
1
// load our own validation script on page 434 add_action( 'wp_enqueue_scripts', 'babelscribe_load_scripts' ); function babelscribe_load_scripts() { // register your script location, dependencies and version wp_register_script('validate_script', get_stylesheet_directory_uri().'/js/actions.js', array('jquery')); // enqueue the script if (is_page(434)) { // only load if it is selected page wp_enqueue_script('validate_script'); } } 

Posted in Code, Theming, Tips and Tricks, Wordpress 3 | Leave a comment

Querying the WordPress Database

This is a summary of basic ways to query the WordPress database:

Basic Query returning an object:

 PHP |  copy code |? 
1
<?php
2
   $posts = $wpdb->get_results("SELECT ID, post_title FROM wp_posts WHERE post_status = 'future'
3
   AND post_type='post' ORDER BY post_date ASC LIMIT 0,4")
4
 
5
   // Echo the title of the first scheduled post
6
   echo $posts[0]->post_title;
7
?>

 Basic Query returning a row:

 PHP |  copy code |? 
1
<?php
2
   $posts = $wpdb->get_row("SELECT ID, post_title FROM wp_posts WHERE post_status = 'publish'
3
   AND post_type='post' ORDER BY comment_count DESC LIMIT 0,1")
4
 
5
   // Echo the title of the most commented post
6
   echo $posts->post_title;
7
?>

Basic query returning a variable:

 PHP |  copy code |? 
1
<?php
2
   $email = $wpdb->get_var("SELECT user_email FROM wp_users WHERE user_login = 'danielpataki' ")
3
 
4
   // Echo the user's email address
5
   echo $email;
6
?>

Insert into the database:

Format is as follows:

 PHP |  copy code |? 
1
$wpdb->insert( $table, $data, $format);

for example:

 PHP |  copy code |? 
1
<?php
2
   $wpdb->insert($wpdb->usermeta, array("user_id" => 1, "meta_key" => "awesome_factor", "meta_value" => 10), array("%d", %s", "%d"));
3
 
4
   // Equivalent to:
5
   // INSERT INTO wp_usermeta (user_id, meta_key, meta_value) VALUES (1, "awesome_factor", 10);
6
?>

Formats are:

%s = string

%d = decimal number

%f = floats

Update a row

Format is as follows:

 PHP |  copy code |? 
1
$wpdb->update( $table, $data, $where, $format = null, $where_format = null );

for example:

 PHP |  copy code |? 
1
$wpdb->update( $wpdb->posts, array("post_title" => "Modified Post Title"), array("ID" => 5), array("%s"), array("%d") );

Other Queries:

Use the general query method, for example:

 PHP |  copy code |? 
1
$wpdb->query("DELETE FROM wp_usermeta WHERE meta_key = 'first_login' OR meta_key = 'security_key' ");

SQL Injection Protection

All helper functions above escape the input for you.

The general query doesn’t escape data so use the prepare() function:

 PHP |  copy code |? 
1
$sql = $wpdb->prepare( 'query' [, value_parameter, value_parameter ... ] );

or

 PHP |  copy code |? 
1
$sql = $wpdb->prepare( "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value ) VALUES ( %d, %s, %d )", 3342, 'post_views', 2290 )
2
$wpdb->query($sql);

Insert_ID()

Get last insert auto increment value:

 PHP |  copy code |? 
1
 $meta_id = $wpdb->insert_id;

Table Names

Because table prefix might change use this function to get table name:

 PHP |  copy code |? 
1
//get the name of the posts table (wp_posts)
2
$wpdb->posts
3
 
4
//get name of postmeta table (wp_postsmeta)
5
$wpdb->postsmeta

Add New Table Name

If you create a custom table in the database you need to add it to the $wpdb object by adding this to your themes functions.php file:

 PHP |  copy code |? 
1
// add table to $wpdb class
2
if (!isset($wpdb->paystation_transactions)) {
3
$wpdb->paystation_transactions = $table_prefix . 'paystation_transactions';
4
}
5

Full article is here >>

Posted in Code, Tips and Tricks, Wordpress 3 | Leave a comment

Chrome conditional CSS styles

Conditional styling for Chrome browser can be achieved by adding this to the style sheet:

 CSS |  copy code |? 
1
@media screen and (-webkit-min-device-pixel-ratio:0) { 
2
  Body {
3
    font-size: 20px;      
4
    }
5
}

Posted in CSS, General CSS, Tips and Tricks | 1 Comment

Custom Widget Area

Register a Custom Sidebar as described here

Add widget to the sidebar.

Use the following in your template file to show the sidebar (custom widget area)

 PHP |  copy code |? 
1
// Custom widget Area Start
2
            if( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Left Column Widgets') ) :
3
                endif;
4
// Custom widget Area end

where ‘Left Column Widgets’ is the registered name of the sidebar.

Posted in Code, Theming, Tips and Tricks, Wordpress 3 | Leave a comment